PostMessage for hidden form not working for the first time

I have a C # WinForms application that appears in the taskbar. The application hides the main form when loading:

private void MainForm_Load(object sender, System.EventArgs e)
{
    Hide();
}

For the same basic form, I overdid it with WndProc to catch a custom window message (this message is registered on Windows with a call to the RegisterWindowMessageWin32 API ).

protected override void WndProc(ref Message m)
{
    if (m.Msg == WM_MYCUSTOM_MESSAGE)
    {
        // Handle custom message
    }
}

From an external C ++ application, I am broadcasting the same window with the PostMessage message.

UINT msg = RegisterWindowMessage(L"WM_MYCUSTOM_MESSAGE");
PostMessage(HWND_BROADCAST, msg, NULL, NULL);

When I execute the above code after the first launch of a C # application, it does not fall into WndProc. After displaying the main form (by double-clicking on the icon in the system tray, which essentially does Show()), catching the broadcast message works and continues to work after hiding the form with Hide().

- , Hide() MainForm_Load? ?

EDIT1: , Hide on load. WndProc , ...

+3
3

, PostMessage() to HWND_BROADCAST Form.WndProc, Form.ShowInTaskbar false, SendMessage() - HWND_BROADCAST.

, MSDN HWND_BROADCAST .

, , Windows API.

+6

- , Hide() Load event handler . Winforms, .

, , . , . OnHandleCreated() , . Create() , , NotifyIcon . , Application.Run(), .

, , , .

+3
+1

All Articles