Click here to Skip to main content
11,435,451 members (66,398 online)
Click here to Skip to main content

Making a Borderless Form Movable in C++

, 15 Mar 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

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

Comments and Discussions

 
Suggestion[My vote of 1] Useless hack to solve a problem that already has a clean solution. Pin
.:floyd:.4-Mar-14 2:18
member.:floyd:.4-Mar-14 2:18 
QuestionHow I implemented it for an MFC dialog Pin
lkitross5-Jan-14 21:54
memberlkitross5-Jan-14 21:54 
AnswerRe: How I implemented it for an MFC dialog Pin
.:floyd:.4-Mar-14 2:24
member.:floyd:.4-Mar-14 2:24 
GeneralMy vote of 5 Pin
wangxj_nemo10-Jan-13 19:11
memberwangxj_nemo10-Jan-13 19:11 
QuestionThat is funny [modified] Pin
Peter Hawke13-Feb-12 14:39
memberPeter Hawke13-Feb-12 14:39 
AnswerRe: That is funny Pin
Preston Gull13-Feb-12 18:37
memberPreston Gull13-Feb-12 18:37 
QuestionForm Move Pin
geoyar13-Feb-12 11:47
membergeoyar13-Feb-12 11:47 
GeneralMy vote of 2 Pin
MemberHeck13-Feb-12 6:34
memberMemberHeck13-Feb-12 6:34 
GeneralRe: My vote of 2 Pin
Preston Gull13-Feb-12 10:27
memberPreston Gull13-Feb-12 10:27 
QuestionVery funny C++ Pin
Leonid Shikhmatov13-Feb-12 6:31
memberLeonid Shikhmatov13-Feb-12 6:31 
AnswerRe: Very funny C++ Pin
brik0013-Feb-12 8:25
memberbrik0013-Feb-12 8:25 
AnswerRe: Very funny C++ Pin
Preston Gull13-Feb-12 10:29
memberPreston Gull13-Feb-12 10:29 
AnswerRe: Very funny C++ Pin
Daniele Rota Nodari16-Mar-12 5:50
memberDaniele Rota Nodari16-Mar-12 5:50 
GeneralRe: Very funny C++ Pin
.:floyd:.4-Mar-14 4:37
member.:floyd:.4-Mar-14 4:37 
AnswerRe: Very funny C++ Pin
Daniele Rota Nodari4-Mar-14 6:02
memberDaniele Rota Nodari4-Mar-14 6:02 
GeneralRe: Very funny C++ Pin
.:floyd:.4-Mar-14 7:27
member.:floyd:.4-Mar-14 7:27 
AnswerRe: Very funny C++ Pin
Daniele Rota Nodari4-Mar-14 10:03
memberDaniele Rota Nodari4-Mar-14 10:03 
GeneralRe: Very funny C++ Pin
.:floyd:.4-Mar-14 10:59
member.:floyd:.4-Mar-14 10:59 
AnswerRe: Very funny C++ Pin
Daniele Rota Nodari4-Mar-14 11:37
memberDaniele Rota Nodari4-Mar-14 11:37 
QuestionOne more way of doing it Pin
Rahul Rajat Singh10-Feb-12 5:38
memberRahul Rajat Singh10-Feb-12 5:38 
AnswerRe: One more way of doing it Pin
GottZ13-Feb-12 9:10
memberGottZ13-Feb-12 9:10 
AnswerRe: One more way of doing it Pin
Pranit Kothari16-Mar-12 4:04
memberPranit Kothari16-Mar-12 4:04 
AnswerRe: One more way of doing it Pin
.:floyd:.4-Mar-14 4:28
member.:floyd:.4-Mar-14 4:28 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150428.2 | Last Updated 15 Mar 2012
Article Copyright 2012 by Preston Gull
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid