Lavina Shaktawat
Lavina Shaktawat

Reputation: 1

How to prevent a mdichild form to move/repositioning on screen

I want to fix the position of mdi child form. so that no one can move it from one location to another. I try to implement the following code but it gives me error for screen.width Here is my code:- Place this code in a Module:

public Sub CenterMe(Myform as Form)
    with Myform
        Myform.Left = (Screen.Width - .Width) / 2
        Myform.Top = (Screen.Height - .Height) / 2 ' - mdiMain.StatusBar.Height 
    End With
End Sub

now in the form load event of the children - call it like this:

Private Sub Form_Load()
    'do some initialization stuff... 
    '...
    Call CenterMe(me)
End Sub

Upvotes: 0

Views: 331

Answers (1)

ivayle
ivayle

Reputation: 1070

Once the initial location of the form is set, you can prevent form's position to be changed via overriding form's WndProc method and listen for WM_MOVE windows message like this.

protected override void WndProc(ref Message message)
{
    const int WM_SYSCOMMAND = 0x0112;
    const int SC_MOVE = 0xF010;

    switch (message.Msg)
    {
        case WM_SYSCOMMAND:
            int command = message.WParam.ToInt32() & 0xfff0;
            if (command == SC_MOVE)
                return;
            break;
    }

    base.WndProc(ref message);
}

Upvotes: 1

Related Questions