【问题标题】:Deleting drawn rectangle zoom box after zooming in放大后删除绘制的矩形缩放框
【发布时间】:2016-02-07 16:38:51
【问题描述】:

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

我已经进行了缩放并绘制了矩形,但是我不能 1) 弄清楚如何使其透明,以及 2) 弄清楚如何删除矩形放大后。一旦向下单击鼠标以在放大的图像上绘制另一个缩放框(我正在绘制分形),它就会再次被删除,但我不知道在放大后要写什么来删除它。

绘画

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();
                }

【问题讨论】:

    标签: c# zooming mouse


    【解决方案1】:

    一开始我以为你需要这个:

    1. 这是非常罕见的情况之一,您的绘图应该Paint 事件中完成,但在MouseMove 中使用Graphics 对象完成使用 CreateGraphics 创建。

    这是正确的方式这里的原因是,对于这种交互式橡皮筋绘图,您确实希望绘图坚持。其他示例是行预览光标交叉

    1. 要使Rectangle 透明,您可以

      • 使用DrawRectangle
      • 或使用半透明颜色和FillRectangle
      • 或两者都使用,如下例所示:

    这是您需要的代码:

    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);
        }
    }
    

    这个函数可以让你开始绘制任何角落,而不仅仅是左上角:

    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,上述代码将无法正常工作。

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

    在这种情况下,我们回到正常的方式并在Paint 中绘制东西,但只要按下鼠标按钮即可。由于这里没有鼠标参数,我们需要另一个类级别 Point 并使用 Control.MouseButtons 属性:

    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);
        }
    
    }
    

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

    【讨论】:

    • 感谢 TaW,非常棒的回复。谢谢!但是我的 IDE 不同意 Rectangle rect = rectangle(mDown, mCurrent); :( 'rectangle' 带有下划线,它表示它在当前上下文中不存在,但我认为它是在该行中声明的!
    • 它是之前版本的rectangle 函数,一种Rectangle(Point, Point) 构造函数。您还需要 MouseDownPoint mDown .. - 请注意 c# 区分大小写,因此 rectangle != Rectangle !!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-29
    • 2019-07-24
    • 2012-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多