VC程序设计--文字输出方法与字体示例

在用户窗口上输出一个扇形,并在扇面竖向输出一首唐诗.本例使用绝对定位确定输出文字的位置,并采用多种自定义字体输出文字。

// poemDemo.cpp : 定义应用程序的入口点。
//

#include "stdafx.h"
#include "poemDemo.h"
#include "math.h"

#define MAX_LOADSTRING 100

#define PI 3.1415926

// 全局变量:
HINSTANCE hInst;								// 当前实例
TCHAR szTitle[MAX_LOADSTRING];					// 标题栏文本
TCHAR szWindowClass[MAX_LOADSTRING];			// 主窗口类名

// 此代码模块中包含的函数的前向声明:
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK	About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);

 	// TODO: 在此放置代码。
	MSG msg;
	HACCEL hAccelTable;

	// 初始化全局字符串
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_POEMDEMO, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

	// 执行应用程序初始化:
	if (!InitInstance (hInstance, nCmdShow))
	{
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_POEMDEMO));

	// 主消息循环:
	while (GetMessage(&msg, NULL, 0, 0))
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	return (int) msg.wParam;
}



//
//  函数: MyRegisterClass()
//
//  目的: 注册窗口类。
//
//  注释:
//
//    仅当希望
//    此代码与添加到 Windows 95 中的“RegisterClassEx”
//    函数之前的 Win32 系统兼容时,才需要此函数及其用法。调用此函数十分重要,
//    这样应用程序就可以获得关联的
//    “格式正确的”小图标。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX);

	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_POEMDEMO));
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= MAKEINTRESOURCE(IDC_POEMDEMO);
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

	return RegisterClassEx(&wcex);
}

//
//   函数: InitInstance(HINSTANCE, int)
//
//   目的: 保存实例句柄并创建主窗口
//
//   注释:
//
//        在此函数中,我们在全局变量中保存实例句柄并
//        创建和显示主程序窗口。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // 将实例句柄存储在全局变量中

   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

HFONT CreateMyFont(TCHAR *fontName,int height,int lean);
//
//  函数: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  目的: 处理主窗口的消息。
//
//  WM_COMMAND	- 处理应用程序菜单
//  WM_PAINT	- 绘制主窗口
//  WM_DESTROY	- 发送退出消息并返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;

	HDC hDC;
	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_COMMAND:
		wmId    = LOWORD(wParam);
		wmEvent = HIWORD(wParam);
		// 分析菜单选择:
		switch (wmId)
		{
		case IDM_ABOUT:
			DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
			break;
		case IDM_EXIT:
			DestroyWindow(hWnd);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
		}
		break;
	case WM_PAINT:
		hDC = BeginPaint(hWnd, &ps);
		// TODO: 在此添加任意绘图代码...
		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);
			EndPaint(hWnd,&ps); 
			break;	
		}
		r=(clientDimension.bottom-clientDimension.top)*8/10; //用屏幕高度的/5作为扇形的半径
		org.x=(clientDimension.right-clientDimension.left)/2;
		org.y=(clientDimension.bottom-clientDimension.top)*9/10;	
		//将圆心坐标定在屏幕中间向下的/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);	
		} 
	}
		EndPaint(hWnd, &ps);
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

HFONT CreateMyFont(TCHAR *fontName,int height,int lean)
{return CreateFont        //创建自定义字体
(	height,    //字体的高度
	0, //由系统根据高宽比选取字体最佳宽度值
	lean, //文本的倾斜度为,表示水平
	0,		//字体的倾斜度为
	FW_HEAVY,			
	0,					//非斜体字
	0,					//无下划线
	0,					//无删除线
	GB2312_CHARSET, //表示所用的字符集为ANSI_CHARSET
	OUT_DEFAULT_PRECIS,	//输出精度为默认精度
	CLIP_DEFAULT_PRECIS,	//剪裁精度为默认精度
	DEFAULT_QUALITY,		//输出质量为默认值
	DEFAULT_PITCH|FF_DONTCARE,
//字间距和字体系列使用默认值
	fontName				//字体名称
	);}

// “关于”框的消息处理程序。
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	UNREFERENCED_PARAMETER(lParam);
	switch (message)
	{
	case WM_INITDIALOG:
		return (INT_PTR)TRUE;

	case WM_COMMAND:
		if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
		{
			EndDialog(hDlg, LOWORD(wParam));
			return (INT_PTR)TRUE;
		}
		break;
	}
	return (INT_PTR)FALSE;
}
原文地址:https://www.cnblogs.com/zw1sh/p/12456560.html