例题5-1 在扇面上输出一首唐诗
#include"windows.h"
#include"tchar.h"
#include"math.h"
//#include "example5_1.h"
#define PI 3.1415926
BOOLEAN InitWindowClass(HINSTANCE hInstance, int nCmdShow);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HFONT CreateMyFont(TCHAR* FontName, int height, int lean);//创建自定义字体
//三个参数分别是字体名称、字体大小、字体的倾斜度。倾斜度以/10为一个逻辑单位
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;
if (!InitWindowClass(hInstance, nCmdShow))
{
MessageBox(NULL, L"创建窗口失败!", _T("创建窗口"), NULL);
return 1;
}
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return(int)msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hDC;
PAINTSTRUCT ps;
HFONT font;
HPEN hPen;
LPWSTR title = L"登高唐.杜甫", poem[8] = { L"风急天高猿啸哀",L"渚清沙白鸟飞回",L"无边落木萧萧下",L"不尽长江滚滚来",
L"万里悲秋常作客",L"百年多病独登台",L"艰难苦恨繁霜鬓",L"潦倒新停浊酒杯" };
int r, r0, i, j = -1, fontSize, fontSize0, color;
RECT clientDimension; //存放客户区的尺寸
POINT begin, end, org; //保存点的信息,org表示圆心坐标
double sita;
switch (message)
{
case WM_SIZE:
InvalidateRect(hWnd, NULL, true);
break;
case WM_PAINT:
hDC = BeginPaint(hWnd, &ps);
hPen = CreatePen(PS_DASH, 1, RGB(127, 127, 127));//设置画笔为系统预定义的黑色画笔
SelectObject(hDC, hPen); //选择画笔
GetClientRect(hWnd, &clientDimension);//获取客户区的尺寸
if ((clientDimension.right - clientDimension.left) < 400 || (clientDimension.bottom - clientDimension.top) < 300)
{
MessageBox(hWnd, L"屏幕尺寸太小,无法绘图!", L"错误信息", 0);
break;
}
r = (clientDimension.bottom - clientDimension.top) * 8 / 10; //用屏幕高度的/5作为扇形的半径
org.x = (clientDimension.right - clientDimension.left) / 2;
org.y = (clientDimension.bottom - clientDimension.top) * 9 / 10;
Arc(hDC, org.x - r, org.y - r, org.x + r, org.y + r, org.x + (int)(r + sin(PI / 3)), org.y - (int)(r * cos(PI / 3)),
org.x - (int)(r * sin(2 * PI / 3)), org.y + (int)(r * cos(2 * PI / 3))); //画外围圆弧
for (sita = PI / 6;sita <= PI * 5 / 6;sita += PI * 2 / 27)
{
begin.x = org.x - (int)(r * cos(sita));
begin.y = org.y - (int)(r * sin(sita));
MoveToEx(hDC, begin.x, begin.y, NULL);
end.x = org.x;
end.y = org.y;
LineTo(hDC, end.x, end.y);
} //画折线
r0 = r * 2 / 5;
Arc(hDC, org.x - r0, org.y - r0, org.x + r0, org.y + r0, org.x + (int)(r0 + sin(PI / 3)), org.y - (int)(r0 * cos(PI / 3)),
org.x - (int)(r0 * sin(2 * PI / 3)), org.y + (int)(r0 * cos(2 * PI / 3))); //画内侧圆弧
sita = PI / 6 + PI * 4 / 15 / 5;
fontSize0 = fontSize = (r - r0) / 7;
r0 = r - 20;
for (i = 0;i < 7;i++)
{
LPCWSTR outInfo = &title[i];//逐步取诗的标题字
fontSize -= 3;
font = CreateMyFont(L"楷体_GB2312", fontSize - 5, -(sita + PI / 15) * 1800 / PI); //创建字体
SelectObject(hDC, font);//将创建的字体句柄选入设备环境
begin.x = org.x + (int)(r0 * cos(sita));
begin.y = org.y - (int)(r0 * sin(sita)); //计算输出文字的坐标
TextOut(hDC, begin.x, begin.y, outInfo, 1);//输出文字
r0 -= fontSize;//文字位置由外向内移动
DeleteObject(font);
}
for (sita = PI / 6 + PI * 4 / 27 - PI / 40;sita < PI * 5 / 6;sita += PI * 2 / 27)
{
fontSize = fontSize0;
r0 = r - 20;
j++;
color = 0;
for (i = 0;i < 7;i++)
{
color += 255 / 7;
SetTextColor(hDC, RGB(255 - color, 0, color));
LPCWSTR outInfo = &poem[j][i];
fontSize -= 3;
font = CreateMyFont(L"华文行楷", fontSize, (int)(((sita - PI / 2) * 1800 / PI)) % 3600);
SelectObject(hDC, font);
begin.x = org.x + (int)(r0 * cos(sita));
begin.y = org.y - (int)(r0 * sin(sita));
TextOut(hDC, begin.x, begin.y, outInfo, 1);
r0 -= fontSize;
DeleteObject(font);
Sleep(10);//输出一个文字暂停1秒
}
}
EndPaint(hWnd, &ps);//结束绘图
break;
case WM_DESTROY:
PostQuitMessage(0);//调用PostQuitMessage发出WM_QUIT消息
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam); //默认时采用系统消息默认处理函数
break;
}
return 0;
}
HFONT CreateMyFont(TCHAR* fontName, int height, int lean)
{
return CreateFont( //创建自定义字体
height, //字体高度
0, //由系统根据高宽比选取字体最佳宽度值
lean, //文本的倾斜度为0,表示水平
0, //字体的倾斜度为0
FW_HEAVY, //字体的粗度
0, //非斜字体
0, //无下划线
0, //无删除线
GB2312_CHARSET, //表示所用的字符集为ANSI_CHARSET
OUT_DEFAULT_PRECIS, //输出精度为默认精度
CLIP_DEFAULT_PRECIS, //剪裁精度为默认值
DEFAULT_QUALITY, //输出质量为默认值
DEFAULT_PITCH | FF_DONTCARE, //字间距和字体系列使用默认值
fontName //字体名称
);
}
BOOLEAN InitWindowClass(HINSTANCE hInstance, int nCmdShow)
{
WNDCLASSEX wcex;
HWND hWnd;
TCHAR szWindowClass[] = L"窗口示例";
TCHAR szTitle[] = L"字体位置及位置示例";
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = 0;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
if (!RegisterClassEx(&wcex))
return FALSE;
hWnd = CreateWindow(
szWindowClass,
szTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL
);
if (!hWnd)
return FALSE;
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
遇到的问题如下:
具体解决方式:
在项目中找到属性,然后按照顺序,将符合模式改为“否”。
结果如下: