放大后删除绘制的矩形缩放框 [英] Deleting drawn rectangle zoom box after zooming in

查看:118
本文介绍了放大后删除绘制的矩形缩放框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个透明的可拖动矩形缩放框,一旦鼠标再次向上移动,它就会放大该区域并删除绘制的矩形.

I'm trying to code a draggable rectangle zoombox that's transparent, once the mouse is up again, it zooms into that area and deletes the drawn rectangle.

我已经完成缩放工作并绘制了矩形,但是我不能1)弄清楚如何使其透明,以及2)弄清楚如何删除矩形放大后.鼠标单击鼠标在放大的图像上绘制另一个zoombox时,它将再次被删除(我正在绘制一个分形),但是我不知道要写些什么来放大后将其删除.

I've got the zoom working and drawing the rectangle, however I can't 1) Figure out how to make it transparent, and 2) Figure out how to delete the rectangle after it's zoomed in. It gets deleted again once the mouse is clicked down to draw another zoombox on the zoomed in image (I'm drawing a fractal) but I can't figure out what to write to get it to delete after zooming in.

绘画

private void Form1_Paint(object sender, PaintEventArgs e)
        {                
            Graphics windowG = e.Graphics;
            windowG.DrawImageUnscaled(picture, 0, 0);
            if (rectangle == true)
            {
               e.Graphics.FillRectangle(Brushes.Aquamarine, rec);
            }
            if (rectangle == false)
            {
                Invalidate();
            }


        }

鼠标下移

                rectangle = true;
                if (e.Button == MouseButtons.Left)
                {
                    rec = new Rectangle(e.X, e.Y, 0, 0);
                    Invalidate();
                }

鼠标上移

{
    rectangle = false;
}

鼠标移动

if (e.Button == MouseButtons.Left)
                {
                    rec.Width = e.X - rec.X;
                    rec.Height = e.Y - rec.Y;
                    Invalidate();
                }

推荐答案

起初,我认为您需要这样做:

At first I thought you need this:

  1. 这是非常稀有情况之一,在这种情况下,您的图形不应该在Paint事件中而是在MouseMove中使用Graphics对象进行完成用CreateGraphics创建.
  1. This is one of the very rare cases where your drawing should not be done in the Paint event but in the MouseMove using a Graphics object created with CreateGraphics.

此处是正确方法的原因是,对于这种交互式橡皮筋工程图,您不希望工程图坚持.其他示例是行预览光标十字线.

The reason why this is the right way here is that for this kind of interactive rubberband drawing you do not want the drawing to persist. Other examples would be a line preview or a cursor cross.

  1. 要使Rectangle透明,您可以

  • 使用DrawRectangle
  • 或使用半透明颜色和FillRectangle
  • 或在下面的示例中同时使用两者:
  • Use DrawRectangle
  • or use a semi-transparent color and FillRectangle
  • or use both as in the example below:

这是您需要的代码:

Point mDown = Point.Empty;

private void Form1_MouseDown(object sender, MouseEventArgs e)
{
    mDown = e.Location;  // note the first corner
}

private void Form1_MouseUp(object sender, MouseEventArgs e)
{
    Invalidate();   // clear the rectangle
}

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
    if (e.Button == System.Windows.Forms.MouseButtons.Left)
    using (Graphics G = this.CreateGraphics() ) // !!! usually wrong !!!
    {
        G.Clear(BackColor); // Invalidate();
        Rectangle rect = rectangle(mDown, e.Location);
        // either
        using (SolidBrush brush = new SolidBrush(Color.FromArgb(32, 255, 0, 0)))
            G.FillRectangle(brush, rect);
        // or
        G.DrawRectangle(Pens.Red, rect);
    }
}

此功能可让您开始绘制任何角,而不仅仅是左上角:

This is a function that lets you start drawing any any corner, not just top left:

Rectangle rectangle (Point p1, Point p2)
{
    int x = Math.Min(p1.X, p2.X);
    int y = Math.Min(p1.Y, p2.Y);
    int w = Math.Abs(p1.X - p2.X);
    int h = Math.Abs(p1.Y - p2.Y);
    return new Rectangle(x, y, w, h);
}

请注意,如果您有BackgroundImage,则上面的代码将无法正常工作.

但是现在我认为这更接近您的情况:

But now I think this is closer to your situation:

在这种情况下,我们返回正常方式并在Paint中绘制内容,但前提是只要按下鼠标按钮即可.由于这里没有鼠标参数,因此我们需要另一个类级别Point并使用Control.MouseButtons属性:

In this case we go back to the normal way and draw things in the Paint but only as long as the mouse button is pressed. As we don't have the mouse parameters here we need another class level Point and also use the Control.MouseButtons property:

Point mCurrent = Point.Empty;

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
    mCurrent = e.Location;
    Invalidate();
}

private void Form1_MouseUp(object sender, MouseEventArgs e)
{
    mDown = Point.Empty;
    mCurrent = Point.Empty;
    Invalidate();
}

private void Form1_Paint(object sender, PaintEventArgs e)
{
    if (Control.MouseButtons == System.Windows.Forms.MouseButtons.Left)
    {
        Rectangle rect = rectangle(mDown, mCurrent);
        // either one..
        using (SolidBrush brush = new SolidBrush(Color.FromArgb(32, 255, 0, 0)))
            e.Graphics.FillRectangle(brush, rect);
        // ..or both of these
        e.Graphics.DrawRectangle(Pens.Red, rect);
    }

}

因此,总结一下:除了很多细节外,您的主要问题是在paint事件中缺少对鼠标按钮的检查..

So to sum it up: Besides quite a few details your main problem is missing the check for the mouse button in the paint event..

这篇关于放大后删除绘制的矩形缩放框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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