سؤال

In C++ on Windows, we use user defined message to in form UI to update data thru PostMessage API. We can use PostMessage in C# but do not know how to process user defined message in Forms! Some ones will tell me to use delegated and invoke but we have problems when forms closed or not already created when threads call delegated. We still prefer PostMessage to inform UI to update data. Please Help.

هل كانت مفيدة؟

المحلول

Form has a method you can override, WndProc, that will receive your custom message. It takes a Message structure as its parameter, which encapsulates the hwnd, msg, wParam, and lParam parameters of the message, and includes a field for the message result. So assuming you have a registered message:

class MyForm : Form
{
    const int MyMessage = WM_USER + 0x05; // for example

    protected override void WndProc(ref Message m)
    {
        if (m.Msg == MyMessage)
        {
            // do whatever with your message
        }
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top