Click here to Skip to main content
Click here to Skip to main content

Making a Borderless Form Movable in C++

By , 15 Mar 2012
Rate this:
Please Sign up or sign in to vote.

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)

About the Author

Preston Gull
Software Developer Graffiware Software Development
United States United States
I am 13 years old, and love to code. I normally don't get my homework done in time because I can't wait to finish and code my latest project! Writing software and graphic designing for a company I hope to make someday. My site (for my company) is: http://graffiware.weebly.com/

Comments and Discussions

 
Suggestion[My vote of 1] Useless hack to solve a problem that already has a clean solution. Pinmember.:floyd:.4-Mar-14 1:18 
QuestionHow I implemented it for an MFC dialog Pinmemberlkitross5-Jan-14 20:54 
AnswerRe: How I implemented it for an MFC dialog Pinmember.:floyd:.4-Mar-14 1:24 
GeneralMy vote of 5 Pinmemberwangxj_nemo10-Jan-13 18:11 
QuestionThat is funny [modified] PinmemberPeter Hawke13-Feb-12 13:39 
AnswerRe: That is funny PinmemberPreston Gull13-Feb-12 17:37 
QuestionForm Move Pinmembergeoyar13-Feb-12 10:47 
GeneralMy vote of 2 PinmemberMemberHeck13-Feb-12 5:34 
GeneralRe: My vote of 2 PinmemberPreston Gull13-Feb-12 9:27 
QuestionVery funny C++ PinmemberLeonid Shikhmatov13-Feb-12 5:31 
AnswerRe: Very funny C++ Pinmemberbrik0013-Feb-12 7:25 
AnswerRe: Very funny C++ PinmemberPreston Gull13-Feb-12 9:29 
AnswerRe: Very funny C++ PinmemberDaniele Rota Nodari16-Mar-12 4:50 
GeneralRe: Very funny C++ Pinmember.:floyd:.4-Mar-14 3:37 
AnswerRe: Very funny C++ PinmemberDaniele Rota Nodari4-Mar-14 5:02 
GeneralRe: Very funny C++ Pinmember.:floyd:.4-Mar-14 6:27 
AnswerRe: Very funny C++ PinmemberDaniele Rota Nodari4-Mar-14 9:03 
GeneralRe: Very funny C++ Pinmember.:floyd:.4-Mar-14 9:59 
AnswerRe: Very funny C++ PinmemberDaniele Rota Nodari4-Mar-14 10:37 
QuestionOne more way of doing it PinmemberRahul Rajat Singh10-Feb-12 4:38 
AnswerRe: One more way of doing it PinmemberGottZ13-Feb-12 8:10 
AnswerRe: One more way of doing it PinmemberPranit Kothari16-Mar-12 3:04 
AnswerRe: One more way of doing it Pinmember.:floyd:.4-Mar-14 3: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 | Mobile
Web03 | 2.8.140415.2 | Last Updated 15 Mar 2012
Article Copyright 2012 by Preston Gull
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid