C#   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了c# – 使用MouseMove事件在画布内移动动态绘制的矩形大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试在画布内移动一个动态绘制的矩形.我能够在画布中动态绘制矩形,同时尝试在画布内移动矩形我遇到问题

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事件在画布内移动动态绘制的矩形所遇到的程序开发问题。

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

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