Drag operation outside the form header bar
(Author: Zhang Jun-hong) 2004-9-3
We know that the drag of the form only needs to swear the title bar, drag the mouse. But I want to implement the operation of dragging the form outside the form of the form of the form. At this time, we need us. Self-written code, here is my approach for everyone to visit.
Create a new form FORM1 and put two Radiobutton controls, the first is to determine if the form is dragged, and the third is to determine if a certain area is specified for forming a form to drag.
The following is the form code:
Using system; usneric; using system.data; using system.drawing; using system.windows.forms; using system.io;
Namespace WindowsApplication1 {Partial Class Form1: form {point _Startxy; // Mouse Pressing the position bool _movedown = false; // The mouse is not prescribed // Specify a zone, good writing in onpaint rectangle _rec = New Rectangle (50 , 50, 70, 70); public form1 () {initializationComponent ();
PROTECTED OVERRIDE VOID ONMOUSEDOWN (MouseEventArgs E) {// Want to move? By confirming the mouse (onMousedown), BOOL OKTOMOVE = false; base.onmousedown (e); if (this.radiobutton1.checked) {OkTomove = true;} else {// If point coordinates in the mouse are specified in _rec The area // then we agree to move the form to operate. IF (this.pointtoclient)) {oktomove = true;
}}} // If you agree to move, press the mouse a moment of coordinates to _startxy // and agreed to move by mouse. IF (OkTomove == True) {// The current mouse position is assigned to the variable _Startxy = mouseposition; _MOVedown = true;}}
Protected Override Void OnMouseMove (MouseEventargs E) {base.onmousemove (e); // If you agree to move the mouse, the amount of movement is given to the variable // so that the form is moved according to your amount. IF (_MOVEDOWN) {Int xdiff, ydiff; xdiff = mouseposition.x - _startxy.x; ydiff = mouseposition.y - _Startxy.y; // Change Form position this.left = this.Left xdiff; this.top = THIS.TOP YDIFF; // Give the new position to _startxy variables _Startxy = mouseposition;}}} protected override void onmouseup (mouseEventArgs e) {base.onmouseup (e); _MOVEDOWN = false;
}
Protected Override Void onPaint (Painteventargs E) {base.onpaint (e); // To a new brush, to Net To Hao Solidbrush B = New Solidbrush (Color.red); if (Radiobutton2.Checked) {// Pluggled_ REC's area, the color of the pen is red. E.Graphics.FillRectangle (b, _rec); // change the color B.Color = color.White; // Redefine a zone, this area is actually _rec variable Region Rectanglef Recf = New Rectanglef (_Rec.x, _rec.height); // Write a few words in this area E.Graphics.drawstring ("Click On Me Drag", FONT, B, RECF);} else {// If you do not agree to change the form position, set the background as a form of //, which is from affecting the visual. B.Color = backcolor; // Put this area E.Graphics.FillRectangle (B, _REC);} b.dispose (); // Put the pen, lest, from} Private void radiobutton2_checkedchanged (Object Sender, Eventargs e) {this.invalidate (this._rec); // 使 使}}}}}
Ok, try it!!