Click here to Skip to main content
15,867,939 members
Articles / Desktop Programming / Windows Forms
Article

Enable Moving Form With mouse move using custom control

Rate me:
Please Sign up or sign in to vote.
2.33/5 (6 votes)
9 Aug 2007CPOL2 min read 51.7K   609   12   10
Enablehandleing Form move With mouse move using custom control in case form border style is none

Introduction

I build this control to enables me to moving this form when I make the form border style = none

Background

the problem we faces is when we need to build none rectangle form . to do this we but picture on form and make its border style = none

in this case we need to handle how we can change form postion on screen using mouse .

i solve this by build custom control adding this future to the form when we but the control on it

Using the code

At first to enable moving the form with mouse moving we need to handle 3 events first one is when mouse button is pressed (Parent_MouseDown) second when mouse moving + mouse button is pressed (Parent_MouseMove )last one when releasing mouse button (Parent_MouseUp)


Sample control code is :
public partial class HandelFormMove : Control

{

private Point mouseOffset;

private bool isMouseDown = false;

System.Windows.Forms.Form fr;

public HandelFormMove()

{

InitializeComponent();

}

protected override void OnPaint(PaintEventArgs pe)

{

// TODO: Add custom paint code here

// Calling the base class OnPaint

// base.OnPaint(pe);

}

/// <summary>

/// Initialize Form Moving by initialize the from postion

/// And Set The Flage (isMouseDown) = true

/// to Enable Moving Form With Mouse Moving

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

private void Parent_MouseDown(object sender, MouseEventArgs e)

{

int xOffset;

int yOffset;

// MessageBox.Show("click");

if (e.Button == MouseButtons.Left)

{

// Assign coordinates to mouseOffset variable based on

// current position of the mouse pointer.

xOffset = -e.X - SystemInformation.FrameBorderSize.Width;

yOffset = -e.Y - SystemInformation.CaptionHeight -

SystemInformation.FrameBorderSize.Height;

mouseOffset = new Point(xOffset, yOffset);

isMouseDown = true;

}

}

/// <summary>

/// check the flage (isMouseDown) if it is true

/// change form postion acording to mouse postion

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

private void Parent_MouseMove(object sender, MouseEventArgs e)

{

// If the left mouse is held down.

if (isMouseDown)

{

// Set the form's location property to the new position.

Point mousePos = Control.MousePosition;

mousePos.Offset(mouseOffset.X, mouseOffset.Y);

fr.Location = mousePos;

}

}

/// <summary>

/// finalize Form Moving

/// by Seting The Flage (isMouseDown) = false

/// to disable Moving Form With Mouse Moving

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

private void Parent_MouseUp(object sender, MouseEventArgs e)

{

// Changes the isMouseDown field so that the form does

// not move unless the user is pressing the left mouse button.

if (e.Button == MouseButtons.Left)

{

isMouseDown = false;

}

}

/// <summary>

///

/// </summary>

protected override void OnCreateControl()

{

//base.OnCreateControl();

fr = (System.Windows.Forms.Form)this.Parent;

fr.MouseDown += new MouseEventHandler(Parent_MouseDown);

fr.MouseMove += new MouseEventHandler(Parent_MouseMove);

fr.MouseUp += new MouseEventHandler(Parent_MouseUp);

this.Visible = false;

}

}


License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Egypt Egypt
AHMED AHMED MOHAMED ABO EL-MAGD :
Team Leader For 3D game (1’s Person View) The Last Great Pharouh

Senior Instructor
Senior Developer .NET C#

Comments and Discussions

 
GeneralTANX Pin
Mori_hatam24-Jun-10 2:02
Mori_hatam24-Jun-10 2:02 
Questionmouse up Pin
OpenSessame18-Oct-09 20:45
OpenSessame18-Oct-09 20:45 
What if the user alt-tabs away from the form while holding the mouse button down? The form may not receive a mouse up click

Fast, Cheap, Good: choose any 2
- anonymous

GeneralOne method alternative Pin
cipher_nemo20-Apr-09 10:14
cipher_nemo20-Apr-09 10:14 
GeneralRe: One method alternative Pin
Mori_hatam24-Jun-10 2:21
Mori_hatam24-Jun-10 2:21 
GeneralMy vote of 1 Pin
cipher_nemo20-Apr-09 10:07
cipher_nemo20-Apr-09 10:07 
General@_@ Pin
sawo.9-Oct-07 10:40
sawo.9-Oct-07 10:40 
GeneralRe: @_@ Pin
cipher_nemo12-Mar-10 6:25
cipher_nemo12-Mar-10 6:25 
QuestionHmmmmmm???? Pin
Martin#13-Aug-07 19:49
Martin#13-Aug-07 19:49 
AnswerRe: Hmmmmmm???? Pin
magedo9318-Aug-07 22:56
magedo9318-Aug-07 22:56 
Generalsame issue, other solution Pin
Guido_d10-Aug-07 0:00
Guido_d10-Aug-07 0:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.