Click here to Skip to main content
Licence CPOL
First Posted 9 Aug 2007
Views 26,887
Downloads 295
Bookmarked 11 times

Enable Moving Form With mouse move using custom control

By | 9 Aug 2007 | Article
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)

About the Author

magedo93

Web Developer

Egypt Egypt

Member

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#


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralTANX PinmemberMori_hatam2:02 24 Jun '10  
Questionmouse up PinmemberMember 408304020:45 18 Oct '09  
GeneralOne method alternative PinmemberMember 457220610:14 20 Apr '09  
GeneralRe: One method alternative PinmemberMori_hatam2:21 24 Jun '10  
GeneralMy vote of 1 PinmemberMember 457220610:07 20 Apr '09  
General@_@ Pinmembersawo.10:40 9 Oct '07  
GeneralRe: @_@ Pinmembercipher_nemo6:25 12 Mar '10  
QuestionHmmmmmm???? PinmemberMartin#19:49 13 Aug '07  
AnswerRe: Hmmmmmm???? Pinmembermagedo9322:56 18 Aug '07  
Generalsame issue, other solution PinmemberGuido_d0:00 10 Aug '07  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 10 Aug 2007
Article Copyright 2007 by magedo93
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid