Thanks to visit codestin.com
Credit goes to www.codeproject.com

65.9K
CodeProject is changing. Read more.
Home

Enable Moving Form With mouse move using custom control

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.33/5 (6 votes)

Aug 10, 2007

CPOL

2 min read

viewsIcon

52521

downloadIcon

611

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;

}

}