Click here to Skip to main content
15,877,888 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My question is simple.

Can we move a borderless form in Visual Studio?

I have search on net, but the answers were coming for Visual Basic.

It's very hard to understand and I have searched on Code Project but I don't think so there is an article as such.

I just want to move a form with a panel on the top. Not just want to move by clicking anywhere in the form.

Can we do such thing in Visual Studio?

Which event will be using?

Please help me

Thanks in advance
Posted
Updated 21-Sep-12 4:40am
v3
Comments
[no name] 21-Sep-12 10:43am    
"answers were coming for Visual Basic", so what? Convert it!
"Not just want to move by clicking anywhere in the form", so how else are you wanting to do this?
"Can we do such thing in Visual Studio", no you would have to write some code for this.
"Which event will be using", the last time I did this I used mouse events.
shaikh-adil 21-Sep-12 10:46am    
i am learning .net.
and dont have much knowledge. neither i would i done myself.
i dont want to move form b clicking anywhere. i will create a panel either and b clicking on that panel form will move.
means panel will serve as a title for moving my form
[no name] 21-Sep-12 11:03am    
So you want to move the form by clicking and at the same time not click anywhere and have the form move? That is confusing and contradictory. How else are you wanting to move the form around? Magic?

Yes.

Amazingly, opening google and searching for "C# Dragging Borderless form" threw up a number of results including;

Draggable Form: Drag a Borderless Form by Clicking Anywhere on the Form[^]

The secret lies in (extract from source);
C#
//
            //Add Mouse Event Handlers for each control added into the form,
            //if Draggable property of the form is set to true and the control
            //name is not in the ExcludeList.Exclude list is the comma separated
            //list of the Controls for which you do not require the mouse handler 
            //to be added. For Example a button.  
            //
 
Share this answer
 
v2
You can use APIs for this problem.

C#
private const int HT_CAPTION = 0x2;

C#
private const int WM_NCLBUTTONDOWN = 0x00A1;


C#
[DllImport("user32", CharSet = CharSet.Auto)]
private static extern bool ReleaseCapture();

C#
[DllImport("user32", CharSet = CharSet.Auto)]
private static extern int SendMessage(
    IntPtr hwnd,
    int wMsg,
    int wParam,
    int lParam);



Use this methods from a MouseDown event, as shown here:
C#
protected override void OnMouseDown(MouseEventArgs e)
{
    base.OnMouseDown(e);
    if (e.Button == MouseButtons.Left)
    {
        Rectangle rct = DisplayRectangle;
        if (rct.Contains(e.Location))
        {
            ReleaseCapture();
            SendMessage(this.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
        }
    }
}


You can also use that you've created in a mousedown event handler.
 
Share this answer
 
v2
Comments
DaveAuld 21-Sep-12 12:21pm    
Does that handle clicking on a panel in the form, as well as the form, as requested in the OPs original question?
Burak Ozdiken 21-Sep-12 12:41pm    
If you'll use this technique with a panel on the form, you'll call the same method in the Panel mouse event. For example;

if (rct.Contains(e.Location))
{
ReleaseCapture();
SendMessage(panel1.Parent.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}

That's it.
shaikh-adil 23-Sep-12 2:20am    
bro its working thanks. but can you explain whats does the line of coding if for? because i have presentation of which i have done. so i ave to explain the coding part
private const int HT_CAPTION = 0x2; // what does this means
private const int WM_NCLBUTTONDOWN = 0x00A1; // what does this means
[DllImport("user32", CharSet = CharSet.Auto)]// what does this means
private static extern bool ReleaseCapture();
[DllImport("user32", CharSet = CharSet.Auto)]// what does this means
private static extern int SendMessage(
IntPtr hwnd,
int wMsg,
int wParam,
int lParam);
protected override void OnMouseDown(MouseEventArgs e)// what does this means
{
base.OnMouseDown(e);
if (e.Button == MouseButtons.Left)
{
Rectangle rct = DisplayRectangle;
if (rct.Contains(e.Location))
{
ReleaseCapture();
SendMessage(this.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}
}
Burak Ozdiken 23-Sep-12 8:07am    
The HT_CAPTION(wParam value of the SendMessage API) and WM_NCLBUTTONDOWN(Windows message constants) are our symbolic constants.
To use a win32API for your application, you need to add the 'DllImport' attribute at top of the code. So you can call it from your method. Also you need to pass the parameter of the character set and which *.dll will be used for this API.

OnMouseDown: When you pressed the left mouse button on the panel or form, this event occurs.
shaikh-adil 23-Sep-12 11:58am    
HT_CAPTION
wparam values of the send message api? what is this use for? bro..
and wm_ncbutton -windows message constant are smbollic constant?
whats does this two used for? give me some more info bro.
thanks for helping me
:)

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