Silverlight 2動態建立矩形對象(附完整原始碼)

來源:互聯網
上載者:User
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. }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.