杭州盘发培训班:WPF几何绘图(二)画矩形

来源:百度文库 编辑:偶看新闻 时间:2024/04/29 11:50:30

WPF几何绘图(二)画矩形

2009-03-09 11:06 1498人阅读 评论(1) 收藏 举报

 

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

[c-sharp] view plaincopy?

  1. RectangleGeometry myRectangleGeometry = new RectangleGeometry();      
  2. myRectangleGeometry.Rect = new Rect(50,50,25,25);  
  3.   
  4. Path myPath = new Path();  
  5. myPath.Fill = Brushes.LemonChiffon;  
  6. myPath.Stroke = Brushes.Black;  
  7. myPath.StrokeThickness = 1;  
  8. myPath.Data = myRectangleGeometry;  

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

效果如下:

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

 

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

 

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

[c-sharp] view plaincopy

  1. ///    
  2.     /// 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);  
  •         }  
  •     }