Win32程序实现的步骤:
WinMain函数的定义
创建一个窗口
1. 设计一个窗口类 2. 注册窗口类 3. 创建窗口 4. 显示及刷新窗口
- 进行消息循环
- 完成回调函数
//WinMain函数的定义
int WINAPI WinMain( HINSTANCE hInstance, //当前实例句柄
HINSTANCE hPrevInstance, //上一个实例句柄
LPSTR lpCmdLine,//传递给应用程序的参数字符串
int nCmdShow); //窗口显示状态
/*创建一个窗口
1. 设计一个窗口类
2. 注册窗口类
3. 创建窗口
4. 显示及刷新窗口
*/
WNDCLASSEX mywnd;
memset(&mywnd, 0, sizeof(WNDCLASSEX));
mywnd.cbSize = sizeof(WNDCLASSEX);
mywnd.style = CS_HREDRAW | CS_VREDRAW; // 窗口风格
mywnd.lpfnWndProc = MyWndProc; // 窗口过程函数
mywnd.hInstance = hInstance; // 进程句柄
mywnd.hIcon = NULL; // 应用程序图标句柄,使用LoadIcon函数
mywnd.hCursor = NULL; // 应用程序光标句柄,LoadCursor函数
/*mywnd.hbrBackground = (HBRUSH)(COLOR_WINDOW+2);
mywnd.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
*/
mywnd.hbrBackground = (HBRUSH)CreateSolidBrush(RGB(255,0,0));// 背景画刷句柄
mywnd.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);
mywnd.lpszClassName = "MyWndClass";
//注册窗口类
ATOM atom = RegisterClassEx(&mywnd);
//创建窗口
HWND myhwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
"MyWndClass",
"WindowsName",
WS_OVERLAPPEDWINDOW,
100,//与窗口Left的距离
120,//与窗口Top的距离
640,//窗口的宽度
480,//窗口的高度
NULL,
NULL,
hInstance,
NULL);
//显示及刷新窗口
ShowWindow(myhwnd, SW_SHOWNORMAL);
UpdateWindow(myhwnd);
//进行消息循环
BOOL bRet;
MSG msg;
HWND hWnd = myhwnd;
while( (bRet = GetMessage(&msg, hWnd, 0 , 0)) != 0 )
{
if(bRet == -1)
{
return -1;//if hWnd is an invalid window handle
}
else
{
TranslateMessage (&msg);
DispatchMessage(&msg);
}
}
//回调函数
LRESULT CALLBACK MyWndProc(
HWND hwnd,// handle to window
UINT uMsg,// message identifier
WPARAM wParam,// first message parameter
LPARAM lParam)// second message parameter
{
switch (uMsg)
{
case WM_CREATE:
// Initialize the window.
return 0;
case WM_PAINT:
// Paint the window's client area.
return 0;
case WM_CLOSE:
DestroyWindow(hwnd);
return 0;
case WM_DESTROY:
PostMessage(hwnd, WM_QUIT, NULL, NULL);
return 0;
case WM_COMMAND:
// Process other messages.
return 0;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
return 0;
}