Silverlight 2动态创建矩形对象(附完整源代码)[转]

使用Silverlight 2的Canvas,写了一个动态创建Rectangle的示例,由于时间的原因所以难免有些不足之处,但程序功能都正常使用.用鼠标可以点击画布任何位置拖出一个矩形对象,松开鼠标即可完成一个矩形的创建!

程序运行效果:

 

XAML代码:

  1. <UserControl x:Class="Sample.dragrect"
  2.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  3.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  4.     Width="780" Height="400">
  5.     <StackPanel Background="Green" 
  6.                 Orientation="Horizontal">
  7.         <Canvas x:Name="LayoutRoot"
  8.                 Background="GreenYellow"
  9.                 Width="650" Height="400"
  10.                 MouseMove="Canvas_MouseMove"
  11.                 MouseLeftButtonDown="Canvas_MouseLeftButtonDown" 
  12.                 MouseLeftButtonUp="Canvas_MouseLeftButtonUp"/>
  13.         <StackPanel Background="Gold" Margin="10">
  14.             <TextBlock Text="选择颜色:"/>
  15.             <Button x:Name="btnRed" 
  16.                     Width="100" Height="50" 
  17.                     FontSize="20" Content="Red" Margin="5" 
  18.                     Click="btnRed_Click"/>
  19.             <Button x:Name="btnBlue" 
  20.                     Width="100" Height="50"
  21.                     FontSize="20" Content="Blue" Margin="5" 
  22.                     Click="btnBlue_Click"/>
  23.             <Button x:Name="btnGreen" 
  24.                     Width="100" Height="50"
  25.                     FontSize="20" Content="Green" Margin="5"
  26.                     Click="btnGreen_Click"/>
  27.             <Button x:Name="btnClear" 
  28.                     Width="100" Height="50"
  29.                     FontSize="20" Content="Clear" Margin="5" 
  30.                     Background="Red"
  31.                     Click="btnClear_Click"/>
  32.         </StackPanel>
  33.     </StackPanel>
  34. </UserControl>

C#代码:

  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. namespace Sample
  13. {
  14.     public partial class dragrect : UserControl
  15.     {
  16.         public dragrect()
  17.         {
  18.             InitializeComponent();
  19.         }
  20.         bool mouseMoveing = false;
  21.         Point mousePoint;
  22.         Color rectColor = Colors.Red;
  23.         private void Canvas_MouseMove(object sender, MouseEventArgs e)
  24.         {
  25.             //如果鼠标没有拖动矩形则返回
  26.             if (!mouseMoveing)
  27.                 return;
  28.             //获取鼠标当前坐标
  29.             Point curPos = e.GetPosition(null);
  30.             //取得最小坐标值
  31.             double posX = mousePoint.X;
  32.             double posY = mousePoint.Y;
  33.             //计算矩形的宽和高
  34.             double rectWidth = Math.Abs(curPos.X - mousePoint.X);
  35.             double rectHeight = Math.Abs(curPos.Y - mousePoint.Y);
  36.             //创建一个矩形元素
  37.             Rectangle rect = new Rectangle();
  38.             //声明矩形的宽和高
  39.             rect.Width = rectWidth;
  40.             rect.Height = rectHeight;
  41.             //填充颜色
  42.             rect.Fill = new SolidColorBrush(rectColor);
  43.             //声明矩形在Canvas中创建的位置
  44.             Canvas.SetLeft(rect, posX);
  45.             Canvas.SetTop(rect, posY);
  46.             //添加矩形到Canvas中
  47.             LayoutRoot.Children.Add(rect);
  48.         }
  49.         private void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  50.         {
  51.             //获取当前的鼠标位置
  52.             mousePoint = e.GetPosition(null);
  53.             //开始创建矩形
  54.             mouseMoveing = true;
  55.         }
  56.         private void Canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  57.         {
  58.             //矩形创建完成
  59.             mouseMoveing = false;
  60.         }
  61.         private void btnRed_Click(object sender, RoutedEventArgs e)
  62.         {
  63.             //声明矩形颜色为Red
  64.             rectColor = Colors.Red;
  65.         }
  66.         private void btnBlue_Click(object sender, RoutedEventArgs e)
  67.         {
  68.             //声明矩形颜色为Blue
  69.             rectColor = Colors.Blue;
  70.         }
  71.         private void btnGreen_Click(object sender, RoutedEventArgs e)
  72.         {
  73.             //声明矩形颜色为Green
  74.             rectColor = Colors.Green;
  75.         }
  76.         private void btnClear_Click(object sender, RoutedEventArgs e)
  77.         {
  78.             //清除所有Canvas内的矩形元素
  79.             LayoutRoot.Children.Clear();
  80.         }
  81.     }
  82. }
posted on 2008-09-16 10:55  帅死活该  阅读(421)  评论(0编辑  收藏  举报