Select a rectangular area by dragging it

I am creating an application view of an image viewer. I am on Windows and using .Net

In my application, I am trying to select a specific area while dragging. I have created a rectangle.

Rectangle areaRect = new Rectangle(100,100, 300, 300);
Point ptOld = new Point(0, 0);
Pen rectPen = new Pen(Brushes.White, 3);

protected override void OnPaint(PaintEventArgs e)
{
  Graphics dcPaint = e.Graphics;
  dcPaint.DrawRectangle(rectPen, areaRect);
}

      

Now I drag this rectangular area along with my mouse movements.

protected override void OnMouseMove(MouseEventArgs e)
{
 Point ptNew = new Point(e.X, e.Y);
 int dx = ptNew.X - ptOld.X;
 int dy = ptNew.Y - ptOld.Y;
 areaRect.Offset(dx, dy);
 MoveRect(ptNew);
 ptOld = ptNew;
}

      

Here I am trying to move this rectangle with the mouse

void MoveRect(Point point)
{
 Graphics grfxClient = CreateGraphics();
 Rectangle tempRectangle = new Rectangle(areaRect.Left, areaRect.Top, areaRect.Width,   areaRect.Height);
 grfxClient.DrawRectangle(rectPen, tempRectangle);
 this.Invalidate();
 grfxClient.Dispose();
}

      

My code still works fine. Now I would like to darken the INVERSE drag area (the area that is outside the drag area), I mean that the area that is inside this rectangle should be selected when dragging.

Any idea how to proceed.

Thank.

-Pankaj

+3


source to share


1 answer


I suppose you can do this by creating an object Region

that covers the outside of the rectangle and fills it with a semi-transparent SolidBrush

one to make it appear dark.

You also don't need to create graphics and draw on the OnMouseMove

event, but just slide the rectangle and invalidate the surface of the control you are drawing on.

The code I am using looks something like this:

Rectangle areaRect = new Rectangle(100,100, 300, 300);
Point ptOld = new Point(0, 0);
Pen rectPen = new Pen(Brushes.White, 3);

//A new field with a semi-transparent brush to paint the outside of the rectangle
Brush dimmingBrush = new SolidBrush(Color.FromArgb(128, 0, 0, 0));

protected override void OnPaint(PaintEventArgs e)
{
    Region outsideRegion = new System.Drawing.Region(e.ClipRectangle);
    outsideRegion.Exclude(areaRect);
    Graphics dcPaint = e.Graphics;
    dcPaint.FillRegion(dimmingBrush, outsideRegion);
    dcPaint.DrawRectangle(rectPen, areaRect);
}

protected override void OnMouseMove(MouseEventArgs e)
{
    Point ptNew = new Point(e.X, e.Y);
    int dx = ptNew.X - ptOld.X;
    int dy = ptNew.Y - ptOld.Y;
    areaRect.Offset(dx, dy);
    ptOld = ptNew;
    this.Invalidate();
}

      

A named method is MoveRect

not needed.

Now it works the way you wanted it to.



suggestions

I also have some suggestions. You do not need to use them, they may be useful to you.

You haven't specified which control you are using to draw (or override methods Form

and draw directly on it), but I suggest you use the control PictureBox

, create your own control derived from it, and override its events. This should make the painting process even and prevent flickering. For this:

  • Create a new user control by choosing Add and User Control ... and name the new control ie. MyPictureBox

  • change the parent class of the control so it should now contain the line:

    public partial class MyPictureBox : PictureBox
    
          

  • open the file MyPictureBox.Designer.cs and comment out these lines:

    //this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    //this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    
          

  • copy the code I posted in this answer and add the line base.OnPaint(e);

    and start of the methodOnPaint

  • compile the project

  • now you can open the designer of your main form, drag the control MyPictureBox

    from the toolbar and use it without additional code

You might also want to consider changing the behavior of the selection so the mouse cursor was in the center of it. I suppose it would be more intuitive for the user.

If you have any problems with the code, just post it in the comments and I'll try to help :).

+4


source







All Articles