Click here to Skip to main content
15,867,686 members
Articles / Programming Languages / C++

Making a Borderless Form Movable in C++

Rate me:
Please Sign up or sign in to vote.
4.76/5 (11 votes)
15 Mar 2012CPOL3 min read 63.9K   887   12   24
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".

Image 1

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.

Image 2

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:

C++
//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):

C++
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:

C++
//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.

Image 3

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:

C++
//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.

C++
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:

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

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

C++
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Student L&N STEM Academy
United States United States
Code sucks, but its rewarding so do it.

Comments and Discussions

 
PraiseVery Nice ❤️ Pin
hamza mostakim16-Dec-20 23:30
hamza mostakim16-Dec-20 23:30 
Suggestion[My vote of 1] Useless hack to solve a problem that already has a clean solution. Pin
.:floyd:.4-Mar-14 1:18
.:floyd:.4-Mar-14 1:18 
QuestionHow I implemented it for an MFC dialog Pin
lkitross5-Jan-14 20:54
lkitross5-Jan-14 20:54 
AnswerRe: How I implemented it for an MFC dialog Pin
.:floyd:.4-Mar-14 1:24
.:floyd:.4-Mar-14 1:24 
GeneralMy vote of 5 Pin
NemoWang10-Jan-13 18:11
NemoWang10-Jan-13 18:11 
QuestionThat is funny Pin
Peter Hawke13-Feb-12 13:39
Peter Hawke13-Feb-12 13:39 
AnswerRe: That is funny Pin
Preston Gull13-Feb-12 17:37
Preston Gull13-Feb-12 17:37 
QuestionForm Move Pin
geoyar13-Feb-12 10:47
professionalgeoyar13-Feb-12 10:47 
GeneralMy vote of 2 Pin
MemberHeck13-Feb-12 5:34
MemberHeck13-Feb-12 5:34 
Is this C or CLR
GeneralRe: My vote of 2 Pin
Preston Gull13-Feb-12 9:27
Preston Gull13-Feb-12 9:27 
QuestionVery funny C++ Pin
Leonid Shikhmatov13-Feb-12 5:31
Leonid Shikhmatov13-Feb-12 5:31 
AnswerRe: Very funny C++ Pin
brik0013-Feb-12 7:25
brik0013-Feb-12 7:25 
AnswerRe: Very funny C++ Pin
Preston Gull13-Feb-12 9:29
Preston Gull13-Feb-12 9:29 
AnswerRe: Very funny C++ Pin
Daniele Rota Nodari16-Mar-12 4:50
Daniele Rota Nodari16-Mar-12 4:50 
GeneralRe: Very funny C++ Pin
.:floyd:.4-Mar-14 3:37
.:floyd:.4-Mar-14 3:37 
AnswerRe: Very funny C++ Pin
Daniele Rota Nodari4-Mar-14 5:02
Daniele Rota Nodari4-Mar-14 5:02 
GeneralRe: Very funny C++ Pin
.:floyd:.4-Mar-14 6:27
.:floyd:.4-Mar-14 6:27 
AnswerRe: Very funny C++ Pin
Daniele Rota Nodari4-Mar-14 9:03
Daniele Rota Nodari4-Mar-14 9:03 
GeneralRe: Very funny C++ Pin
.:floyd:.4-Mar-14 9:59
.:floyd:.4-Mar-14 9:59 
AnswerRe: Very funny C++ Pin
Daniele Rota Nodari4-Mar-14 10:37
Daniele Rota Nodari4-Mar-14 10:37 
QuestionOne more way of doing it Pin
Rahul Rajat Singh10-Feb-12 4:38
professionalRahul Rajat Singh10-Feb-12 4:38 
AnswerRe: One more way of doing it Pin
GottZ13-Feb-12 8:10
GottZ13-Feb-12 8:10 
AnswerRe: One more way of doing it Pin
Pranit Kothari16-Mar-12 3:04
Pranit Kothari16-Mar-12 3:04 
AnswerRe: One more way of doing it Pin
.:floyd:.4-Mar-14 3:28
.:floyd:.4-Mar-14 3:28 

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.