字体处理

来源:互联网 发布:五轴编程视频 编辑:程序博客网 时间:2024/04/30 23:17

边学边想,感觉真的不错!

下面将为大家介绍有关c++里面的字体处理,只列出了view里面的相关函数:

需添加的全局变量

private:

int m_mWidth;

CPoint m_ptOrigin;

CString m_strLine;

CBitmap bitMap;

全局变量的初始化

CTest2View::CTest2View()

{

// TODO: add construction code here

m_strLine="";

m_ptOrigin=0;

m_mWidth=0;

}

onDraw消息队列

void CTest2View::OnDraw(CDC* pDC)

{

CTest2Doc* pDoc = GetDocument();

ASSERT_VALID(pDoc);

// TODO: add draw code for native data here

//CString str("我的C++字形处理");

CString str;

str="我的C++字形处理";//因为Ctring有字符串的等号重载

pDC->TextOut(50,50,str);

CSize sz=pDC->GetTextExtent(str);//获取字符串的长度

str.LoadString(String_Test);//String_Test为rs中创建的字符串ID

pDC->TextOut(0,200,str);

pDC->BeginPath();//开始路径层

pDC->Rectangle(50,50,50+sz.cx,50+sz.cy);//在路径层绘制矩形

pDC->EndPath();//结束路径层

pDC->SelectClipPath(RGN_DIFF);

for(int i=0;i<300;i+=10)

{

pDC->MoveTo(0,i);

pDC->LineTo(300,i);

pDC->MoveTo(i,0);

pDC->LineTo(i,300);

}

}

onCreate消息队列

int CTest2View::OnCreate(LPCREATESTRUCT lpCreateStruct)

{

if (CView::OnCreate(lpCreateStruct) == -1)

return -1;

// TODO: Add your specialized creation code here

CClientDC dc(this);//获得设备描述表

TEXTMETRIC tm;//字体的大小信息

dc.GetTextMetrics(&tm);

bitMap.LoadBitmap(IDB_BITMAP1);//位图必须声明为全局变量,因为onCreate运行完之后局部变量会消失

CreateCaret(&bitMap);

// CreateSolidCaret(tm.tmAveCharWidth/8,tm.tmHeight);//创建插入符

ShowCaret();//显示插入符

//创建时设置定时器

SetTimer(1,100,NULL);

return 0;

}

OnChar消息队列

void CTest2View::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)

{

// TODO: Add your message handler code here and/or call default

CClientDC dc(this);

//设置字体的属性

CFont font;

font.CreatePointFont(300,"华文行楷",NULL);

CFont *pOldFont=dc.SelectObject(&font);

TEXTMETRIC tm;

dc.GetTextMetrics(&tm);

if (0x0d==nChar)//判断是否是回车

{

m_strLine.Empty();

m_ptOrigin.y+=tm.tmHeight;

}

else if (0x08==nChar)

{

COLORREF clr=dc.SetTextColor(dc.GetBkColor());//设置字体的信息dc.getBKColor为获得当前的背景色

dc.TextOut(m_ptOrigin.x,m_ptOrigin.y,m_strLine);

m_strLine=m_strLine.Left(m_strLine.GetLength()-1);//删除时减去字符串最左的字符

dc.SetTextColor(clr);//将字体的设置会之前的颜色

}

else

{

m_strLine+=nChar;

}

dc.TextOut(m_ptOrigin.x,m_ptOrigin.y,m_strLine);

CSize sz=dc.GetTextExtent(m_strLine);//获得字符串的宽度

CPoint pt;

pt.x=m_ptOrigin.x+sz.cx;//插入符的x坐标

pt.y=m_ptOrigin.y;//插入符的y坐标

SetCaretPos(pt);//设置插入符的位置,总体的效果是实现插入符在输入字体的后面

dc.SelectObject(pOldFont);

CView::OnChar(nChar, nRepCnt, nFlags);

}

OnLButtonDown消息队列

void CTest2View::OnLButtonDown(UINT nFlags, CPoint point)

{

// TODO: Add your message handler code here and/or call default

SetCaretPos(point);//指示输入符的位置

m_strLine.Empty();//清空输入字符

m_ptOrigin=point;

CView::OnLButtonDown(nFlags, point);

}

OnTimer消息队列

void CTest2View::OnTimer(UINT nIDEvent)

{

// TODO: Add your message handler code here and/or call default

m_mWidth+=5;//已显示的宽度

CClientDC dc(this);

TEXTMETRIC tm;

dc.GetTextMetrics(&tm);

CRect rect;

rect.left=0;

rect.top=200;

rect.right=m_mWidth;

rect.bottom=rect.top+tm.tmHeight;

dc.SetTextColor(RGB(255,0,0));

CString str;

str.LoadString(String_Test);

dc.DrawText(str,rect,DT_LEFT);//为字符串加色

rect.top=150;

rect.bottom=rect.top+tm.tmHeight;

dc.DrawText(str,rect,DT_RIGHT);

CSize sz=dc.GetTextExtent(str);

//字符串输出完毕是换成其他的颜色

if(m_mWidth>sz.cx)

{

m_mWidth=0;

dc.SetTextColor(RGB(0,255,0));

dc.TextOut(0,200,str);

}

CView::OnTimer(nIDEvent);

}