为什么ControlPaint.DrawReversibleFrame似乎是在鼠标位置的偏移处绘制的 [英] why does ControlPaint.DrawReversibleFrame seem to be drawn at an offset to the mouse location

查看:351
本文介绍了为什么ControlPaint.DrawReversibleFrame似乎是在鼠标位置的偏移处绘制的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个大型应用程序中开始遇到此问题,并检查我复制了标准的microsoft示例,如下所示。但是这个例子仍然显示了在偏移处(靠近左上角)绘制的可逆框架,我确定这曾经工作正常,是什么时候发生的?



I started having this problem in a large application and to check i copied the standard microsoft example, part shown below. But this example still shows the reversible frame drawn at an offset (closer to the upper left hand corner), i'm sure this used to work ok, whats going on?

private void Form1_MouseMove(object sender,
          System.Windows.Forms.MouseEventArgs e)
      {

          // If the mouse is being dragged,
          // undraw and redraw the rectangle as the mouse moves.
          if (isDrag)

          // Hide the previous rectangle by calling the
          // DrawReversibleFrame method with the same parameters.
          {
              ControlPaint.DrawReversibleFrame(theRectangle,
                  this.BackColor, FrameStyle.Dashed);

              // Calculate the endpoint and dimensions for the new
              // rectangle, again using the PointToScreen method.
              Point endPoint = ((Control)sender).PointToScreen(new Point(e.X, e.Y));

              int width = endPoint.X - startPoint.X;
              int height = endPoint.Y - startPoint.Y;
              theRectangle = new Rectangle(startPoint.X,
                  startPoint.Y, width, height);

              // Draw the new rectangle by calling DrawReversibleFrame
              // again.
              ControlPaint.DrawReversibleFrame(theRectangle,
                  this.BackColor, FrameStyle.Dashed);
          }
      }

推荐答案

它适用于我:我粘贴你的代码,并处理了一些事件:

It works fine for me: I pasted your code in, and handled a few events:
bool isDrag = false;
Rectangle theRectangle;
Point startPoint;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
    isDrag = true;
    startPoint = ((Control)sender).PointToScreen(new Point(e.X, e.Y));
    theRectangle = new Rectangle(startPoint.X, startPoint.Y, 0, 0);
    }

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
    // If the mouse is being dragged,
    // undraw and redraw the rectangle as the mouse moves.
    if (isDrag)

    // Hide the previous rectangle by calling the
    // DrawReversibleFrame method with the same parameters.
        {
        ControlPaint.DrawReversibleFrame(theRectangle,
            this.BackColor, FrameStyle.Dashed);

        // Calculate the endpoint and dimensions for the new
        // rectangle, again using the PointToScreen method.
        Point endPoint = ((Control)sender).PointToScreen(new Point(e.X, e.Y));

        int width = endPoint.X - startPoint.X;
        int height = endPoint.Y - startPoint.Y;
        theRectangle = new Rectangle(startPoint.X,
            startPoint.Y, width, height);

        // Draw the new rectangle by calling DrawReversibleFrame
        // again.
        ControlPaint.DrawReversibleFrame(theRectangle,
            this.BackColor, FrameStyle.Dashed);
        }
    }

private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
    {
    isDrag = false;
    }



开始和结束角落正是我期望的地方。

你还记得中的PointToScreen吗? MouseDown处理程序?


The start and end corners are exactly where I expect.
Did you remember the PointToScreen in the MouseDown handler?


这篇关于为什么ControlPaint.DrawReversibleFrame似乎是在鼠标位置的偏移处绘制的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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