Click here to Skip to main content
15,887,267 members
Articles / Programming Languages / C#

Movable Freeform/Round Edged Window/Form in C#

Rate me:
Please Sign up or sign in to vote.
4.10/5 (22 votes)
11 Mar 2009GPL31 min read 47.7K   5   35   14
This application shows how to code a movable freeform window in C#, which does not have a title bar or maximize / minimize button.

Introduction

It is a bit tricky in Visual Studio to create a movable free form window (irrespective of the language you choose). Here I am going to show you how easy it is.

Step 1

Create a form in C# and add one exit button too.

Step 2

Change the properties of the form like this. Change the Form Border Style as none and Transparence key to ‘control’.

Step 3

Add a picture box and a picture to the form.

Step 4

Now the final and the tricky part comes into the picture. Create the following mouse events for the newly created form.

C#
public int diff_x;
public int diff_y;
public bool mouse_down = false;

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
    diff_x = Form.MousePosition.X - Form.ActiveForm.Location.X;
    diff_y = Form.MousePosition.Y - Form.ActiveForm.Location.Y;
    mouse_down = true;
}

In the picture box’s mouse down event, we find out the difference in mouse positions and set a public variable mouse_down to true.

C#
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
    if (mouse_down == true)
    {
        Point p = new Point(MousePosition.X - diff_x, MousePosition.Y - diff_y);
        Form.ActiveForm.Location = p;
    }
}

In the mouse move event of the picture box, find out the position and set the active window to it. But we need to check whether the mouse is actually down or not. Only if the mouse is down, we need to look further for mouse movements.

C#
private void pictureBox1_MouseUp(object sender, MouseEventArgs e) 
{
    mouse_down = false;
}

And here is the code for mouse up event. We are just changing the global variable mouse_down to false.

Add the following code to your exit button. Otherwise, you will face difficulty in closing your application.

C#
private void button1_Click(object sender, EventArgs e)
{
    Dispose();
    this.Close();
}

Finally here is our movable transparent window in action!

History

  • 11th March, 2009: Initial post

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Software Developer IBM
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Wendell D H15-May-14 9:51
professionalWendell D H15-May-14 9:51 
Questionthx Pin
mersahin27-Oct-11 12:04
mersahin27-Oct-11 12:04 
GeneralCool ...... Pin
alrsds8-Sep-09 20:57
alrsds8-Sep-09 20:57 
GeneralMake it easier Pin
madooo1220-Jun-09 2:49
madooo1220-Jun-09 2:49 
GeneralVery interesting... Pin
TBear SoftWare17-Mar-09 0:10
TBear SoftWare17-Mar-09 0:10 
GeneralRe: Very interesting... Pin
jimsweb17-Mar-09 1:11
jimsweb17-Mar-09 1:11 
Generalhmm Pin
johannesnestler12-Mar-09 5:26
johannesnestler12-Mar-09 5:26 
GeneralRe: hmm Pin
jimsweb12-Mar-09 18:00
jimsweb12-Mar-09 18:00 
General've seen this for too many times... Pin
Seishin#11-Mar-09 9:54
Seishin#11-Mar-09 9:54 
GeneralRe: 've seen this for too many times... Pin
gillardg12-Mar-09 0:52
gillardg12-Mar-09 0:52 
GeneralRe: 've seen this for too many times... Pin
jimsweb12-Mar-09 3:48
jimsweb12-Mar-09 3:48 
GeneralRe: 've seen this for too many times... Pin
jimsweb12-Mar-09 3:47
jimsweb12-Mar-09 3:47 
GeneralRe: 've seen this for too many times... Pin
Seishin#12-Mar-09 3:49
Seishin#12-Mar-09 3:49 
GeneralRe: 've seen this for too many times... Pin
jimsweb12-Mar-09 3:51
jimsweb12-Mar-09 3:51 

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.