Search code examples
c#sendmessagepostmessage

Maximize form using send/postmessage User32.dll


I have windows application, i need to maximize the window/form.

I tried

    public const int WM_NCLBUTTONDOWN = 0xA1;
    public const int HT_CAPTION = 0x2;
    SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);

and 

    int WM_SIZE = 0x0005;
    int SIZE_MAXIMIZED = 2;
    SendMessage(Main, WM_SIZE, SIZE_MAXIMIZED, 0);

Nothing is not working.

Can any show me what the right wat to do it?


Solution

  • As described in the documentation, WM_SIZE is a notification, sent to the window in the event of the window's size changing. Sending it to a window won't change the size of the window. Your sending WM_NCLBUTTONDOWN appears to be an attempt to fake a click in the caption bar. That won't work, at least not the way you've done it.

    Use ShowWindowAsync passing the handle of the window that you wish to maximize, and SW_MAXIMIZE.