初识Window API
窗口类WNDCLASS是Windows中的创建窗口的模板
系统窗口类,系统内部使用
应用程序全局窗口类 CS_GLOBALCLASS
应用程序局部窗口类
#include <windows.h>
#define null NULL
//窗口过程
LRESULT CALLBACK MyWindowProc
(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam)
{
switch (Msg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hWnd, Msg, wParam, lParam);
}
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine,int nShowCmd)
{
//注册窗口类
WNDCLASS wnd;
wnd.cbClsExtra = 0;
wnd.cbWndExtra = 0;
wnd.hbrBackground = (HBRUSH)(GetStockObject(GRAY_BRUSH));//背景色
wnd.hCursor = LoadCursor(NULL,IDC_ARROW);//光标
wnd.hIcon = LoadIcon(NULL,IDI_APPLICATION);//图标
//wnd.lpfnWndProc = DefWindowProc;//窗口过程函数,用于处理消息
wnd.lpfnWndProc = MyWindowProc;
wnd.lpszClassName = L"MrVurtual";
wnd.lpszMenuName = NULL;//菜单资源名称
wnd.style = CS_HREDRAW;//窗口类样式
wnd.hInstance = hInstance;
RegisterClass(&wnd);
//创建窗口 WM_CREATE
HWND hWnd = CreateWindow(L"MrVurtual", L"第一个窗口程序", WS_OVERLAPPEDWINDOW,
100, 100, 300, 300, NULL, NULL, hInstance,NULL);
//显示窗口
ShowWindow(hWnd, nShowCmd);
//更新窗口 WM_PAINT
UpdateWindow(hWnd);
//消息循环 收到WM_QUIT消息退出
MSG msg;
while (GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);//翻译消息
DispatchMessage(&msg);//分发消息到窗口过程
}
return 0;
}