文件为:FirstApp.cpp
Gitee代码链接(有100多MB)
FirstApp.cpp
#include<windows.h>
#include<tchar.h>
#include<iostream>
using namespace std;
static TCHAR szWindowClass[] = _T("WindowClass");
static TCHAR szTitle[] = _T("我的第一个窗口程序");
// 第五步:窗口函数中处理窗口消息
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
TCHAR greeting[] = _T("大家好!");
switch (uMsg)
{
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
TextOut(hdc, 5, 5, greeting, _tcslen(greeting));
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, uMsg, wParam, lParam);
break;
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, LPSTR lpCmdLine, int nCmdShow)
{
// 第一步:注册窗口类, 指定窗口类的名称和窗口回调函数
WNDCLASSEX wcex = {0};
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.lpfnWndProc = WindowProc;
wcex.lpszClassName = szWindowClass;
if (!RegisterClassEx(&wcex))
{
MessageBox(NULL, _T("注册窗口类失败"), _T("Tip"), NULL);
return 1;
}
// 第二步:创建窗口,指定注册窗口类,窗口标题,窗口大小
HWND hWnd = CreateWindow(
szWindowClass,
szTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
500, 300,
NULL,
NULL,
hInstance,
NULL
);
if (!hWnd)
{
MessageBox(NULL, _T("创建窗口失败"), _T("Tip"), NULL);
return 1;
}
//第三步:显示窗口
ShowWindow(hWnd, nCmdShow);
// 第四步:开始消息循环
MSG msg = {0};
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}