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

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