欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

字体效果

程序员文章站 2022-03-27 12:28:45
...

一、空心字

// 绘制空心字
void CFontEffectsSampleView::OnMenuKongxinFont() 
{
	// 获得窗口的客户区设备上下文句柄
	CClientDC dc(this);
	// 更改当前字体
	LOGFONT lf;
	dc.GetCurrentFont()->GetLogFont(&lf);
	CFont font;
	CFont *pOldFont; // 保存设备上下文最初使用的字体对象
	lf.lfCharSet=134;
	lf.lfHeight=-80;
	lf.lfWidth=0;
	strcpy(lf.lfFaceName, "宋体");
	font.CreateFontIndirect(&lf);
	pOldFont=dc.SelectObject(&font);
	dc.SetBkMode(TRANSPARENT);
	// 更改当前画笔
	CPen pen(PS_SOLID, 2, RGB(255, 50, 0));
	CPen *pOldPen;
	pOldPen=dc.SelectObject(&pen);
	// 开始一个路径
	dc.BeginPath();
	dc.TextOut(10, 10, "这里是空心字效果示例");
	dc.EndPath();
	// 绘制路径
	dc.StrokePath();
	// 恢复设备上下文的原有设置
	dc.SelectObject(pOldFont);
	dc.SelectObject(pOldPen);	
}





二、渐变颜色的字体

// 颜色渐变字体
void CFontEffectsSampleView::OnMenuitemJianbianFont() 
{
	// TODO: Add your command handler code here
	// 获得窗口的客户区设备上下文句柄
	CClientDC dc(this);
	// 更改当前字体
	LOGFONT lf;
    dc.GetCurrentFont()->GetLogFont(&lf);
    CFont font, *pOldFont;
    lf.lfCharSet=134;
    lf.lfHeight=-50;
    lf.lfWidth=0;
    strcpy(lf.lfFaceName, "宋体");
    font.CreateFontIndirect(&lf);
    pOldFont=dc.SelectObject(&font);
    dc.SetBkMode(TRANSPARENT);
    // 更改当前画笔为空
    CPen pen(PS_NULL, 1, RGB(0, 0, 255)), *pOldPen;
    pOldPen=dc.SelectObject(&pen);
    // 更改当前刷子
    CBrush br, *pOldBrush;
    pOldBrush=dc.SelectObject(&br);
    // 开始一个路径
    dc.BeginPath();
    dc.TextOut(10, 10, "这里是渐变颜色字体示例");
    dc.EndPath();
    // 绘制渐变效果
    dc.SelectClipPath(RGN_COPY);
    for (int i=255; i>0; i--)
    {
		int iRadius=(600*i)/255;
        dc.SelectObject(pOldBrush);
        br.DeleteObject();
        br.CreateSolidBrush(RGB(i, i, i));
        dc.SelectObject(&br);
        dc.Ellipse(-iRadius, -iRadius/3, iRadius, iRadius/3);
    }
    // 恢复设备上下文的原有设置
    dc.SelectObject(pOldFont);
    dc.SelectObject(pOldPen);
    dc.SelectObject(pOldBrush); 
}


三、文字跟随鼠标

void CFontEffectsSampleView::OnMouseMove(UINT nFlags, CPoint point) 
{
	 mousePoint.x = point.x;
	 mousePoint.y = point.y;
	
	CView::OnMouseMove(nFlags, point);
	  Invalidate();   
	// UpdateWindow();
}
void CFontEffectsSampleView::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	 dc.TextOut(mousePoint.x+3, mousePoint.y+3, "欢迎");
	//dc.TextOut(m_iXExtend,m_iYExtend,"水平文字滚动测试");
/*	CRect rc(m_iXExtend, m_iYExtend, m_iXExtend+15, m_iYExtend+150);   
	dc.DrawText("垂直文字滚动测试", &rc, DT_EDITCONTROL|DT_WORDBREAK|DT_CENTER);    */
    CView::OnPaint();
}


四、旋转字体

// 实现旋转字体
void CFontEffectsSampleView::OnMenuitemRotateFont() 
{
	// TODO: Add your command handler code here	      
	CClientDC dc(this);	// 获得窗口的客户区设备上下文句柄 
	//定义字体属性
	LOGFONT lf;
	lf.lfHeight = 50; 
	lf.lfWidth = 0; 
	lf.lfEscapement = 400; //倾斜40度,十分之一度为单位
	lf.lfOrientation = 0;
	lf.lfItalic = FALSE; 
	lf.lfUnderline = FALSE;
	lf.lfStrikeOut = FALSE;
	lf.lfCharSet = GB2312_CHARSET; 
	strcpy(lf.lfFaceName,"楷书");
	//创建字体
	CFont font;
	font.CreateFontIndirect(&lf);
	//更改当前字体
	CFont *pOldFont  = dc.SelectObject(&font);
	//绘制字体
	dc.SetBkMode(TRANSPARENT);
	dc.SetTextColor(RGB(0,255,0));
	dc.TextOut(10,300,"这里是旋转字体示例");
	//恢复设备上下文的原有设置
	dc.SelectObject(pOldFont);
}




五、文字水平滚动

void CFontEffectsSampleView::OnTimer(UINT nIDEvent) 
{
	CRect   rect;   
	GetClientRect(rect);
	//int iWidth = rect.Width();
    int iHeight = rect.Height();
	Invalidate();   
	UpdateWindow();
	m_iYExtend+=2;   
	if (m_iYExtend > iHeight)   
	{
		m_iYExtend = 0;
	}
	CView::OnTimer(nIDEvent);
}


int CFontEffectsSampleView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;	
	 SetTimer(1,   100,   NULL);   
	return 0;
}




void CFontEffectsSampleView::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	 dc.TextOut(mousePoint.x+3, mousePoint.y+3, "欢迎");
	 dc.TextOut(m_iXExtend,m_iYExtend,"水平文字滚动测试");
 	CRect rc(m_iXExtend, m_iYExtend, m_iXExtend+15, m_iYExtend+150);   
	dc.DrawText("垂直文字滚动测试", &rc, DT_EDITCONTROL|DT_WORDBREAK|DT_CENTER);    
    CView::OnPaint();
}


六、3D立体文字

创建使用的对象,并使用SetTextColor()函数分别进行高亮状态显示和阴影状态显示


//3D字体效果
void CFontEffectsSampleView::OnMenuitem3dFont() 
{
	// TODO: Add your command handler code here
	// 获得窗口的客户区设备上下文句柄
	CClientDC dc(this);
	//定义字体属性
	LOGFONT lf;
	lf.lfHeight = 50;
	lf.lfWidth = 0;
	lf.lfEscapement = 0;
	lf.lfOrientation = 0;
	lf.lfWeight = FW_HEAVY;
	lf.lfItalic = FALSE;
	lf.lfUnderline = FALSE;
	lf.lfStrikeOut = FALSE; 
	lf.lfCharSet = GB2312_CHARSET;
	strcpy(lf.lfFaceName,"隶书");
	//创建字体
	CFont font;
	font.CreateFontIndirect(&lf);
	//更改当前字体
	CFont *pOldFont  = dc.SelectObject(&font);
	//绘制字体
	dc.SetBkMode(TRANSPARENT);
	dc.SetTextColor(::GetSysColor(COLOR_3DDKSHADOW));
	CString text = "这里是3D字体示例";
	CRect rc;
	GetClientRect(&rc);
	// 开始一个路径
	dc.BeginPath();
	dc.DrawText( text, rc, DT_SINGLELINE|DT_LEFT|DT_VCENTER|DT_CENTER);
	dc.SetTextColor(::GetSysColor( COLOR_3DHILIGHT) );
	dc.DrawText( text, rc+CPoint(2, 2), DT_SINGLELINE|DT_LEFT|DT_VCENTER|DT_CENTER);
	dc.EndPath();
	// 绘制路径
	dc.StrokePath();
	dc.SelectObject(pOldFont);
}




























相关标签: 字体