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(); 
        } 
    } 
}
 
本文转自dotfun 51CTO博客,原文链接:http://blog.51cto.com/dotfun/285833
 
相关文章
Silverlight自定义数据绑定控件应该如何处理IEditableObject和IEditableCollectionView对象
原文:Silverlight自定义数据绑定控件应该如何处理IEditableObject和IEditableCollectionView对象 原创文章,如需转载,请注明出处。   最近在一直研究Silverlight下的数据绑定控件,发现有这样两个接口IEditableObject 和IEditableCollectionView,记录一下结论,欢迎交流指正。
872 0
Silverlight & Blend动画设计系列六:动画技巧(Animation Techniques)之对象与路径转化、波感特效
原文:Silverlight & Blend动画设计系列六:动画技巧(Animation Techniques)之对象与路径转化、波感特效   当我们在进行Silverlight & Blend进行动画设计的过程中,可能需要设计出很多效果不一的图形图像出来作为动画的基本组成元素。
1053 0

热门文章

最新文章

下一篇
DataWorks