注意红色部分,
1、如果缺少 #include 链接不会通过,
2、wcs.lpszClassName = clsName; 和 CreateWindow(clsName, 中的 clsName一定要保持一致,否则会出错误
#include
#include
//消息回调函数
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMessage, WPARAM wParam, LPARAM lParam)
{
return DefWindowProc(hWnd, uMessage, wParam, lParam);
}
//int APIENTRY _tWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPreInstance, _In_ LPTSTR lpCmdLine, _In_ int nShowCmd)
int APIENTRY _tWinMain( _In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPTSTR lpCmdLine, _In_ int nShowCmd )
{
//类名
TCHAR clsName[] = TEXT("Hellp");
TCHAR winName[] = TEXT("窗口Hello");
//描述一个窗口类
WNDCLASSW wcs ;
wcs.style = CS_HREDRAW | CS_VREDRAW;
wcs.lpfnWndProc = WndProc;
wcs.cbClsExtra = 0;
wcs.cbWndExtra = 0;
wcs.hInstance = hInstance;
wcs.hIcon = NULL;
wcs.hCursor = LoadCursor(hInstance, IDC_ARROW);
wcs.hbrBackground = CreateSolidBrush(RGB(0xFF, 0xFF, 0xFF));
wcs.lpszMenuName = NULL;
wcs.lpszClassName =
clsName;
//注册窗口类
if ( RegisterClass(&wcs))
{
MessageBox(NULL, TEXT("失败"), NULL, NULL);
return 0;
}
//创建窗口类
HWND hwnd = CreateWindow(
clsName, winName, WS_OVERLAPPEDWINDOW, 100, 100, 200, 200, NULL, NULL, hInstance, NULL);
//显示更新窗口
ShowWindow(hwnd, nShowCmd);
UpdateWindow(hwnd);
MSG msg ;
//循环消息
//获取消息
while (GetMessage(&msg, hwnd, NULL, NULL))
{
//转换消息
TranslateMessage(&msg);
//分发消息
DispatchMessage(&msg);
}
return 0;
}