Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?

C#
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);
          }
      }
Posted

1 solution

It works fine for me: I pasted your code in, and handled a few events:
C#
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;
    }

The start and end corners are exactly where I expect.
Did you remember the PointToScreen in the MouseDown handler?
 
Share this answer
 
Comments
Member 7918752 11-Nov-15 12:27pm    
Yes, i've tried the microsoft demo code exactly to be 100% sure. What seems to happen is the offset gets bigger the further away i start from the top left corner. A bit like there is a scaling factor being used somewhere but there isn't one in the code. I'm about to try on a different computer as i can only think its something to do with screen resolution or the way my laptop handles the screen, i know it shouldn't be but... Weird, i've had this code working for several years and nothing has changed to stop it working other than my laptop...
Member 7918752 11-Nov-15 13:27pm    
Well turned out to be screen resolution! It works fine at lower screen resolution but at the highest (Recommended!!) screen resolution it does not work! Somehow, somewhere, the location is being multiplied by some ratio of the supposed screen resolution and what it perhaps thinks is the actual screen resolution. Very weird and raises the possibility that it wont work on everyones computer!!
OriginalGriff 11-Nov-15 13:35pm    
Check your video driver: I'm running win 10 with nVidea drivers at the recommended resolution on both my monitors (which have different resolutions) and it's fine.
Member 7918752 11-Nov-15 14:38pm    
Seems i have AMD Firepro M400 and the driver seems to be upto date, i'm using windows 8.1.

Not much i can do about it as its company issue. I'll try tomorrow to see if it's ok with an external monitor. The only other alternative would be program something independently, the form coordinates seem fine its only the actual screen coordinates that are wrong.
Member 7918752 12-Nov-15 5:25am    
Yep, works ok with an external monitor... doesn't work with the laptop screen.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900