当前位置:C++技术网 > 资讯 > 文字特效处理之旋转字体

文字特效处理之旋转字体

更新时间:2015-12-06 21:54:55浏览次数:1+次

利用GDI+实现坐标变换的最主要特点就是引入了对矩阵对象的支持,矩阵是变换的核心。没错就是线性代数里的矩阵!图形变换的一些基本概念:

1,平移变换(TranslateTransform)
2,旋转变换(RotateTransform)
3,缩放变换(ScaleTransform)
GDI实现文本的旋转输出着重点是基于字体信息本身,而与具体的坐标位置无关。这种文本输出的特效在GDI中主要是通过构造新字来实现的,因为在LOGFONT数据结构中的数据成员lfEscapement可以指定基线的方向。GDI+中改变了这种思路,文本的旋转输出主要体现在绘图平面坐标系本身的旋转。

新建工程,加入GDI+程序必备的代码,你可以在之前我写的文章去找,有介绍。下面新建一个菜单项“GDI+学习”,新建一个子菜单项“旋转字体”,建立消息响应

void C旋转字体View::OnFontrotate()
{
	// TODO: 在此添加命令处理程序代码
	Graphics graphics(this->m_hWnd);
	this->RedrawWindow();
	FontFamily fontFamily(L"宋体");
	Gdiplus::Font myFont(&fontFamily,20,FontStyleRegular,UnitPixel);
	SolidBrush redBrush(Color::Red);
	/////获取当前窗口大小
	CRect rect;
	this->GetClientRect(&rect);
	RectF layoutRect(0,0,rect.Width()/2,rect.Height()/2);
	StringFormat format;
	//////设置当前文本输出格式
	format.SetAlignment(StringAlignmentNear);/////水平
	format.SetLineAlignment(StringAlignmentNear);//////垂直
	//////将文本的输出起点设置成窗口的中心
	graphics.TranslateTransform(layoutRect.Width,layoutRect.Height);
	///////在旋转时每隔30°输出文本
	for(int i=0; i<360; i+=30)
	{
		graphics.RotateTransform(i);
		graphics.DrawString(L"旋转字体",-1,&myFont,layoutRect,&format,&redBrush);
		//////恢复所做的旋转
		graphics.RotateTransform(-i);
	}
}
效果图:

 

上述代码中的函数SetAlignment,SetLineAlignment的参数(水平对齐和垂直对齐方式)都是默认值,如果对这两个函数进行修改,就会得到不同的文本输出效果。如下面的代码实现图就是更改了SetLineAlignment(StringAlignmentCenter)函数而实现的。

void C旋转字体View::OnFontrotate()
{
	// TODO: 在此添加命令处理程序代码
	Graphics graphics(this->m_hWnd);
	this->RedrawWindow();
	FontFamily fontFamily(L"宋体");
	Gdiplus::Font myFont(&fontFamily,20,FontStyleRegular,UnitPixel);
	SolidBrush redBrush(Color::Red);
	/////获取当前窗口大小
	CRect rect;
	this->GetClientRect(&rect);
	RectF layoutRect(0,0,rect.Width()/2,rect.Height()/2);
	StringFormat format;
	//////设置当前文本输出格式
	format.SetAlignment(StringAlignmentCenter);/////水平
	format.SetLineAlignment(StringAlignmentCenter);//////垂直
	//////将文本的输出起点设置成窗口的中心
	graphics.TranslateTransform(layoutRect.Width,layoutRect.Height);
	///////在旋转时每隔30°输出文本
	for(int i=0; i<360; i+=30)
	{
		graphics.RotateTransform(i);
		graphics.DrawString(L"旋转字体",-1,&myFont,layoutRect,&format,&redBrush);
		//////恢复所做的旋转
		graphics.RotateTransform(-i);
	}
}

后面我将写篇详细介绍的文章,方便你理解