Silverlight 2動態建立矩形對象(附完整原始碼)[轉]
使用Silverlight 2的Canvas,寫了一個動態建立Rectangle的樣本,由於時間的原因所以難免有些不足之處,但程式功能都正常使用.用滑鼠可以點擊畫布任何位置拖出一個矩形對象,鬆開滑鼠即可完成一個矩形的建立!
程式運行效果:
XAML代碼:
- <UserControl x:Class="Sample.dragrect"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- Width="780" Height="400">
- <StackPanel Background="Green"
- Orientation="Horizontal">
- <Canvas x:Name="LayoutRoot"
- Background="GreenYellow"
- Width="650" Height="400"
- MouseMove="Canvas_MouseMove"
- MouseLeftButtonDown="Canvas_MouseLeftButtonDown"
- MouseLeftButtonUp="Canvas_MouseLeftButtonUp"/>
- <StackPanel Background="Gold" Margin="10">
- <TextBlock Text="選擇顏色:"/>
- <Button x:Name="btnRed"
- Width="100" Height="50"
- FontSize="20" Content="Red" Margin="5"
- Click="btnRed_Click"/>
- <Button x:Name="btnBlue"
- Width="100" Height="50"
- FontSize="20" Content="Blue" Margin="5"
- Click="btnBlue_Click"/>
- <Button x:Name="btnGreen"
- Width="100" Height="50"
- FontSize="20" Content="Green" Margin="5"
- Click="btnGreen_Click"/>
- <Button x:Name="btnClear"
- Width="100" Height="50"
- FontSize="20" Content="Clear" Margin="5"
- Background="Red"
- Click="btnClear_Click"/>
- </StackPanel>
- </StackPanel>
- </UserControl>
C#代碼:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Animation;
- using System.Windows.Shapes;
- namespace Sample
- {
- public partial class dragrect : UserControl
- {
- public dragrect()
- {
- InitializeComponent();
- }
- bool mouseMoveing = false;
- Point mousePoint;
- Color rectColor = Colors.Red;
- private void Canvas_MouseMove(object sender, MouseEventArgs e)
- {
- //如果滑鼠沒有拖動矩形則返回
- if (!mouseMoveing)
- return;
- //擷取滑鼠當前座標
- Point curPos = e.GetPosition(null);
- //取得最小座標值
- double posX = mousePoint.X;
- double posY = mousePoint.Y;
- //計算矩形的寬和高
- double rectWidth = Math.Abs(curPos.X - mousePoint.X);
- double rectHeight = Math.Abs(curPos.Y - mousePoint.Y);
- //建立一個矩形元素
- Rectangle rect = new Rectangle();
- //聲明矩形的寬和高
- rect.Width = rectWidth;
- rect.Height = rectHeight;
- //填充顏色
- rect.Fill = new SolidColorBrush(rectColor);
- //聲明矩形在Canvas中建立的位置
- Canvas.SetLeft(rect, posX);
- Canvas.SetTop(rect, posY);
- //添加矩形到Canvas中
- LayoutRoot.Children.Add(rect);
- }
- private void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
- {
- //擷取當前的滑鼠位置
- mousePoint = e.GetPosition(null);
- //開始建立矩形
- mouseMoveing = true;
- }
- private void Canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
- {
- //矩形建立完成
- mouseMoveing = false;
- }
- private void btnRed_Click(object sender, RoutedEventArgs e)
- {
- //聲明矩形顏色為Red
- rectColor = Colors.Red;
- }
- private void btnBlue_Click(object sender, RoutedEventArgs e)
- {
- //聲明矩形顏色為Blue
- rectColor = Colors.Blue;
- }
- private void btnGreen_Click(object sender, RoutedEventArgs e)
- {
- //聲明矩形顏色為Green
- rectColor = Colors.Green;
- }
- private void btnClear_Click(object sender, RoutedEventArgs e)
- {
- //清除所有Canvas內的矩形元素
- LayoutRoot.Children.Clear();
- }
- }
- }