Windows message loops and window-to-thread relationships

Source: Internet
Author: User

/winmain function

int Apientry WinMain (hinstance hinstance, hinstance hprevinstance, LPSTR lpcmdline, int ncmdshow)
{
G_hinstance = hinstance;
if (! Register ("Main", WndProc))//Registration window class RegisterClassEx
{
MessageBox (NULL, "registration Failed", "Infor", MB_OK);
return 0;
}
HWND hwnd = Createmainwnd ("Main", "window"); Create Window CreateWindow
Display (HWND);
Message ();//messages Loop is responsible for handling messages from the OS in the main thread
return 0;
}
message loop (message pump)
void Message ()
{
MSG nmsg ={0};
while (GetMessage (&nmsg, NULL, 0, 0))
{
TranslateMessage (&NMSG);
DispatchMessage (&NMSG);
}
}

Windows message processing functions
LRESULT CALLBACK WindowProc (HWND hwnd, UINT nmsg, WPARAM WPARAM, LPARAM LPARAM)
{
Finally, messages you don't want to process are handed to the default message handler
Return DefWindowProc (hWnd, Nmsg, WParam, LParam); Default Windows Message handler function
}

Where the operating system sends the hardware messages to the message pump for each window's corresponding thread to process
//
If a window (WND) is created in a child thread, the message pump that processes the message needs to be in the child thread, and the message is not sent to the main thread's message loop
//
CreateThread (Null,0,threadproctrade,null,0,&threadid_trade);

DWORD WINAPI Threadproctrade (LPVOID lpparam)
{
G_hinstance = hinstance;
if (! Register ("Main", WndProc))//Registration window class RegisterClassEx
{
MessageBox (NULL, "registration Failed", "Infor", MB_OK);
return 0;
}
HWND hwnd = Createmainwnd ("Main", "window"); Create Window CreateWindow
Display (HWND);
Message (); Messages in the message loop sub-thread that are responsible for processing the UI created by this thread from the OS
return 0;
}

Windows message loops and window-to-thread relationships

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.