Editor's note: MFC has done a lot of work for us and it is necessary to learn the detailed process.
:
// Win32 dialog box. cpp: Defines the entry point for the application.
//
# Include "stdafx. h"
// Define the window message Response Function
Lresult callback WndProc (HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam)
{
HDC hdc;
RECT rect;
PAINTSTRUCT ps; // receives the customer plot information returned by BeginPaint.
Switch (uMsg) // receives the client Region coordinates returned by GetClientRect
{
Case WM_PAINT:
{
// The pointer parameter generally indicates the return value of the receiving function, that is, _ out
Hdc = BeginPaint (hwnd, & ps );
GetClientRect (hwnd, & rect );
DrawText (hdc, "http://hi.baidu.com/darks00n",-1, & rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER );
EndPaint (hwnd, & ps );
Break;
}
Case WM_DESTROY: // terminate the corresponding thread after the window is closed
{
PostQuitMessage (0 );
}
Default:
Break;
}
Return DefWindowProc (hwnd, uMsg, wParam, lParam); // The default message processing function is used to send unprocessed messages.
}
Int APIENTRY WinMain (HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
Int nCmdShow)
{
// Step 1. Fill in the WNDCLASS struct
WNDCLASS wndclass;
Wndclass. style = CS_HREDRAW | CS_VREDRAW;
Wndclass. lpfnWndProc = WndProc;
Wndclass. cbClsExtra = 0;
Wndclass. cbWndExtra = 0;
Wndclass. hInstance = hInstance;
Wndclass. hIcon = LoadIcon (NULL, IDI_APPLICATION );
Wndclass. hCursor = LoadCursor (NULL, IDC_ARROW );
Wndclass. hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH );
Wndclass. lpszMenuName = NULL;
Wndclass. lpszClassName = "WND ";
// Step 2. register the window class
RegisterClass (& wndclass );
// Step 3. Create a window
HWND hwnd = CreateWindow ("WND", "Win32 dialog box", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL );
// Step 4. Specify the display window status
ShowWindow (hwnd, nCmdShow );
// Step 5. display window (send WM_PAINT message)
UpdateWindow (hwnd );
// Step 6. Message loop (continuously retrieve and process messages from the window thread Message Queue)
MSG msg;
// The value 0 indicates that WM_QUIT exits the program, and-1 indicates that the Message failed to be obtained.
While (GetMessage (& msg, NULL, 0, 0)> 0)
{
// Translate messages (convert a virtual key message to a character message, and then return it to the window Message Queue)
TranslateMessage (& msg); // differentiate the PreTranslateMessage function in MFC
// Send the message to the Message response function (in this example, WndProc)
DispatchMessage (& msg );
}
Return msg. wParam;
}