Moving a borderless form that contains child controls

Hi all..

I have followed the directions outlined in http://msdn2.microsoft.com/en-us/netframework/aa497373.aspx#7qgifyua
(search page for How do I support moving a borderless form )

private const int WM_NCLBUTTONDOWN = 0xA1;
private const int HTCAPTION = 0x2;

[ DllImport( "user32.dll" ) ]
public static extern bool ReleaseCapture();

[ DllImport( "user32.dll" ) ]
public static extern int SendMessage( IntPtr hWnd, int Msg, int wParam, int lParam );

private void Form1_MouseDown( object sender, MouseEventArgs e )
{
if ( e.Button == MouseButtons.Left )
{
ReleaseCapture();
SendMessage( Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0 );
}
}

to move a borderless form, but what I am seeing is that this solution only works for clicking
on the form, but not on a child control. For example, if I have a form that contains a group
box that covers most of the form, then the above solution only works for the area that the
group box doesn't cover.

Is there an easy way to implement this move regardless if the user clicked on the form or a
control

I am using a base form that all of my dialogs inherit from. I have implemented the above code
on my base class.

Any help would be appreciated.



Answer this question

Moving a borderless form that contains child controls

  • meravsha

    You could do it the easy way and use the same event in your groupBox's MouseDown event.


  • Rhubarb

    You can do the following which would make every control do that or add some logic to only do certain kinds of controls on the form. With the code below you could dock controls and still be able to move the form. But if you want to add say a button that the user can click on you'd need to exclude those types of controls. Maybe just include things like pictureboxes, panels, labels, groupboxes, splitters, etc.

    Public Class Form1

    Private Const WM_NCLBUTTONDOWN As Integer = &HA1

    Private Const HTCAPTION As Integer = &H2

    Declare Auto Function ReleaseCapture Lib "user32.dll" () As Boolean

    Declare Auto Function SendMessage Lib "user32" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer

    Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)

    If e.Button = MouseButtons.Left Then

    ReleaseCapture()

    SendMessage(Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0)

    End If

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Call MoveControls(Me.Controls)

    End Sub

    Private Sub MoveControls(ByVal ctrls As ControlCollection)

    For Each ctrl As Control In ctrls

    AddHandler ctrl.MouseDown, AddressOf Form1_MouseDown

    If ctrl.Controls.Count > 0 Then

    Call MoveControls(ctrl.Controls)

    End If

    Next

    End Sub

    End Class



  • Moving a borderless form that contains child controls