本节我们讲一个关于在Sivlerlight中动态绘制矩形框的小技巧。此技巧可以让用户自定义的绘制矩形框。此技巧的关键在于,在一个Canvas中使 用其事件,来绘制矩形,注意这里选用Canvas是因为Canvas.Top和Canvas.Left是一个很好的定位方法。当用户想要动态绘制一个矩形 的时候,用户按下鼠标左键(MouseLeftButtonDown事件),记录当前鼠标点击的Canvas坐标,然后鼠标移动(MouseMove事 件)的时候再记录当前鼠标移动到的点位,由此动态生成一个Rectangle矩形框。这个矩形框就会跟随你鼠标移动变换大小,当鼠标左键弹起 (MouseLeftButtonUp事件)的时候,取消MouseLeftButtonDown事件的绑定。

      下面请大家看完整源代码:

MainPage.xaml.cs


      
      
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Net; 
  5. using System.Windows; 
  6. using System.Windows.Controls; 
  7. using System.Windows.Documents; 
  8. using System.Windows.Input; 
  9. using System.Windows.Media; 
  10. using System.Windows.Media.Animation; 
  11. using System.Windows.Shapes; 
  12.   
  13. namespace SLRectangle 
  14. { 
  15.     public partial class MainPage : UserControl 
  16.     { 
  17.         public MainPage() 
  18.         { 
  19.             InitializeComponent(); 
  20.         } 
  21.         private Rectangle rect; //声明一个矩形引用 
  22.         private Point origPoint; //设置一个鼠标点的引用 
  23.         private bool isAddMouseEvent = false; //标示是否添加了鼠标事件 
  24.   
  25.         /// <summary> 
  26.         /// 鼠标左键按下去的时候产生相应的矩形框控件,添加相应的移动控件事件和鼠标左键弹起事件 
  27.         /// </summary> 
  28.         /// <param name="sender"></param> 
  29.         /// <param name="e"></param> 
  30.         private void Handle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
  31.         { 
  32.             rect = new Rectangle(); 
  33.             origPoint = e.GetPosition(AddUC); 
  34.   
  35.             rect.SetValue(Canvas.LeftProperty, origPoint.X); //设置矩形的起始X坐标 
  36.             rect.SetValue(Canvas.TopProperty, origPoint.Y);  //设置矩形的起始Y坐标 
  37.             rect.Opacity = 1;          //设置本控件透明度 
  38.             rect.Fill = new SolidColorBrush(Colors.Blue); 
  39.             rect.RadiusX = 10;//为了让矩形美观一些设置了圆角属性 
  40.             rect.RadiusY = 10; 
  41.             AddUC.MouseMove += Handle_MouseMove; //为Canvas面板加载MouseMove事件 
  42.             AddUC.MouseLeftButtonUp += Handle_MouseLeftButtonUp;//为Canvas面板加载MouseLeftButtonUp事件 
  43.             AddUC.Children.Add(rect); //在Canvas面板中添加矩形 
  44.         } 
  45.   
  46.         /// <summary> 
  47.         /// 当鼠标左键绘制完成,弹起鼠标的时候移除本页面的鼠标事件绑定。 
  48.         /// </summary> 
  49.         /// <param name="sender"></param> 
  50.         /// <param name="e"></param> 
  51.         private void Handle_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) 
  52.         { 
  53.             AddUC.MouseLeftButtonUp -= Handle_MouseLeftButtonUp; 
  54.             AddUC.MouseMove -= Handle_MouseMove; 
  55.         } 
  56.         /// <summary> 
  57.         /// 当鼠标按下去移动的时候,矩形框控件也相应的改变大小以形成相应的框 
  58.         /// </summary> 
  59.         /// <param name="sender"></param> 
  60.         /// <param name="e"></param> 
  61.         private void Handle_MouseMove(object sender, MouseEventArgs e) 
  62.         { 
  63.             Point curPoint = e.GetPosition(AddUC); 
  64.             if (curPoint.X > origPoint.X) 
  65.             { 
  66.                 rect.Width = curPoint.X - origPoint.X; 
  67.             } 
  68.             if (curPoint.X < origPoint.X) 
  69.             { 
  70.                 rect.SetValue(Canvas.LeftProperty, curPoint.X); 
  71.                 rect.Width = origPoint.X - curPoint.X; 
  72.             } 
  73.             if (curPoint.Y > origPoint.Y) 
  74.             { 
  75.                 rect.Height = curPoint.Y - origPoint.Y; 
  76.             } 
  77.             if (curPoint.Y < origPoint.Y) 
  78.             { 
  79.                 rect.SetValue(Canvas.TopProperty, curPoint.Y); 
  80.                 rect.Height = origPoint.Y - curPoint.Y; 
  81.             } 
  82.   
  83.         } 
  84.   
  85.         private void button1_Click(object sender, RoutedEventArgs e) 
  86.         { 
  87.             //监测是否已经为鼠标绘制状态。如果不是那么则绑定为鼠标绘制状态,且设置此按钮为不可用状态 
  88.             if (isAddMouseEvent == false) 
  89.             { 
  90.                 this.AddUC.MouseLeftButtonDown += Handle_MouseLeftButtonDown; 
  91.                 isAddMouseEvent = true; 
  92.                 this.button1.IsEnabled = false; 
  93.   
  94.             } 
  95.         } 
  96.   
  97.     } 
  98. } 

MainPage.xaml


      
      
  1. <UserControl x:Class="SLRectangle.MainPage" 
  2.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  3.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  4.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  5.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  6.     mc:Ignorable="d" 
  7.     d:DesignHeight="1000" d:DesignWidth="1000" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"> 
  8.   
  9.     <Grid x:Name="LayoutRoot"  > 
  10.         <Canvas x:Name="AddUC"   HorizontalAlignment="Stretch"  Background="Black"   VerticalAlignment="Stretch" Width="1920" Height="1080"> 
  11.         </Canvas> 
  12.         <Button Width="97" Height="30" Content="点我开始绘制矩形" x:Name="button1" Margin="15,10,0,0"   VerticalAlignment="Top" HorizontalAlignment="Left" Click="button1_Click"></Button> 
  13.          
  14.     </Grid> 
  15. </UserControl> 

        在本实例中,所有需要注释的地方在文中都已经写好注释。这是一个很小的小技巧,可是在实际项目中还是有一些使用的地方。比如说。我们要让用户自定义的在某 一个有很多台机器设备的背景图片上自己设置很多机器的锚点,然后为这些锚点配置相关的机器信息。在这里我们就可以让用户自己在那些机器上绘制一些矩形。然 后当绘制完毕,关联好数据后,我们就可以将用户的这个自定义配置的界面以及该页面上矩形的位置和大小以XML的形式存入数据库中,下次从数据库中取出来, 然后还原即可呈现自定义好的节面。

        效果图如下:

        本实例为VS2010+Silverlight 4.0环境。由于时间有限,全新制作Demo,有不当之处请指教。

       点击请下载:

SLRectangle.rar