0 Introduction
The Win32 API is an application programming interface for creating Windows applications. Typically, a WIN32 application consists of several parts:
1) Application portal;
2) Registration window class;
3) Create a window;
4) display window;
5) Update window
6) message loop;
7) Distribution of information;
1 Application Portal
The Win32 application takes the WinMain function as a program entry and the function prototype is as follows:
int WinMain (
HINSTANCE HInstance,
HInstance hPrevInstance,
LPTSTR lpCmdLine,
int ncmdshow);
2 Register window class
The registration window class needs to provide a custom window class name, a window created by this window class name that the operating system will post to the window procedure function specified when registering the window class.
The WIN32 application uses RegisterClassEx to register the window class, and the function prototype is as follows:
ATOM registerclassex (CONST wndclassex* pwndclass);
We use Visual Studio to create a function that automatically generates a registration window on a Win32 project, and the function is implemented as follows:
ATOM MyRegisterClass (hinstance hinstance)
{
Wndclassex Wcex;
wcex.cbsize = sizeof (wndclassex);
Wcex.style = Cs_hredraw | Cs_vredraw;
Wcex.lpfnwndproc = WndProc;
Wcex.cbclsextra = 0;
Wcex.cbwndextra = 0;
Wcex.hinstance = hinstance;
Wcex.hicon = LoadIcon (hinstance, Makeintresource (Idi_skinui));
Wcex.hcursor = LoadCursor (NULL, Idc_arrow);
Wcex.hbrbackground = (hbrush) (color_window+1);
Wcex.lpszmenuname = Makeintresource (Idc_skinui);
Wcex.lpszclassname = Szwindowclass;
WCEX.HICONSM = LoadIcon (Wcex.hinstance, Makeintresource (Idi_small));
return RegisterClassEx (&WCEX);
}
3 Creating Windows
The WIN32 application uses CreateWindowEx to create the window, and the function prototype is as follows:
HWND CREATEWINDOWEXW (
__in DWORD dwExStyle,
__in_opt LPCWSTR Lpclassname,
__in_opt LPCWSTR Lpwindowname,
__in DWORD Dwstyle,
__in int X,
__in int Y,
__in int nwidth,
__in int nheight,
__in_opt HWND hWndParent,
__in_opt HMENU HMENU,
__in_opt hinstance HInstance,
__in_opt lpvoid Lpparam);
4 display window
The WIN32 application uses the ShowWindow display window, and the function prototype is as follows:
BOOL ShowWindow (__in hwnd hwnd, __in int ncmdshow);
5 Update window
The WIN32 application uses the UpdateWindow Update window and the function prototype is as follows:
BOOL UpdateWindow (__in hwnd hwnd);
6 Message Loops
Each WIN32 application requires at least one message loop with the following structure:
MSG msg;
while (GetMessage (&msg, NULL, 0, 0))
{
if (! TranslateAccelerator (Msg.hwnd, hacceltable, &msg))
{
TranslateMessage (&MSG);
DispatchMessage (&MSG);
}
}
Function prototypes are not listed here, specifically for MSDN.
7 Distributing messages
The window procedure function is used to receive messages from the system, and the WIN32 application distributes the messages here. A typical window procedure function is as follows:
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM WPARAM, LPARAM LPARAM)
{
int Wmid, wmevent;
Paintstruct PS;
HDC hdc;
Switch (message)
{
Case WM_COMMAND:
Wmid = LoWord (WParam);
Wmevent = HiWord (WParam);
Parse the menu selections:
Switch (WMID)
{
Case Idm_about:
DialogBox (HInst, Makeintresource (Idd_aboutbox), hWnd, about);
Break
Case Idm_exit:
DestroyWindow (HWND);
Break
Default
Return DefWindowProc (hWnd, message, WParam, LParam);
}
Break
Case WM_PAINT:
HDC = BeginPaint (hWnd, &ps);
Todo:add any drawing code here ...
EndPaint (HWnd, &ps);
Break
Case Wm_destroy:
PostQuitMessage (0);
Break
Default
Return DefWindowProc (hWnd, message, WParam, LParam);
}
return 0;
}
8 concluding remarks
With the combination of the above sections, a simple WIN32 application is completed. The specific code is as follows:
int WinMain (hinstance hinstance, hinstance hprevinstance, LPTSTR lpcmdline, int ncmdshow)
{
Unreferenced_parameter (hprevinstance);
Unreferenced_parameter (lpCmdLine);
MyRegisterClass (HINSTANCE);
HWND hwnd = CreateWindow (Szwindowclass, SzTitle, Ws_overlappedwindow, Cw_usedefault, 0, cw_usedefault, 0, NULL, NULL, HINS Tance, NULL);
if (!hwnd)
{
return FALSE;
}
ShowWindow (HWnd, ncmdshow);
UpdateWindow (HWND);
MSG msg;
while (GetMessage (&msg, NULL, 0, 0))
{
if (! TranslateAccelerator (Msg.hwnd, hacceltable, &msg))
{
TranslateMessage (&MSG);
DispatchMessage (&MSG);
}
}
return (int) Msg.wparam;
}
Basic structure of the WIN32 application