반응형
안녕하세요. WPF관련 예제입니다.
소스로 사각형 ( Rectangle ) 을 만들고, 만들어진 사각형 안에 글자도 넣어보는 예제입니다.
코드부터 보시죠~
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WpfApplication2 { ////// MainWindow.xaml에 대한 상호 작용 논리 /// public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void Window_Loaded(object sender, RoutedEventArgs e) { Grid panel = new Grid(); //사각형을 감싸줄 Panel 생성 Rectangle rec = new Rectangle();//사각형 생성 rec.Width = 200; rec.Height = 100; rec.Stroke = Brushes.Blue; TextBlock textBlock = new TextBlock();//Text 생성 textBlock.Text = "TEXT BLOCK"; textBlock.HorizontalAlignment = System.Windows.HorizontalAlignment.Center; textBlock.VerticalAlignment = System.Windows.VerticalAlignment.Center; panel.Children.Add(rec);//Panel에 사각형 추가 panel.Children.Add(textBlock);//Panel에 Text 추가 Canvas.SetLeft(panel, 10);//Panel 위치 조정 Canvas.SetTop(panel, 10); this.canvas1.Children.Add(panel);//Panel 등록 } } }
Rectangle에 바로 Text를 넣는 방법이 없을까 하고 계속 찾아봤는데
전 못찾겠더라고요;
그래서 찾은 방법이 Rectangle을 Panel객체로 한번 더 싸서 넣는 방법이 있었습니다.
위 코드를 보시면 Grid의 Children에 Rectangle과 TextBlock를 위치시키고
Grid를 Canvas의 Children에 등록합니다.
(Grid는 Panel을 상속받은 객체입니다.)
그랬더니 아래와 같은 결과가~짜잔~!
이 방법으로 이제 동적으로 딴거도 만들어 봐야겠네요 >_<
반응형
'C# > WPF' 카테고리의 다른 글
WPF 선그리기 예제 (0) | 2020.10.28 |
---|---|
[C# WPF] 코드로 선 그리기 (0) | 2018.04.19 |
[C# WPF]WinForm에 WPF Control사용하기 (0) | 2018.03.21 |
[C# WPF] 소스상에서 사각형 그리기(Rectangle : Shape) (0) | 2018.03.09 |
댓글