孙鑫VC学习(第5课--文档编程)

新建一个单文档的程序(Test

创建一个插入符。

int CTestView::OnCreate(LPCREATESTRUCT lpCreateStruct)

{

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

              return -1;

       CClientDC dc(this);

       TEXTMETRIC tm;

       dc.GetTextMetrics(&tm);

       CreateSolidCaret(tm.tmAveCharWidth/8,tm.tmHeight);//tm.tmAveCharWidth/8是经验

       ShowCaret( );

       // TODO: Add your specialized creation code here

       return 0;

}

下面创建一个图形的插入符:

int CTestView::OnCreate(LPCREATESTRUCT lpCreateStruct)

{

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

              return -1;

       CBitmap* pBitmap = new CBitmap;

       pBitmap->LoadBitmap(IDB_BITMAP1);

       CreateCaret(pBitmap);

       ShowCaret();

       // TODO: Add your specialized creation code here

       return 0;

}

void CTestView::OnDraw(CDC* pDC)

{

       CTestDoc* pDoc = GetDocument();

       ASSERT_VALID(pDoc);

       CString str1;

       str1.LoadString(IDS_STR1);//IDS_STR1在资源视图中的String Table中有定义

       pDC->TextOut(0,0,str1);

       // TODO: add draw code for native data here

}

下面是路径层的概念:(和轨迹相似)

void CTestView::OnDraw(CDC* pDC)

{

       CTestDoc* pDoc = GetDocument();

       ASSERT_VALID(pDoc);

       CString str1;

       str1.LoadString(IDS_STR1);//IDS_STR1在资源视图中的String Table中有定义

       CSize sizestr1=pDC->GetTextExtent(str1);

       pDC->BeginPath();

       pDC->Rectangle(CRect(0,0,sizestr1.cx,sizestr1.cy));//在路径层中做的操作是看不见的。在路径层中只能做图

       pDC->EndPath();

       pDC->TextOut(0,0,str1);

       pDC->SelectClipPath(RGN_DIFF);

       //SelectClipPath在它的矩形的范围内对下面的操作有影响。

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

       {

              pDC->MoveTo(0,i);

              pDC->LineTo(300,i);

              pDC->MoveTo(i,0);

              pDC->LineTo(i,300);

       }

       //SelectClipPathstr2有影响。

       CString str2="yyyyyyyyyy";

       pDC->TextOut(200,200,str2);

       // TODO: add draw code for native data here

}

下面是字符输入的功能:

VIEW类添加:

private:

       CString m_str;并初始化。

CPoint m_dnpt; 初始化。

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

{

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

       CClientDC dc(this);

       TEXTMETRIC  tmc;

       dc.GetTextMetrics(&tmc);

       CFont font;

    font.CreatePointFont(120, "Arial", &dc);

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

//     dc.SelectObject(def_font);

    //font.DeleteObject();

       if (0x0d==nChar)

       {

              m_str.Empty();

              m_dnpt.y+=tmc.tmHeight;

       }

       else if (0x08==nChar)//先白色输出,再用黑色输出去掉后的字符

       {

              COLORREF txprevcolor=dc.SetTextColor(dc.GetBkColor());

              dc.TextOut(m_dnpt.x,m_dnpt.y,m_str);//用白色输出字符

              m_str=m_str.Left(m_str.GetLength()-1);

              dc.SetTextColor(txprevcolor);

       }

       else

       {

              m_str+=nChar;

             

       }

       CSize size=dc.GetTextExtent(m_str);

       SetCaretPos(CPoint(size.cx+m_dnpt.x,m_dnpt.y));

       dc.TextOut(m_dnpt.x,m_dnpt.y,m_str);

       dc.SelectObject(def_font);

    font.DeleteObject();

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

}

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

{

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

       //将插入符移到左键位置

        SetCaretPos( point );

        m_str.Empty();

        m_dnpt=point;

       CView::OnLButtonDown(nFlags, point);

}

下面模拟卡拉OK字幕变色功能:

VIEWONCREATE函数添加:SetTimer(1,1000,NULL);

VIEW类增加:int m_nwidth; CString m_colstr;(初始化为0

ONDRAW函数中添加:

       m_colstr="模仿卡拉OK的变色功能";

    pDC->TextOut(300,300,m_colstr);

void CTestView::OnTimer(UINT nIDEvent)

{

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

       CClientDC dc(this);

       TEXTMETRIC textm;

       dc.GetTextMetrics(&textm);

 

       static CRect rect;

       rect.left=300;

       rect.top=300;

       rect.bottom=300+textm.tmHeight;

       rect.right=300+m_nwidth;

       if (m_nwidth>=dc.GetTextExtent(m_colstr).cx)

       {

              m_nwidth=0;

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

       }

       else

       {

        m_nwidth+=5;

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

       }

       dc.DrawText(m_colstr,rect,DT_CENTER);

       CView::OnTimer(nIDEvent);

}

你可能感兴趣的:(孙鑫VC学习(第5课--文档编程))