WPF几何绘图(二)画矩形

WPF几何绘图(二)画矩形_第1张图片

 

绘制矩形的时候需要使用RectangleGeometry类,代码实例如下:

RectangleGeometry myRectangleGeometry = new RectangleGeometry(); myRectangleGeometry.Rect = new Rect(50,50,25,25); Path myPath = new Path(); myPath.Fill = Brushes.LemonChiffon; myPath.Stroke = Brushes.Black; myPath.StrokeThickness = 1; myPath.Data = myRectangleGeometry;

矩形的相对位置和尺寸由 Rect 结构定义。相对位置是 50,50,高度和宽度均为 25,这将创建一个正方形。矩形的内部是使用 LemonChiffon 画笔绘制的,它的轮廓是用厚度为 1Black 笔画绘制的。

效果如下:

WPF几何绘图(二)画矩形_第2张图片

尽管此示例使用 Path 元素来呈现 RectangleGeometry,但还可以通过许多其他方法来使用 RectangleGeometry 对象。例如,RectangleGeometry 可用于指定 UIElementClip,或者指定 GeometryDrawingGeometry

 

当然如果你要画很多的矩形,比如矩形图表,如上图所示,还是要用到PathGeometry。

 

最上面我所显示的例子就是使用了PathGeometry,所用代码如下:

///

/// Interaction logic for DrawRect.xaml /// public partial class DrawRect : System.Windows.Window { private System.Windows.Threading.DispatcherTimer TimerClock; public DrawRect() { InitializeComponent(); DrawingRect(); //initialize Timer TimerClock = new System.Windows.Threading.DispatcherTimer(); //Creates a timerClock and enables it TimerClock.IsEnabled = false; TimerClock.Tick += new EventHandler(TimerClock_Tick); checkBox1.Click += new RoutedEventHandler(CheckBox_Checked); checkBox2.Click += new RoutedEventHandler(CheckBox_Checked); } private void CheckBox_Checked(object sender, EventArgs e) { //if (((CheckBox)sender).Name == "checkBox1") //{ TimerClock.Interval = new TimeSpan(0, 0, 1); if (checkBox1.IsChecked == true) TimerClock.IsEnabled = true; else TimerClock.IsEnabled = false; //} //else if (((CheckBox)sender).Name == "checkBox2") //{ // TimerClock.Interval = new TimeSpan(0, 0, 1); // if (checkBox2.IsChecked == true) // TimerClock.IsEnabled = true; // else TimerClock.IsEnabled = false; //} } private void TimerClock_Tick(object sender, EventArgs e) { canvas1.Children.Clear(); double y = 150; PathGeometry aa = new PathGeometry(); for (int i = 0; i < 360; i += 5) { double value = i + DateTime.Now.Second; double h = Math.Sin(value * Math.PI / 180) * 100; bool isMinus = false; if (h < 0) { isMinus = true; h = -h; aa.AddGeometry(new RectangleGeometry(new Rect(50 + i, y, 5, h))); } else aa.AddGeometry(new RectangleGeometry(new Rect(50 + i, y - h, 5, h))); } Path myPath = new Path(); myPath.Fill = Brushes.LemonChiffon; myPath.Stroke = Brushes.Black; myPath.StrokeThickness = 1; //myPath.Data = myRectangleGeometry; myPath.Data = aa; this.canvas1.Children.Add(myPath); } private void DrawingRect() { double y = 150; PathGeometry aa = new PathGeometry(); for (int i = 0; i < 360; i+=5) { double h = Math.Sin(i * Math.PI / 180) * 100; bool isMinus = false; if (h < 0) { isMinus = true; h = -h; aa.AddGeometry(new RectangleGeometry(new Rect(50 + i, y, 5, h))); } else aa.AddGeometry(new RectangleGeometry(new Rect(50 + i, y - h, 5, h))); } Path myPath = new Path(); myPath.Fill = Brushes.LemonChiffon; myPath.Stroke = Brushes.Black; myPath.StrokeThickness = 1; //myPath.Data = myRectangleGeometry; myPath.Data = aa; this.canvas1.Children.Add(myPath); } }

你可能感兴趣的:(WPF几何绘图(二)画矩形)