The Graphic Device Interface for the MFC - Practice and Text continued (Page 3 of 4 )
MFC has a few classes that are very useful. For example, look at the CString class. It is just like STL’s string. It replaces the char arrays, but this allocates its memory dynamically. It has some additional advantages also. But going into the depths of it is, unfortunately, beyond the purpose of this article.
Notwithstanding, let’s start from the beginning. At first, we create the necessary DC to allow us to draw to the client area. This is followed by declaring a 24-bit color to be the current text color. Now, if that is black — the default — then we will change it to red. The RGB color is made out of the three basic colors, using the following scheme: RedGreenBlue (Red amount, Green amount, Blue amount).
The amount can be a value between 0 and 255. Now the RGB (0, 0, 0) stands for the black color, while the RGB (255, 255, 255) represents the white. For a text, the background can be either TRANSPARENT or OPAQUE. The transparent attribute ignores the background color and only the text itself will be drawn.
There are many other really advanced text functions and as a foretaste, I’m presenting you the Spacing one. Basically, it adds extra spaces between the characters. As for the text part, we’ll only need to call the TextOutW (only TextOut for Visual Studios prior to the .Net platform). The first two parameters are the starting coordinates from where the code will be drawn, while the third parameter holds the content. The text is in a CString type variable.
I’ve added some other dedicates. The CPen class is for the line style along with what the DC is drawing to the client area. The CBrush holds the pattern, which helps fill in the contour. The constructors each have a style and a color parameter, then there's the CPen and extra width parameter. Here I showed you how you can replace the customized settings.
The SelectObject function replaces the assigned object and returns a pointer to the old object. Now if we choose to save this, we can later make a switch back to the old settings so that when we are writing a program, we always have the default settings. This way we might prevent those irritating situations where we are expecting one thing, but thanks to some of our changes at a different place in the code, we are dealing with a different situation.
Just another remark: since we have written this code on the OnLButtonDown section and not in the OnDraw function, when the window requires a redraw (e.g.; minimizing the application), the previous image in its entirety will disappear. To solve this problem we should implement a saving/serializing algorithm and make the drawing part in the OnDraw function.
We can force a redraw of the client area by calling the Invalidate() function first. This will result in the repainting of the client area at the next WM_PAINT message. Now we will also send a message, specifically a WM_PAINT one. We can individually send any message, but think about the results before you do. The combo will look like this:
Invalidate ();
const char *Msg = "The sent message";
SendMessage(WM_PAINT, 0, (LPARAM)(LPCTSTR)Msg);
Next: Conclusion >>
More BrainDump Articles
More By Gabor Bernat