Rune Bune’s Blog

Games, Gadgets, Code & Tech

Archive for the ‘.NET’ Category

.NET Code

C# tip: move forms without clicking on the top bar

Posted by runebune on April 24, 2009

This is a way to move a form without clicking on the top bar.
This code snippet shows how a form could look like:


private bool _mouseDownClick;

public bool MouseDownClick
{
get { return _mouseDownClick; }
set { _mouseDownClick = value; }
}

private Point _mouseOffset;

public Point MouseOffset
{
get { return _mouseOffset; }
set { _mouseOffset = value; }
}

public DropBoxForm()
{
InitializeComponent();
}

private void DropBoxForm_MouseMove(object sender, MouseEventArgs e)
{
if (this.MouseDownClick)
{
Point mousePos = Control.MousePosition;
mousePos.Offset(this.MouseOffset.X, this.MouseOffset.Y);
this.Location = mousePos;
}
}

private void DropBoxForm_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.MouseDownClick = false;
}
}

private void DropBoxForm_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.MouseOffset = new Point(-e.X, -e.Y);
this.MouseDownClick = true;
}
}

Posted in .NET, Code, Tips & Tricks | Tagged: | Leave a Comment »