Click here to Skip to main content
15,918,050 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
If I set FormBorderStyle = None, how can I allow it to be resized?

[Modified: Learn how to write a proper question. The subject should be general and the "Detailed Description" should contain the more detailed description of the question]
Posted
Updated 24-Jun-10 7:49am
v2
Comments
CoderzF1 26-Feb-16 12:19pm    
you can use the mouse events to check coordinates to display the correct cursor, then ise the mousedown event along side a sendmessage api call for resizing. i cant quite remember the message or parameters to send via SendMessage for this to work. google "using sendmessage api to resize and move form"

What you have to do is implement it yourself using the Form's MouseMove and MouseDown events. This is a little more complicated if you have menus because then you also have to handle the menu's MouseMove and MouseDown as well because it will override the Form's events. The basics are:

C#
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
    if ((e.X <= 2 && e.Y <= 2) || (e.X + 2 >= this.Width && e.Y + 2 >= this.Height))
    {
        this.Cursor = Cursors.SizeNWSE;
    }
    else if ((e.X + 2 >= this.Width && e.Y <= 2) || (e.X <= 2 && e.Y + 2 >= this.Height))
    {
        this.Cursor = Cursors.SizeNESW;
    }
    else if (e.X <= 2 || e.X + 2 >= this.Width)
    {
        this.Cursor = Cursors.SizeWE;
    }
    else if (e.Y <= 2 || e.Y + 2 >= this.Height)
    {
        this.Cursor = Cursors.SizeNS;
    }
    else
    {
        this.Cursor = Cursors.Default;
    }
}


Of course, to fully implement it, you would have to check if the mouse was down (you can use a bool for that and set it in the MouseDown and MouseUp events) and then check to see which way it is being dragged. If it's being dragged up or left, you'd have to first move the form and then resize based on how much it moved. If it's to the right or bottom, you can just set the height and width to the mouse location (of course, check first, because it looks like the mouse isn't registered as being in the form until it is two less than the height and width).

And you could simplify it by only allowing them to resize using the bottom-right and you could add a picture in that corner indicating you can resize it.

[Update]
Actually, it's not going to be as simple as that. You would first have to do a global hook on the mouse. The reason is that MouseMove won't work when you move outside of the form, which you would have to do to resize it. So, it can be done, but again, you'd have to use a global hook. An article on how to do that is here: Processing Global Mouse and Keyboard Hooks in C#[^]
 
Share this answer
 
v2
In C# (and VB, for that matter) you need to have a border for a form to be sizeable: it is the border that the user pulls, and when there is no border, there is nothing to grab on to.

Assuming that what you really want is a sizable form without a title bar, you can get this by setting ControlBox, MaximizeBox and MinimizeBox all to False and Text to an empty string. With nothing to display in the title bar, the Framework won't draw one. Just remember to give your users some way to close the form ;P
 
Share this answer
 
v3
Comments
William Winner 24-Jun-10 14:01pm    
Reason for my vote of 2
that's only true if you assume that the coder can't implement the resizing, which is not that hard to do.
Gregory Gadow 24-Jun-10 14:04pm    
When I responded 4 hours ago, the question was quite different.
William Winner 24-Jun-10 14:20pm    
um...no...it said, "how make a form resizable with FormBorderStyle = None in C#"

The only thing that has changed is that the question was put into the question and put into proper English. I believe the intent of the question is the same.
Without a border, only way AFAIK, will be to handle the MouseDown, MouseMove,MouseUp and Paint events. You will have to get the co-ordinates of the move and change the size of the form.

If you really need resizing, you should have a border.

BTW why are you having a borderless form? If your motive can be achieved with a border, you do not need all those events.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900