Recently, you need to make a rectangle, and drag the rectangle with the left mouse button to move its position. I looked it up on the internet and they did it quite complicated. I study one day, did a relatively simple, published for your reference. If you feel simple, you can pass by, thank you. Ha ha.
First of all about the reason, GDI draw a rectangular box is not said, very simple. The main thing here is that the wheel zooms in and the left key moves two events, to calculate the coordinate position of the rectangle box. The following code is posted as follows:
First define the required variables, and then four variables.
Rectangle Frame coordinates
Private Rectangle DrawRect = new Rectangle (0, 0, 100, 100);
Mouse down time coordinates
Private point MOUSEDOWNP = new Point ();
Magnification
private int zoom = 0;
The number of times the mouse moves the rectangle, if you move the rectangle box is not enlarged from the center, the position after the move to zoom out, reduced to the original size, the scale number is 0 o'clock, reset this number
private int movecount = 0;
Then write mouse events
private void This_mousewheel (object sender, MouseEventArgs e)
{
if (E.delta > 0)
{
if (Zoom < 80)//maximum magnification 80 times times
zoom++;
}
Else
{
if (Zoom > 0)
zoom--;
if (zoom = = 0)//magnification = 0, do not enlarge, drag the mouse tag to 0
Movecount = 0;
}
Amplification principle
1. Get the magnification factor first
2, according to magnification, calculate the height of the rectangle box, height = control height-multiples x2;
3, according to the height, the rectangle width is extracted, width = height x (control width/control height), to ensure that the rectangle and the original control aspect ratio, so multiply the scale
4, according to the size of the rectangle, and the size of the control, calculate the position of the rectangle in the control, that is, x, y coordinates
1) If the rectangle is not dragged, the default middle position is taken, x= (control width-rectangle width)/2,y= (control height-rectangle height)/2
2) If you drag the rectangle box, the original x, Y coordinates do not change
5, according to the new rectangular frame of the coordinates and range, judging whether the bounds, to determine the XY coordinate
Wheel Zoom Event
Rectangular area height = control height-magnification reduction
Drawrect.height = this. Height-zoom * 2;
Calculate width by scale
Drawrect.width = (int) (Convert.tosingle (drawrect.height) * (Convert.tosingle (this). Width)/Convert.tosingle (this. Height));
if (Movecount = = 0)//has not been dragged, the rolling wheel is zoomed in by the middle.
{
Drawrect.x = (this. Width-drawrect.width)/2;
Drawrect.y = (this. Height-drawrect.height)/2;
}
=============== judge whether the =============== is over-bounded
If right exceeds the width of the control
if (Drawrect.right > this. Width)
{
Drawrect.x = Drawrect.x-(drawrect.right-this. Width)-1;
Drawrect.x = drawrect.x > 1? Drawrect.x:1;
}
If the bottom exceeds the height of the control
if (Drawrect.bottom > this. Height)
{
Drawrect.y = Drawrect.y-(drawrect.bottom-this. Height)-1;
Drawrect.y = drawrect.y > 1? Drawrect.y:1;
}
Drawimg ();
}
private void This_mousemove (object sender, MouseEventArgs e)
{
if (E.button = = MouseButtons.Left)
{
if (Drawrect.contains (MOUSEDOWNP))//Determine if the mouse pressed coordinates are in the red box, determine whether to drag the red box
{
Drag the mouse position, rectangle new x= Rectangle box Original x+ (current mouse x Press X), the original x+ offset
The Y axis changes as well
Drawrect.x = Drawrect.x + (e.x-mousedownp.x); //. location = CLIENP;
Drawrect.y = Drawrect.y + (E.Y-MOUSEDOWNP.Y); CLIENP.Y;
Determine if the upper left corner is exceeded
if (Drawrect.x < 0)
drawrect.x = 0;
if (Drawrect.y < 0)
Drawrect.y = 0;
Determine if the lower right corner is exceeded
if (Drawrect.x > (this. WIDTH-DRAWRECT.WIDTH-1))
Drawrect.x = this. Width-drawrect.width-1;
if (Drawrect.y > (this. HEIGHT-DRAWRECT.HEIGHT-1))
Drawrect.y = this. height-drawrect.height-1;
Drawing
Drawimg ();
After calculating the coordinate system, the mouse coordinates are converted to the current mouse coordinates to recalculate the offset
mousedownp.x = e.x;
MOUSEDOWNP.Y = e.y;
Drag over the mouse, drag the mouse tag accumulation
movecount++;
}
}
}
private void This_mousedown (object sender, MouseEventArgs e)
{
if (E.button = = MouseButtons.Left)
MOUSEDOWNP = new Point (e.x, e.y);
}
void Drawimg ()
{
using (Graphics g = this. CreateGraphics ())
{
Redraw background
G.fillrectangle (New SolidBrush (Color.lightgray), 0, 0, this. Width, this. Height);
g.DrawString ("Wheel zoom, left drag rectangle box" + Zoom, New Font ("Blackbody", 12f), New SolidBrush (Color.green), 5, 5);
g.DrawString ("Zoom in" + Zoom, New Font ("Blackbody", 12f), New SolidBrush (color.red), 5, 20);
Redraw Rectangle
G.drawrectangle (New Pen (color.red), drawrect);
}
}
This is mainly a few events. There is a need to refer to the reference.
C # GDI Draw rectangle, drag the left mouse button to move the rectangle box, the wheel zoom to narrow the rectangle box