Sunday, June 2, 2013

Drawing Text in MFC

By default, the CDC class is able to draw text using a font pre-selected, known as the System Font. To draw text, we can use the CDC::TextOut() method. Its syntax is :

 virtual BOOL TesxtOut(int x, int y, LPCTSTR lpszString, int nCount);

To use this method, we must specify where the text would start. This location is determined from the (0,0) origin to the right (x) and to the bottom (y). The text to display is the lpszString. The nCount value is the length of text. Here is an example :

void CExoView::OnDraw(CDC *pDC)
{
      CExoDoc *pDoc = GetDocument();
      ASSERT_VALID(pDoc);
   
     pDC->TextOut(50,42,"Hellow Poipo",12);
}

If we want to control the color used to draw the text, use the CDC::SetTextColor() method whose syntax is :

virtual COLORREF SetTextColor(COLORREF crColor);

The argument can be provided as a COLORREF variable or by calling the RGB macro. Here the example :

void CExoView::OnDraw(CDC *pDC)
{
      CExoDoc *pDoc = GetDocument();
      ASSERT_VALID(pDoc);

     pDC->SetTextColor(RGB(255,25,2));
    pDC->TextOut(50,42,"Hellow Poipo",12);
}

No comments:

Post a Comment