简体   繁体   中英

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

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM