如果是一开始学习可以将空项目选上
选择cpp文件,然后修改文件名称,点击添加。
#include
// 必须要进行前导声明
LRESULT CALLBACK WindowProc(
_In_ HWND hwnd,
_In_ UINT uMsg,
_In_ WPARAM wParam,
_In_ LPARAM lParam
);
int CALLBACK WinMain(
_In_ HINSTANCE hInstance,
_In_ HINSTANCE hPrevInstance,
_In_ LPSTR lpCmdLine,
_In_ int nCmdShow
)
{
// 类名
WCHAR* cls_Name = L"My Class";
// 设计窗口类
WNDCLASS wc = { sizeof(WNDCLASS) };
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.lpfnWndProc = WindowProc;
wc.lpszClassName = cls_Name;
wc.hInstance = hInstance;
wc.style = CS_HREDRAW | CS_VREDRAW;
// 注册窗口类
RegisterClass(&wc);
// 创建窗口
HWND hwnd = CreateWindow(
cls_Name, //类名,要和刚才注册的一致
L"我的应用程序", //窗口标题文字
WS_OVERLAPPEDWINDOW, //窗口外观样式
38, //窗口相对于父级的X坐标
20, //窗口相对于父级的Y坐标
480, //窗口的宽度
250, //窗口的高度
NULL, //没有父窗口,为NULL
NULL, //没有菜单,为NULL
hInstance, //当前应用程序的实例句柄
NULL); //没有附加数据,为NULL
if (hwnd == NULL) //检查窗口是否创建成功
return 0;
// 显示窗口
ShowWindow(hwnd, SW_SHOW);
// 更新窗口
UpdateWindow(hwnd);
// 消息循环
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
// 在WinMain后实现
LRESULT CALLBACK WindowProc(
_In_ HWND hwnd,
_In_ UINT uMsg,
_In_ WPARAM wParam,
_In_ LPARAM lParam
)
{
switch (uMsg)
{
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
case WM_PAINT:
{
PAINTSTRUCT ps;
BeginPaint(hwnd, &ps);
// 设置hdc颜色
SetTextColor(ps.hdc, RGB(10, 0, 255));//设置文本颜色
DrawText(ps.hdc, L"朋友,你好。", wcslen(L"朋友,你好。"), &(ps.rcPaint), DT_CENTER);
// 用于设置每个字符间隔的数组
int arr1[2] = { 45,0 };
int arr2[3] = { 35, 40, 0 };
int arr3[2] = { 32, 0 };
POLYTEXT polys[] = { { 2,2, wcslen(L"大家"),L"大家",ETO_CLIPPED,ps.rcPaint,&arr1[0] },
{ 2,25,wcslen(L"新年好"),L"新年好",ETO_CLIPPED,ps.rcPaint,&arr2[0] },
{ 30,60,wcslen(L"快乐\0"),L"快乐\0",ETO_CLIPPED,ps.rcPaint,&arr3[0] }
};
PolyTextOut(ps.hdc, &polys[0], 3);
// 填充图形
// 创建画刷
HBRUSH hb = CreateSolidBrush(RGB(0, 255, 0));
// 画刷选择到当前DC中
HBRUSH orgBrs = (HBRUSH)SelectObject(ps.hdc, hb);
// 填充图形
Ellipse(ps.hdc, 135, 35, 202, 170);
// 选回原先的画刷
SelectObject(ps.hdc, orgBrs);
DeleteObject(hb); //清理对
EndPaint(hwnd, &ps);
}
default:
break;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}