Drag operations outside the title bar of a form

Source: Internet
Author: User
Tags bool xdiff
Drag operations outside the title bar of a form

(Author: Zhang Junhong) 2004-9-3

We know that the drag on the form simply points to the title bar, drag the mouse on it. But sometimes we want to do this by dragging the form outside the title bar of the form. At this point we need to write some code, the following is my practice for everyone to visit.

Create a new form FORM1 and put two RadioButton controls, the first is to determine whether the form is dragged, and the third is to determine whether a range is specified for form dragging.

The following is the form code:

Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Windows.Forms;
Using System.IO;



Namespace WindowsApplication1
{
Partial class Form1:form
{
Point _startxy; The position where the mouse is pressed
BOOL _movedown=false; Is the mouse pressed down
Specify an area for writing in OnPaint.
Rectangle _rec = new Rectangle (50, 50, 70, 70);
Public Form1 ()
{
InitializeComponent ();
}

protected override void OnMouseDown (MouseEventArgs e)
{
Do you want to move? By pressing the mouse (OnMouseDown) ok ha
BOOL Oktomove=false;
Base. OnMouseDown (e);

if (this.radioButton1.Checked)
{
Oktomove=true;
}
Else
{
If you press the mouse's point coordinates in the area specified by the _rec
Then we agree to move the form to the Operation ha.
if (This._rec. Contains (this. PointToClient (mouseposition))
{
Oktomove = true;

}
}
If you agree to move, give _startxy the coordinates of the mouse moment.
and agreed to press the mouse to move.
if (oktomove==true)
{
Assigns the current mouse position to the variable
_startxy = mouseposition;
_movedown = true;
}
}

protected override void OnMouseMove (MouseEventArgs e)
{
Base. OnMouseMove (e);
If you agree to move with the mouse, move the amount to the variable
So that the form moves at your discretion.
if (_movedown)
{
int Xdiff,ydiff;
Xdiff = mouseposition.x-_startxy.x;
Ydiff = MOUSEPOSITION.Y-_startxy.y;
Change the position of a form
This. left = this. Left + Xdiff;
This. top = this. Top + Ydiff;
Give the new position to the _STARTXY variable, huh?
_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 ha
SolidBrush B = new SolidBrush (color.red);
if (radiobutton2.checked)
{
Fill the _rec area, the pen color is red ha
E.graphics.fillrectangle (b, _rec);
Change the color of a pen
B.color = Color.White;
Redefine an area, which is actually the region of the _REC variable.
RectangleF RECF = new RectangleF (_rec. X
_rec. Y
_rec. Width,
_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 position of the form with the area, set the background to the form
Color, lest affect the vision.
B.color = BackColor;
Paint this area.
E.graphics.fillrectangle (b, _rec);
}
B.dispose ()//Lose the pen, lest take up my place
}


private void Radiobutton2_checkedchanged (object sender, EventArgs e)
{
This. Invalidate (THIS._REC);//Make the place specified by _rec invalid.
}

}
}





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.