The following is the simplest win32 form.
# Include "stdafx. h"
Lresult callback MainWndProc (HWND, UINT, WPARAM, LPARAM );
Int APIENTRY WinMain (HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
Int nCmdShow)
{
// TODO: Place code here.
Char szClassName [] = "MainWClass ";
WNDCLASSEX wndClass;
WndClass. cbSize = sizeof (wndClass );
WndClass. style = CS_HREDRAW | CS_VREDRAW;
WndClass. lpfnWndProc = MainWndProc;
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 (WHITE_BRUSH );
WndClass. lpszMenuName = NULL;
WndClass. lpszClassName = szClassName;
WndClass. hIconSm = NULL;
// Register this window class
: RegisterClassEx (& wndClass );
// Create the Main Window
HWND hwnd =: createmediawex (
0,
SzClassName,
"My frist Wndiows ",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
HInstance,
NULL );
If (hwnd = NULL)
{
: MessageBox (NULL, "An error occurred while creating the window! "," Error ", MB_ OK );
Return-1;
}
// Display the window and refresh the customer area
: ShowWindow (hwnd, nCmdShow );
: UpdateWindow (hwnd );
// Retrieve the message from the message queue and send it to the window function for processing until GetMessage returns False to end the message loop.
MSG msg;
While (: GetMessage (& msg, NULL, 0, 0 ))
{
// Convert keyboard messages
: TranslateMessage (& msg );
// Send the message to the corresponding Window Function
: DispatchMessage (& msg );
}
// When GetMessage returns False, the program ends.
Return msg. wParam;
}
Lresult callback MainWndProc (
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
)
{
Char szText [] = "the simplest window program! ";
Switch (uMsg)
{
Case WM_PAINT:
HDC hdc;
PAINTSTRUCT ps;
// Make invalid customer zones valid and obtain the device environment handle
Hdc =: BeginPaint (hwnd, & ps );
// Display text
: TextOut (hdc, 10, 10, szText, strlen (szText ));
: EndPaint (hwnd, & ps );
Return 0;
Case WM_DESTROY:
// Send a WM_QUIT message to the message queue, prompting the GetMessage function to return 0 and end the message loop.
: PostQuitMessage (0 );
Return 0;
}
Return: DefWindowProc (hwnd, uMsg, wParam, lParam );
}
From the code above, we can see the steps for displaying a form on the desktop:
1. Registration window class
2. Create a window
3. display the window on the desktop
4. Update the customer area in the window
5. Enter windows message loop