放大

时间:2015-11-06 13:51:25

标签: c# zoom mouse

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

我有缩放工作和绘制矩形,但我不能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();
                }

1 个答案:

答案 0 :(得分:1)

起初我认为你需要这个:

  1. 这是非常罕见的情况之一,您的绘图应该在<{1}}事件中但在Paint中使用使用MouseMove创建的Graphics对象。
  2. 这是正确的方法这里的原因是,对于这种交互式橡皮筋绘图,你做希望绘图<强>持续即可。其他示例包括线预览光标交叉

    1. 要使CreateGraphics透明,您可以

      • 使用Rectangle
      • 或使用半透明颜色和DrawRectangle
      • 或使用以下示例中的两者:
    2. enter image description here

      以下是您需要的代码:

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

      请注意,如果你有一个BackgroundImage,那么上面的代码将无法正常运行。

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

      在这种情况下,我们回到正常的方式并在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); } 中绘制内容,但只要按下鼠标按钮即可。由于我们这里没有鼠标参数,因此我们需要另一个类级Paint并使用Point属性:

      Control.MouseButtons

      enter image description here

      总而言之:除了相当多的细节之外,你的主要问题是没有在paint事件中检查鼠标按钮。