标签:
Win32 API是用于创建 Windows 应用程序的应用程序编程接口。通常情况下,一个Win32应用包含一下几个部分:
1) 应用程序入口;
2) 注册窗口类;
3) 创建窗口;
4) 显示窗口;
5) 更新窗口
6) 消息循环;
7) 派发消息;
Win32应用程序以WinMain函数作为程序入口,函数原型如下:
int WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow);
注册窗口类需要提供自定义的窗口类名,通过此窗口类名创建的窗口,操作系统会将消息投递至注册窗口类时指定的窗口过程函数。
Win32应用程序使用RegisterClassEx注册窗口类,函数原型如下:
ATOM RegisterClassEx(CONST WNDCLASSEX* pWndClass);
我们用Visual Studio创建一个win32项目上会自动生成一个注册窗口的函数,函数实现如下:
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);
}
Win32应用程序使用CreateWindowEx创建窗口,函数原型如下:
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);
Win32应用程序使用ShowWindow显示窗口,函数原型如下:
BOOL ShowWindow(__in HWND hWnd, __in int nCmdShow);
Win32应用程序使用UpdateWindow更新窗口,函数原型如下:
BOOL UpdateWindow(__in HWND hWnd);
每个Win32应用程序都需要至少一个消息循环,结构如下:
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
函数原型就不在此一一列出,具体参考MSDN。
窗口过程函数用来接收系统投递的消息,Win32应用程序在此派发消息。一个典型的窗口过程函数如下:
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;
}
通过以上各部分的组合,一个简单的Win32应用程序就完成了。具体代码如下:
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, hInstance, 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;
}
标签:
原文地址:http://www.cnblogs.com/skinui/p/4467949.html