silverlight   发布时间:2022-05-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Silverlight 2动态创建矩形对象(附完整源代码)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

使用Silverlight 2的Canvas,写了一个动态创建Rectangle的示例,由于时间的原因所以难免有些不足之处,但程序功能都正常使用.用鼠标可以点击画布任何位置拖出一个矩形对象,松开鼠标即可完成一个矩形的创建! 程序运行效果: XAML代码: <UserControl x:Class="Sample.dragrect"     xmlns=" http://scheR_614_11845@as.micros
使用Silverlight 2的Canvas,写了一个动态创建Rectangle的示例,由于时间的原因所以难免有些不足之处,但程序功能都正常使用.用鼠标可以点击画布任何位置拖出一个矩形对象,松开鼠标即可完成一个矩形的创建!
程序运行效果:

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

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"
                Mou@R_450_10288@ftButtonDown="Canvas_Mou@R_450_10288@ftButtonDown" 
                Mou@R_450_10288@ftButtonUp="Canvas_Mou@R_450_10288@ftButtonUp"/>
        <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_Mou@R_450_10288@ftButtonDown(object sender,MouseButtonEventArgs E)
        {
            //获取当前的鼠标位置
            mousePoint = e.GetPosition(null);
            //开始创建矩形
            mouseMoveing = true;
        }
        private void Canvas_Mou@R_450_10288@ftButtonUp(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();         }     } }  

大佬总结

以上是大佬教程为你收集整理的Silverlight 2动态创建矩形对象(附完整源代码)全部内容,希望文章能够帮你解决Silverlight 2动态创建矩形对象(附完整源代码)所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。