c# – 使用MouseMove事件在画布内移动动态绘制的矩形

c# – 使用MouseMove事件在画布内移动动态绘制的矩形,第1张

概述我正在尝试在画布内移动一个动态绘制矩形.我能够在画布中动态绘制矩形,同时尝试在画布内移动矩形我遇到问题 XAML: <Grid x:Name="Gridimage1" Margin="0,0,411,100"> <Image Name="image1" HorizontalAlignment="Left" Stretch="Fill" VerticalAlignment="Top"> 我正在尝试在画布内移动一个动态绘制的矩形.我能够在画布中动态绘制矩形,同时尝试在画布内移动矩形我遇到问题

XAML:

<GrID x:name="GrIDimage1" margin="0,411,100">      <Image name="image1" HorizontalAlignment="left" Stretch="Fill" VerticalAlignment="top"></Image>        <Canvas x:name="BackPanel" margin="20,67,0" Height="317" WIDth="331">           <Rectangle x:name="selectionRectangle" stroke="lightBlue" Fill="#220000FF"/>        </Canvas>   </GrID>

C# :

动态绘制矩形后,我正在添加以下鼠标事件.

selectionRectangle.MouseleftbuttonDown += new MousebuttonEventHandler(Rect1_MouseDown);selectionRectangle.MouseMove += new MouseEventHandler(Rectangle_MouseMove_1);selectionRectangle.MouseUp += new MousebuttonEventHandler(Rect1_MouseUp);      # region "rectangle move"    private bool drag = false;    private Point startPt;    private int wID;    private int hei;    private Point lastLoc;    private double Canvasleft,Canvastop;    private voID Rect1_MouseDown(object sender,MousebuttonEventArgs e)    {        drag = true;        Cursor = Cursors.Hand;        startPt = e.Getposition(BackPanel);        wID = (int)selectionRectangle.WIDth;        hei = (int)selectionRectangle.Height;        lastLoc = new Point(Canvas.Getleft(selectionRectangle),Canvas.Gettop(selectionRectangle));       Mouse.Capture((IinputElement)sender);    }    private voID Rectangle_MouseMove_1(object sender,MouseEventArgs e)    {        try        {            if (drag)            {                    var newX = (startPt.X + (e.Getposition(BackPanel).X - startPt.X));                    var newY = (startPt.Y + (e.Getposition(BackPanel).Y - startPt.Y));                    Point offset = new Point((startPt.X - lastLoc.X),(startPt.Y - lastLoc.Y));                    Canvastop = newY - offset.Y;                    Canvasleft = newX - offset.X;                    selectionRectangle.SetValue(Canvas.topProperty,Canvastop);                    selectionRectangle.SetValue(Canvas.leftProperty,Canvasleft);                           }        }        catch (Exception ex)        {            MessageBox.Show(ex.Message);            }    }    private voID Rect1_MouseUp(object sender,MousebuttonEventArgs e)    {        drag = false;        Cursor = Cursors.Arrow;        Mouse.Capture(null);    }    #endregion

问题:我可以在整个窗口移动矩形.我只想在画布边缘内移动矩形.

我可以在画布外移动矩形

解决方法 在提交拖动 *** 作之前,您应该能够获取selectionRectangle的边界并查看它们是否超出画布的宽度和/或高度.

selectionRectangle.MouseMove += new MouseEventHandler(Rectangle_MouseMove_1);private bool drag = false;private Point startPt;private int wID;private int hei;private Point lastLoc;private double Canvasleft,Canvastop;private voID Rectangle_MouseMove_1(object sender,MouseEventArgs e){    try    {        if (drag)        {                var newX = (startPt.X + (e.Getposition(BackPanel).X - startPt.X));                var newY = (startPt.Y + (e.Getposition(BackPanel).Y - startPt.Y));                Point offset = new Point((startPt.X - lastLoc.X),(startPt.Y - lastLoc.Y));                Canvastop = newY - offset.Y;                Canvasleft = newX - offset.X;                // check if the drag will pull the rectangle outsIDe of it's host canvas before performing                // Todo: protect against lower limits too...               if ((Canvastop + selectionRectangle.Height > BackPanel.Height) || (Canvasleft + selectionRectangle.WIDth > BackPanel.WIDth) || Canvastop < 0 || Canvasleft < 0)                    {                        return;                    }                selectionRectangle.SetValue(Canvas.topProperty,Canvastop);                selectionRectangle.SetValue(Canvas.leftProperty,Canvasleft);                       }    }    catch (Exception ex)    {        MessageBox.Show(ex.Message);        }}
总结

以上是内存溢出为你收集整理的c# – 使用MouseMove事件在画布内移动动态绘制的矩形全部内容,希望文章能够帮你解决c# – 使用MouseMove事件在画布内移动动态绘制的矩形所遇到的程序开发问题。

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

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

原文地址:https://www.54852.com/langs/1219010.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存