Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Making a Borderless Form Movable in C++

0.00/5 (No votes)
15 Mar 2012 1  
How to make a borderless form movable. Now in C++!

Introduction

This is an article on a moveable borderless form. This can be really useful when it comes to making a custom GUI. I went looking for this code in C++ and couldn't find it anywhere. So I had to figure it out myself. And to make it easier for you guys, I'm supplying the code here for you, in C++. I'm 13, so if I make no sense, blame it on my age. (Also, I have an F in English :P)

Make and Code!

When you first create a Windows Forms project, it will give you a blank window. The first step to creating a borderless form is to give it no borders. To do this, right-click on the form and select "Properties". On "Form Border Style", select "None".

Now look back at the form and add a couple controls. You can add whatever you'd like, but I suggest you add a close button to the form first. Add a button and label it "X" and put it in the top right corner of the form. You can also add a label for a window name.

To make the close button actually close the form, double click on the button and it will make an event automatically! Inside the brackets, put this code:

//Close the form
Close();

Next (what we've been waiting for!), we're going to make the form movable!

The first step is to add these in the class Form1 (make sure it's before the control properties):

private: bool dragging;
private: Point offset;

None of the following code will work unless you have these! You'll find out why we are using these later.

To make the form moveable, we need to create four events. The first one is Form_Load. To create this event, double click on the form.

Now let's make sure the form isn't going to be dragging when we open it up! Add this code in the brackets:

//Make sure it isn't moving when we open the form.
this->dragging = false;

Go back to the designer, and right-click on the form and select "Properties" again. In the Properties menu, click on the lightning bolt on the top. This will show you all the events of the form. Scroll down to the mouse events.

Here, you want to create an event for all three, MouseDown, MouseMove, and MouseUp. Double click on one to make an event.

When the MouseDown event is made, you'll need to tell the form it's going to be moving soon. So add this code in the brackets:

//tell the form its gonna be draggin'
this->dragging = true;
this->offset = Point(e->X, e->Y);

Now that it knows it is going to be moving, we need to tell it to get the mouse's current position and move the form to that point.

if (this->dragging){ //Move, soldier, MOVE!
    Point currentScreenPos = PointToScreen(e->Location);
    Location = Point(currentScreenPos.X - this->offset.X, 
                     currentScreenPos.Y - this->offset.Y);
}

Now it is going to move to the current position of the cursor. Remember the bool "dragging"? That's what we used in the code above. It says the left click button is down and it's ready to roll!

So now, we need to make it stop dragging when you release the left-click. This event is called MouseUp. Go ahead and create the MouseUp event. Inside the MouseUp event, we need to tell the form that it needs to stop moving. So put this code inside the brackets:

this->dragging = false; //this bool is awesome

Congrats! You're finished coding! Your final code (all the events) should look like this:

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
    Close();
}

private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
    this->dragging = false;
}

private: System::Void Form1_MouseDown(System::Object^  sender, System::Windows::Forms::MouseEventArgs^  e) {
    this->dragging = true;
    this->offset = Point(e->X, e->Y);
}

private: System::Void Form1_MouseMove(System::Object^  sender, System::Windows::Forms::MouseEventArgs^  e) {
    if (this->dragging){ 
        Point currentScreenPos = PointToScreen(e->Location);
        Location = Point(currentScreenPos.X - this->offset.X, currentScreenPos.Y - this->offset.Y);
    }
}

private: System::Void Form1_MouseUp(System::Object^  sender, System::Windows::Forms::MouseEventArgs^  e) {
    this->dragging = false; //this bool is awesome
}

Now that we're finished coding, debug the program and drag it 'round your screen.

Hopefully you found this helpful.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here