Click here to Skip to main content
Licence CPOL
First Posted 10 Feb 2012
Views 10,520
Downloads 206
Bookmarked 8 times

Making a Borderless Form Movable in C++

By | 15 Mar 2012 | Article
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)

About the Author

Preston Gull

Software Developer
Graffiware Software Development
United States United States

Member

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/

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionThat is funny [modified] PinmemberPeter Hawke13:39 13 Feb '12  
AnswerRe: That is funny PinmemberPreston Gull17:37 13 Feb '12  
QuestionForm Move Pinmembergeoyar10:47 13 Feb '12  
GeneralMy vote of 2 PinmemberMemberHeck5:34 13 Feb '12  
GeneralRe: My vote of 2 PinmemberPreston Gull9:27 13 Feb '12  
QuestionVery funny C++ PinmemberLeonid Shikhmatov5:31 13 Feb '12  
AnswerRe: Very funny C++ Pinmemberbrik007:25 13 Feb '12  
AnswerRe: Very funny C++ PinmemberPreston Gull9:29 13 Feb '12  
AnswerRe: Very funny C++ PinmemberDaniele Rota Nodari4:50 16 Mar '12  
QuestionOne more way of doing it PinmemberRahul Rajat Singh4:38 10 Feb '12  
AnswerRe: One more way of doing it PinmemberGottZ8:10 13 Feb '12  
AnswerRe: One more way of doing it PinmemberPranit Kothari3:04 16 Mar '12  

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.

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