반응형
WPF 코드로 선그리기에 대한 예제입니다.
System.Windows.Shapes.Line 객체를 사용해서 선을 그립니다.
첫번째 좌표에서 두번째 좌표까지 연결되는 선을 그리게 되고, 색깔, 두께, 점선 등을 지정할 수 있습니다.
[코드]
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 WpfApplication1 { ////// MainWindow.xaml에 대한 상호 작용 논리 /// public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void Window_Loaded(object sender, RoutedEventArgs e) { Line line1 = CreateLine(10, 10, 300, 300, Brushes.Blue, 2, null); Line line2 = CreateLine(300, 10, 10, 300, Brushes.Red, 2, new DoubleCollection{3, 2}); this.canvas1.Children.Add(line1); this.canvas1.Children.Add(line2); } public Line CreateLine(double x1, double y1, double x2, double y2, Brush brush, double thickness, DoubleCollection dashStyle) { Line line = new Line(); //첫번째 좌표 설정 line.X1 = x1; line.Y1 = y1; //두번째 좌표 설정 line.X2 = x2; line.Y2 = y2; line.Stroke = brush;//선색 지정 line.StrokeThickness = thickness;//선 두께 지정 line.StrokeDashArray = dashStyle;//점선 설정 - new DoubleCollection { 점 길이, 점 간격} return line; } } }
[결과]
반응형
'C# > WPF' 카테고리의 다른 글
WPF 선그리기 예제 (0) | 2020.10.28 |
---|---|
[C# WPF] 소스로 사각형 만들고 Text 적는 방법 (0) | 2018.04.20 |
[C# WPF]WinForm에 WPF Control사용하기 (0) | 2018.03.21 |
[C# WPF] 소스상에서 사각형 그리기(Rectangle : Shape) (0) | 2018.03.09 |
댓글