Click here to Skip to main content
15,172,251 members
Articles / Desktop Programming / Windows Forms
Article
Posted 9 Aug 2007

Stats

49.7K views
596 downloads
12 bookmarked

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
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)

Share

About the Author

magedo93
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 3:02
MemberMori_hatam24-Jun-10 3:02 
Questionmouse up Pin
OpenSessame18-Oct-09 21:45
MemberOpenSessame18-Oct-09 21:45 
GeneralOne method alternative Pin
cipher_nemo20-Apr-09 11:14
Membercipher_nemo20-Apr-09 11:14 
GeneralRe: One method alternative Pin
Mori_hatam24-Jun-10 3:21
MemberMori_hatam24-Jun-10 3:21 
GeneralMy vote of 1 Pin
cipher_nemo20-Apr-09 11:07
Membercipher_nemo20-Apr-09 11:07 
General@_@ Pin
sawo.9-Oct-07 11:40
Membersawo.9-Oct-07 11:40 
GeneralRe: @_@ Pin
cipher_nemo12-Mar-10 7:25
Membercipher_nemo12-Mar-10 7:25 
QuestionHmmmmmm???? Pin
Martin#13-Aug-07 20:49
MemberMartin#13-Aug-07 20:49 
Hello,

I have to say that I was expecting a little more from this subject!

First of all, this is very often answered in the forums.
I hoped to read an outstanding article for this topic, with an clever idea behind or at least the standard solution very well explaint.

But what you presented is the standard solution with a lot of errors inside:
You overrided OnPaint, but deaktivated the call of the base OnPaint.
Very bad idea, I guess!
protected override void OnPaint(PaintEventArgs pe)

{

// TODO: Add custom paint code here


// Calling the base class OnPaint

// base.OnPaint(pe)

}


This limit's your control to be placed on an Form, and will throw an exception if you don't do so!
fr = (System.Windows.Forms.Form)this.Parent;


And why do you need the Control at all?
You could inherit your own "MovableForm" from Windows.Forms.Form and override the Mouse methods there.

P.S.: I haven't written an article myself yet, therefor I appriciate you taking time to do so!
Therfore a 2 from me instead of 1


All the best,

Martin

AnswerRe: Hmmmmmm???? Pin
magedo9318-Aug-07 23:56
Membermagedo9318-Aug-07 23:56 
Generalsame issue, other solution Pin
Guido_d10-Aug-07 1:00
MemberGuido_d10-Aug-07 1: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.