文本编程

来源:互联网 发布:windows激活不可用 编辑:程序博客网 时间:2024/04/29 07:26
1.插入符:
插入符随着输入字体的大小变大变小。
int CTextView::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);
CreateSolidCaret(tm.tmAveCharWidth/8,tm.tmHeight);
ShowCaret();
return 0;
}


2.创建位图的插入符:
int CTextView::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);
// CreateSolidCaret(tm.tmAveCharWidth/8,tm.tmHeight);


bitmap.LoadBitmap(IDB_BITMAP1);
CreateCaret(&bitmap);
ShowCaret();
return 0;
}
我定义的bitmap是一个局部的对象,当create对象执行完成之后,bitmap这个对象要发生析构。通常C++的对象是和资源相关的对象,在它发生析构的时候,它都会把它相关联的资源销毁,这样我们就看不到“位图的插入符”了。我们可以把局部变量bitmap,编程CTextView的成员变量,剪切CBitmap bitmap;到CTextView的头文件当中


3.LoadString:
它的好处就是,我们什么时候想要用到这个字符串的时候,就可以直接用LoadString去加载这个字符串资源就可以不需要每次都进行赋值了。
在VC++这个开发环境当中,我们应该在哪去定义这个字符串资源?
resource->String Table 当中 


void CTextView::OnDraw(CDC* pDC)
{
CTextDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here


CString str;
str="维新科学技术培训中心";
pDC->TextOut(50,50,str);
 
str.LoadString(IDS_WEIXIN);
pDC->TextOut(0,200,str);
}


4.路径城:


void CTextView::OnDraw(CDC* pDC)
{
CTextDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here


CString str;
str="维新科学技术培训中心";
pDC->TextOut(50,50,str);
 
CSize sz=pDC->GetTextExtent(str);


str.LoadString(IDS_WEIXIN);
pDC->TextOut(0,200,str);


pDC->BeginPath();
pDC->Rectangle(50,50,50+sz.cx,50+sz.cy);
pDC->EndPath();
}


5.画网格状线条:
void CTextView::OnDraw(CDC* pDC)
{
CTextDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here


CString str;
str="维新科学技术培训中心";
pDC->TextOut(50,50,str);
 
CSize sz=pDC->GetTextExtent(str);


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); //画网格
}
}


6.字符输入的功能:
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);//利用textout将文字擦除掉
        m_strLine=m_strLine.Left(m_strLine.GetLength()-1);//利用CString内部成员函数left去掉一个字符
dc.SetTextColor(clr);//然后再将它的文本颜色设置成先前的文字的颜色
}
else  
{
m_strLine+=nChar; //把每个字符都加到m_strLine中
}
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);
}


7.模拟KTV歌曲字幕变色功能:
在CTextView::OnCreate中定义定时器:SetTimer(1,100,NULL); 
void CTextView::OnTimer(UINT nIDEvent) 
{
// TODO: Add your message handler code here and/or call default
m_nWidth+=5; //按5个像素点增加


CClientDC dc(this);


TEXTMETRIC tm;
dc.GetTextMetrics(&tm);
CRect rect; //定义一个矩形
//这个矩形呢,因为这个变量在不断增加,每隔100毫秒会发送一个WH_Timer消息
//OnTimer不断被调用,m_nWidth的值不断的增加,因此矩形的宽度在不断增加
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) //如果宽度已经大于cx
{
m_nWidth=0;//将它清零
dc.SetTextColor(RGB(0,255,0));//设置文本的颜色
dc.TextOut(0,200,str);
}




CView::OnTimer(nIDEvent);
}