我如何使矩形的第一个位置成为鼠标光标位置,终点将成为我离开鼠标左键的位置? [英] How do i make that the rectangle first location will be the mouse cursor location and end point will be where i leave the mouse left button?

查看:25
本文介绍了我如何使矩形的第一个位置成为鼠标光标位置,终点将成为我离开鼠标左键的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码:

private void pictureBoxSnap_Paint(object sender, PaintEventArgs e)
        {
            if (pictureBoxSnap.Image != null)
            {
                if (btn == true)
                {
                    if (Rect != null && Rect.Width > 0 && Rect.Height > 0)
                    {
                        e.Graphics.DrawRectangle(pen, Rect);
                    }
                }
            }
        }

        private void pictureBoxSnap_MouseMove(object sender, MouseEventArgs e)
        {
            if (btn == true)
            {
            if (e.Button == MouseButtons.Left)
                return;
            Point tempEndPoint = e.Location;
            Rect.Location = new Point(
                Math.Min(RectStartPoint.X, tempEndPoint.X),
                Math.Min(RectStartPoint.Y, tempEndPoint.Y));
            Rect.Size = new Size(
                Math.Abs(RectStartPoint.X - tempEndPoint.X),
                Math.Abs(RectStartPoint.Y - tempEndPoint.Y));

                pictureBoxSnap.Invalidate();
            }
        }

        private void pictureBoxSnap_MouseDown(object sender, MouseEventArgs e)
        {
            btn = !btn;
        }

我想做的是两件事:

  1. 当我第一次单击鼠标左键时,RectStartPoint XY 应该是鼠标光标所在的位置.然后我移动鼠标,矩形变得更大或更小,然后当我再次单击鼠标左键时,将矩形保持在最后一个鼠标光标所在的大小.我该怎么办?现在它总是从 0,0 开始,RectStartPoint 将是 0,0,如果我将其更改为 e.Location.XY 而不是 RectStartPoint 它不会绘制任何东西.

  1. When i click first time the mouse left button the RectStartPoint X and Y should be where the mouse cursor is now. Then i move the mouse the rectangle get bigger or smaller and then when i click again on the mouse left button leave the rectangle as it on the size where last the mouse cursor was. How can i od it ? Now it all the time start at 0,0 the RectStartPoint will be 0,0 and if i change it to e.Location.X and Y instead RectStartPoint it won't draw anything.

第二件事是当我第一次单击鼠标左键并更改矩形大小时,如果我继续移动鼠标并同时再次单击鼠标左键,矩形将消失/删除.如果我不移动鼠标只左键单击按钮矩形将保留.为什么我在删除矩形的同时移动鼠标并单击左键?

Second thing is when i click first time the left mouse button and change the rectangle size if i will keep move the mouse and at the same time click again on the left mouse button the rectangle will gone/delete. If i will not move the mouse only left click button the rectangle will stay. Why if i move the mouse and click left button at the same time the rectangle is deleted ?

推荐答案

我不太确定你想从你的问题中得到什么.下面的代码执行经典的橡皮筋操作:它绘制一个活动矩形并在您单击而不移动时将其删除.

I'm not really sure what you want from your question. The code below does the classic rubberbanding: It paints a live rectangle and deletes it when you click without moving.

我把计算放在一个灵活的函数中.

I put the calculation in a flexible function.

bool btn = false;
Point RectStartPoint = Point.Empty;
Point RectEndPoint = Point.Empty;

private void pictureBoxSnap_Paint(object sender, PaintEventArgs e)
{
    if (pictureBoxSnap.Image != null)
    {
        {
            Rectangle Rect = getRect(RectStartPoint, RectEndPoint); ;
            if (Rect != Rectangle.Empty)
            {
                e.Graphics.DrawRectangle(Pens.Firebrick, Rect);
            }
        }
    }
}

private void pictureBoxSnap_MouseMove(object sender, MouseEventArgs e)
{
    if (btn == true)
    {
        RectEndPoint = e.Location;
        pictureBoxSnap.Invalidate();
    }
}

private void pictureBoxSnap_MouseDown(object sender, MouseEventArgs e)
{
    RectStartPoint = e.Location;
    btn = true;
}

private void pictureBoxSnap_MouseUp(object sender, MouseEventArgs e)
{
    btn = false;
    RectEndPoint = e.Location;
    pictureBoxSnap.Invalidate();
}

Rectangle getRect(Point p1, Point p2)
{
    Point p = new Point(Math.Min(p1.X, p2.X),Math.Min(p1.Y, p2.Y) );
    Size s = new Size(Math.Abs(p1.X - p2.X), Math.Abs(p1.Y - p2.Y));
    return new Rectangle(p, s);
}

注意 1:PictureBox 控件已经是双缓冲的,所以无论你画得多快或多大,它都不会闪烁.

Note 1: The PictureBox control is already doublebuffered, so it doesn't flicker, no matter how fast or big you draw.

注意 2:您可能需要更彻底地检查鼠标按钮是否会涉及到右侧(或中间)按钮.以下是正确"检查的示例:

Note 2: You might need to do a more thoruogh mouse button check if the right (or middle) buttons could get involved. Here is an example for the 'proper' check:

if (e.Button & MouseButtons.Left != MouseButtons.None)  ..

这篇关于我如何使矩形的第一个位置成为鼠标光标位置,终点将成为我离开鼠标左键的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆