Acquire the core information that will allow you to do (at least) simple painting operations in MFC.
If you are new to MFC and interested in graphic operations, you probably need a structured tutorial to teach you how do to at least basic operations. This article, although not a complete MFC graphics tutorial, is intended to provide you with the core information to allow you to do (at least) simple painting operations.
Select the object you need for the current operation and save the old selected object.
Select back the old object.
MFC provides a class called CDC that encapsulates a HDC, which is a handle to a Windows device context object. All operations with a device context should be made through such such a CDC object. This class provides functions for selecting objects, drawing lines, shapes (ellipses, polygons, rectangles), setting drawing attributes, working with regions, clipping, drawing texts, and so on.
CWindowDC: Manages a display context associated with an entire window, including its frame and controls. It calls GetWindowDC() in the constructor (to retreive a device context for the entire window) and ReleaseDC() in the destructor.
CMetaFileDC: Associates a device context with a metafile.
In this tutorial, you will use only CPaintDC.
In the following pages, you will see how to use pens, brushes, bitmaps, and fonts to draw lines, shapes, images, or text.
The first example shows how to create a simple solid red pen that's 1 pixel wide and draw a rectangle with it (by joining 4 lines). Notice that, to select the object, a call to SelectObject() is made. This can take any type of GDI object, and returns a pointer to the old object of the same type, that it just replaced. You must keep this and restore it when you no longer need the new object.
The second example shows how to create other types of pens. For all the other pens except the solid pen, only the width of 1 device unit is valid. Setting any other value will create a solid pen.
In this last example, you create a geometric pen, alternating dashes and dots, having 5 pixels width and rounded end caps.
Any of the above are created from a LOGBRUSH structure (CreateBrushIndirect).
The first example shows how to draw a rectangle with a red, solid brush. Method Rectangle() requires the brush to be selected in the device context. It also draws margin around the rectangle, using the currently selected pen (by default it's 1 pixel, solid, and black).
The second example shows how to create a solid brush from a COLORREF color or from a system color (COLOR_BTNFACE). Method FillRect draws a rectangle using the provided brush (and does not require it to be previously selected into the CDC object). Another differece from Rectangle() is that it does not draw a margin around the rectangle.
Filling with a pattern requires the use of a bitmap. You can load a bitmap from the resources and create a brush with it. Method RoundRect() draws a rectangle with rounded margins. This example uses a black and white tiles pattern.
// Create a pattern brush from the bitmap.
// Select the brush into a device context, and draw.
// Restore the original brush.
As enumerated at the beginning of this section, you can create six types of hatched brushes. Below is a sample that shows how to use them all.
Bitmaps are used to copy or scroll parts of the screen. Class CBitmap encapsulates a bitmap object and provides functionality for manipulating the bitmap.
Load the bitmap from the resources.
You can use a BITMAP object to retrieve information about the bitmap (such as width and height).
Creatie an in-memory device context, compatible with the painting device context (see CreateCompatibleDC).
Select the bitmap into this in-memory device context.
Copy a portion of the in-memory device context to the painting device context (see BitBlt).
You also can create a bitmap programatically, with CreateBitmap(). This example shows how to create a 32-bit bitmap, with 1 pixel width and 255 pixels height. The color for each pixels start from RGB(255, 255, 255) and end at RGB(0, 0, 0). This is used to create a gradient pattern brush for filling a rectangle.
This sample creates an Arial font, of 16 pixels height (the value specified to CreateFont is in logical units, so a transformation must be performed from pixels to logical units). This font is used to draw a single-line text in the middle (vertical and horizontal) of the client area of the window.
If you are doing painting in a dialog, do it in the OnPaint() method (called by the framework when Windows or an application makes a request to repaint a portion of an application's window). If you are using the doc/view architecture and want to do painting in the view, do it in the OnDraw() method (called by the framework to perform screen display, printing, and print preview, with a different device context in each case). Please refer to MSDN for more information about these methods.
If you have to do custom painting for a dialog, you should do that in OnPaint(), which is called by the framework when Windows or an application makes a request to repaint a portion of the window, in this case the dialog (you should make sure that kind of painting happens only when the window is not minimized—see IsIconic()). If you have an SDI or MDI application and need to do painting for a view, you should do it in OnDraw(), which is called by the framework to render an image of the document. Please refer to MSDN for more information about these methods.
This article provided information about basic graphic operations in MFC, such as drawing lines, filling shapes with a brush, displaying a bitmap, or drawing text. Hopefully, this can be a good start for you. In a future article, you will see more advanced operations.