The Graphic Device Interface for the MFC - Practice and Text (Page 2 of 4 )
In this section, as I promised, we're going to learn how to write a particular text on the client area. Before we begin, let's clarify something. Each time you use a Device context, you should create it. This can be done by declaring a new device context that is linked to the client area you wish to use. Some of the functions that are addressed towards a message map already carry one of these control devices. As a result, in these cases you don't have to create it yourself.
These are the functions that you will obviously and explicitly use like the WM_DRAW message present in any view class under the OnDraw name. The WM_DRAW message is dispatched towards the program at any time the window’s content needs to be updated — redrawn. If you passed through the first two segments of this article series, then you should have created the MFC_example named application. In its view class (CMFC_exampleView), the OnDraw function looks like this:
void CMFC_exampleView::OnDraw(CDC* /*pDC*/)
{
CMFC_exampleDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
// TODO: add draw code for native data here
}
After this, the device creation is commented as the /*pDC*/ part, but after we remove this comment, we can use it without borders. Now we can call one of the many functions that a DC context has. For example, drawing a rectangle can be accomplished in the following line:
pDC->Rectangle( 100, 120, 400, 500);
To exemplify, we’ll create a rectangle from the upper right coordinates (100,120) and the lower right coordinates (400,500). The functions present in a DC are massive, so search for them in the MSDN help files or in an MFC bible. But if you want to use a DC in a function where it doesn’t comes so swiftly, you can declare it with the following line:
CClientDC clientDC(this);
We create a CClientDC device context that is called clientDC (you could also call it my_cat, but for standard coding and for easy understanding, this name suits it the most). This DC is assigned to the current view area. This is realized by calling it for the this pointer. The device can be initialized in other ways too, but this is the most useful because we don’t need to worry about its destruction. As a rule of C programming, all local variables are destroyed at the end of a function.
Enough said! Let’s take a look at the attached code below. It contains many new things; each of them will be covered right after this code snippet.
void CMFC_exampleView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call a // default
CClientDC clientDC(this); // we create a DC that will be used
COLORREF color = clientDC.GetTextColor();
if(color == RGB (0, 0, 0)) // if the background color is black
clientDC.SetTextColor( RGB(255,0,0)); //change it
//we set the Background of the text to transparent
//so windows will draw out only the text, ignoring the BK
clientDC.SetBkMode( TRANSPARENT );
//add some extra character depending upon X coordinates
clientDC.SetTextCharacterExtra(point.x/10);
//create a pen
int width =3;
CPen newPen (PS_SOLID, width, color);
CPen* oldPen = clientDC.SelectObject( &newPen);
//create a brush
CBrush newBrush( HS_CROSS, RGB(0,0,255) );
//select for usage the newly created Brush, replacing the old
CBrush* oldBrush = clientDC.SelectObject( &newBrush);
//Click = Dong at that coordinate
clientDC.TextOutW( point.x, point.y,CString("Dong"));
//and an ellipse just below our click
clientDC.Ellipse( point.x + 30, point.y+ 50, point.x +100,point.y + 150);
//change back tom the old Brush
clientDC.SelectObject(oldBrush);
clientDC.SelectObject(oldPen);
CView::OnLButtonDown(nFlags, point);
}
The upper code writes out Dong where we left click and an ellipse right below it. If you don’t comprehend the code, then the next paragraphs were written for you. They hold some valuable extra information.
Next: Practice and Text continued >>
More BrainDump Articles
More By Gabor Bernat