掌握CDC的文字处理程序的编写,如何产生自定义字体和自定义插入符,熟悉对CString类的使用。通过对卡拉OK程序的编写,讲解定时器的使用和DrawText函数的巧妙运用。讲解如何使用CDC的裁减功能。
/////////////////////////////////////////////////////////////////////////////
// CTextView
IMPLEMENT_DYNCREATE(CTextView, CView)
BEGIN_MESSAGE_MAP(CTextView, CView)
//{{AFX_MSG_MAP(CTextView)
ON_WM_CREATE()
ON_WM_CHAR()
ON_WM_LBUTTONDOWN()
ON_WM_TIMER()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTextView construction/destruction
CTextView::CTextView()
{
// TODO: add construction code here
m_strLine="";
m_ptOrigin=0;
m_nWidth=0;
}
CTextView::~CTextView()
{
}
BOOL CTextView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CTextView drawing
void CTextView::OnDraw(CDC* pDC)
{
CTextDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
//CString str("维新科学技术培训中心");
CString str; //字符串的类 CString ,无论字符多长,多不需要对类进行重新内存分配
str="维新科学技术培训中心";
pDC->TextOut(50,50,str);
CSize sz=pDC->GetTextExtent(str); //返回一个CSize对象,获取特定字符串在屏幕上显示的宽度和高度
str.LoadString(IDS_WEIXIN); //字符串资源
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);
}
}
/////////////////////////////////////////////////////////////////////////////
// CTextView printing
BOOL CTextView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CTextView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CTextView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CTextView diagnostics
#ifdef _DEBUG
void CTextView::AssertValid() const
{
CView::AssertValid();
}
void CTextView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CTextDoc* CTextView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTextDoc)));
return (CTextDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CTextView message handlers
int CTextView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
CClientDC dc(this); // dc设备描述表
TEXTMETRIC tm;
dc.GetTextMetrics(&tm); //获取字体的度量信息
//
CreateSolidCaret(tm.tmAveCharWidth/8,tm.tmHeight); //创建一个一般的插入符
//CBitmap bitmap; //要放在头文件中 *****局部的对象 当oncreate执行完成之后会发生析构,发生析构的时候,会把变量销毁
bitmap.LoadBitmap(IDB_BITMAP1);
CreateCaret(&bitmap); //创建一个图形的插入符
ShowCaret();
SetTimer(1,100,NULL); //设置定时器
return 0;
}
void CTextView::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.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;
}
CSize sz=dc.GetTextExtent(m_strLine);
CPoint pt;
pt.x=m_ptOrigin.x+sz.cx;
pt.y=m_ptOrigin.y;
SetCaretPos(pt); //把鼠标移动到这个店
dc.TextOut(m_ptOrigin.x,m_ptOrigin.y,m_strLine);
dc.SelectObject(pOldFont); //选择字体
CView::OnChar(nChar, nRepCnt, nFlags);
}
void CTextView::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);
}
void CTextView::OnTimer(UINT nIDEvent) //nIDEvent定时器的标示
{
// TODO: Add your message handler code here and/or call default
m_nWidth+=5;
CClientDC dc(this);
TEXTMETRIC tm;
dc.GetTextMetrics(&tm);
CRect rect;
rect.left=0;
rect.top=200;
rect.right=m_nWidth;
rect.bottom=rect.top+tm.tmHeight;
dc.SetTextColor(RGB(255,0,0));
CString str;
str.LoadString(IDS_WEIXIN);
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_nWidth>sz.cx)
{
m_nWidth=0;
dc.SetTextColor(RGB(0,255,0));
dc.TextOut(0,200,str);
}
CView::OnTimer(nIDEvent);
}