素材巴巴 > 程序开发 >

C#拖拽控件

程序开发 2023-09-04 06:55:12

    写了这么些年程序,界面写的较少。实习被逼补了下。趁有空总结下。

    模仿着写了一个简单的拖拽相应命令的Demo。当鼠标点击屏幕时,生成一个红色的UI(这里用Rectangle表示),当点击Rectangle时,变成绿色,可以拖拽它。当松开它时,就会变成黄色。

在XAML里,我让它相应一个鼠标左键按下的命令。

 

 

 

看具体的代码:

using System.Windows;

usingSystem.Windows.Controls;

using System.Windows.Input;

using System.Windows.Media;

usingSystem.Windows.Shapes;

 

namespaceclickAndDragMouseEvents

{

   public partialclass MainPage: UserControl

   {

       //Judge if the mouse is dragging some circles.

       private boolisDragging = false;

       //Store the offset between the click point andthe center of the ellipse

       private PointmouseOffset = new Point();

 

       public MainPage()

       {

            InitializeComponent();

       }

 

    //当有鼠标左键点击的Event发出时,处理这个Event,生成一个UIControl。这里选择一个矩形当做UI。

 private voidmyCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)

       {

            if(isDragging == false)

            {

                CanvasmyCanvs = (Canvas)sender;

 

                Rectanglerect = new Rectangle();

                rect.Fill = new SolidColorBrush(Colors.Red);

                rect.Height = 50;

                rect.Width = 50;

 

                PointclickPoint = e.GetPosition(this);

                rect.SetValue(Canvas.LeftProperty, clickPoint.X - rect.Width /2);

                rect.SetValue(Canvas.TopProperty, clickPoint.Y - rect.Height /2);

 

                rect.MouseLeftButtonDown +=Ellipse_MouseLeftButtonDown;

 

                myCanvas.Children.Add(rect);

            }

       }

 

 

    //当该UI Rectangle被选中(点击)时,做相应的处理。比如通过强制转换sender来获取被点击的UI;修改它的颜色(或者别的你需要的操作);使用+=操作符来订阅鼠标移动,鼠标左键点击等事件;

private void Ellipse_MouseLeftButtonDown(object sender, MouseButtonEventArgse)

       {

            if(isDragging == false)

            {

                isDragging = true;

 

                Rectangleellipse = (Rectangle)sender;//看是哪个UI被选中了。

 

                ellipse.Fill = new SolidColorBrush(Colors.Green);

 

                ellipse.MouseMove += Ellipse_MouseMoving;//订阅鼠标移动消息

 

                ellipse.MouseLeftButtonUp +=Ellipse_MouseLeftButtonUp;

 

                mouseOffset =e.GetPosition(ellipse);

 

                ellipse.CaptureMouse();//如果没有CaptureMouse(),那么当鼠标移动过快离开了该UI范围后,该UI就相应不到鼠标的事件了。

            }

       }

 

      //随着鼠标的移动来移动UI的位置。

       private voidEllipse_MouseMoving(object sender, MouseEventArgs e)

       {

            if(isDragging == true)

            {

                Rectangleellipse = (Rectangle)sender;

                ellipse.SetValue(Canvas.LeftProperty, e.GetPosition(this).X - mouseOffset.X);

                ellipse.SetValue(Canvas.TopProperty, e.GetPosition(this).Y - mouseOffset.Y);

            }

       }

 

//当鼠标左键松开了,做相应的处里,比如让UI不再相应鼠标事件了。

 private voidEllipse_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)

       {

            if(isDragging == true)

            {

                isDragging = false;

                Rectangleellipse = (Rectangle)sender;

                ellipse.SetValue(Canvas.LeftProperty, e.GetPosition(this).X - mouseOffset.X);

                ellipse.SetValue(Canvas.TopProperty, e.GetPosition(this).Y - mouseOffset.Y);

                ellipse.Fill = new SolidColorBrush(Colors.Orange);

 

                ellipse.ReleaseMouseCapture();

            }

       }

   }

}

 

 


标签:

上一篇: 一文总结 Android屏幕适配 下一篇:
素材巴巴 Copyright © 2013-2021 http://www.sucaibaba.com/. Some Rights Reserved. 备案号:备案中。