Sunday, January 3, 2010

Moving a form with no border

After you have changed the FormBorderStyle to None, there is a question standing against you: "How to move such a kind of window?"
The solution is easy as follows:

bool dragging;
Point offset;

private void Form1_MouseDown(object sender, MouseEventArgs e)
{
dragging = true;
offset = e.Location;

}

private void Form1_MouseUp(object sender, MouseEventArgs e)
{
dragging = false;
}

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (dragging)
{
Point currentScreenPos = PointToScreen(e.Location);
Location = new
Point (currentScreenPos.X - offset.X,
currentScreenPos.Y - offset.Y);

}

}

2 comments: