silverlight 实现在Canvas中拖放控件

silverlight 实现在Canvas中拖放控件,第1张

概述1)前台代码 <UserControl x:Class="SilverlightApplication139.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xm

1)前台代码

<UserControl x:Class="SilverlightApplication139.MainPage"    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    xmlns:d="http://schemas.microsoft.com/Expression/blend/2008"    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"    mc:Ignorable="d"    d:DesignHeight="300" d:DesignWIDth="400">    <Canvas x:name="LayoutRoot" Background="White">    </Canvas></UserControl>

2)后台代码

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 SilverlightApplication139{    public partial class MainPage : UserControl    {        bool trackingMousemove = false;        Point mousePoint = new Point(0,0);        public MainPage()        {            InitializeComponent();            Loaded += new RoutedEventHandler(MainPage_Loaded);        }        voID MainPage_Loaded(object sender,RoutedEventArgs e)        {            for (int i = 0; i < 5; i++)            {                Rectangle rec = new Rectangle();                rec.name = i.ToString();                rec.Fill = new SolIDcolorBrush(colors.Red);                rec.WIDth = 50;                rec.Height = 50;                rec.SetValue(Canvas.leftProperty,0D);                rec.SetValue(Canvas.topProperty,i * (rec.WIDth + 5));                rec.MouseleftbuttonDown += new MousebuttonEventHandler(Element_MouseleftbuttonDown);                rec.MouseMove += new MouseEventHandler(Element_MouseMove);                rec.MouseleftbuttonUp += new MousebuttonEventHandler(Element_MouseleftbuttonUp);                LayoutRoot.Children.Add(rec);            }        }        /// <summary>        /// 鼠标按下控件        /// </summary>        private voID Element_MouseleftbuttonDown(object sender,MousebuttonEventArgs e)        {            FrameworkElement element = sender as FrameworkElement;            mousePoint = e.Getposition(null);            trackingMousemove = true;            element.CaptureMouse();            element.Cursor = Cursors.Hand;        }        /// <summary>        /// 鼠标移动控件        /// </summary>        private voID Element_MouseMove(object sender,MouseEventArgs e)        {            FrameworkElement element = sender as FrameworkElement;            if (trackingMousemove)            {                double deltaV = e.Getposition(null).Y - mousePoint.Y;                double deltaH = e.Getposition(null).X - mousePoint.X;                double newtop = deltaV + (double)element.GetValue(Canvas.topProperty);                double newleft = deltaH + (double)element.GetValue(Canvas.leftProperty);                element.SetValue(Canvas.topProperty,newtop);                element.SetValue(Canvas.leftProperty,newleft);                mousePoint = e.Getposition(null);            }        }        /// <summary>        /// 鼠标放下控件        /// </summary>        private voID Element_MouseleftbuttonUp(object sender,MousebuttonEventArgs e)        {            FrameworkElement element = sender as FrameworkElement;            trackingMousemove = false;            element.ReleaseMouseCapture();            element.Cursor = null;        }    }}
总结

以上是内存溢出为你收集整理的silverlight 实现在Canvas中拖放控件全部内容,希望文章能够帮你解决silverlight 实现在Canvas中拖放控件所遇到的程序开发问题。

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

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1022703.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存