Dear, take a look, look at it, newly released ... big auction!
Chapter 2, drawing in the window
Essence concentration:
The graphic output is GDI (ie, Graphic Device Interface, Graphic Devices). This is an embodiment of Windows and hardware-independent graphic output modes. GDI is built on the hardware abstraction layer (HAL), shields the difference between different output devices, providing users with a unified "standard output device". However, different from DOS, Windows is multitasking, independent, each window should have a separate output channel. In this way, GDI uses a simple mechanism to ensure that "devices" can be shared between different programs drawing in the window and do not interfere with each other. This mechanism is DC (Device Context, Device Description Table). Some people put DC asphalt painters, here there are many tools, paintings, brushes, etc.
As far as canvas, the canvas can be different, yes, I can draw on the desk (Window) on the desk (Window), you can also draw on the desktop, can you draw on the wall (tube! ^ _ ^). To this end, Windows MFC provides four different DC environments (encapsulated C classes) to indicate different drawings, namely: CPAINTDC, used to draw in the window customer area (only in the onpaint processing function); CClientdc It is also used to draw in the window customer zone (limited to ONPAINT processing functions); CWindowDC, used to draw any place within the window, including non-client; CMetafiledc, used to draw GDI element files.
These classes can be directly instantiated, such as: CPAINTDC DC (this); // This means that this DC field creates a CPAINTDC object DC for the current window. CWindowDC is generally not commonly used. If you want to draw a non-customer zone to the window, you can capture the WM_NCPAINT message with the onncpaint () handler. Just said, there is a painting, brush, etc. in the DC. These are the properties of the DC, which can be obtained by DC itself (calling its member functions). DC properties include text color, background color, mapping mode, drawing mode, current location, current brush (brush), and current fonts. Painting (PEN), brush (brush) is an independent GDI object that can be selected from the CDC member function SelectObject () to select DC. Objects in the same manner also have fonts, bitmap, palette (Palette), and region (Region). About SelectObject (): After the GDI object is created, selectObject () receives the object pointer, which returns the value of the same type of object pointer (usually used to restore DC). Windows also pre-defines some brushes, paintings, fonts, and other GDI objects. These objects are called a spare object, and use cgdiobject :: selectstockObject (). CGDiobject is a base class that represents CPEN, CFONT, and other classes of GDI objects. The properties of the alternate object can check MSDN. The brush, paintings, and other objects created by CGDiobject derived class, so you must delete them after use. The processing method is similar to other Window objects. The object created in the stack, which is automatically analyzed when this CGDIObject is out of range. CGDIObject objects created in the heap, can be explicitly deleted by calling CGDiobject :: deleteObject (this causes call to the GDI object parsing function) or with the corresponding delete operator. If it is an alternate object, it is not necessary to delete it, and it is possible to leave Windows. VC has a simple method to determine if the GDI object is successful: Just run the application's debug version in the debug state. At the end of the app, the unsealed resource is displayed in the debug window. Some knowledge points: 1. Draw mode: When GDI outputs the pixel point to the logical display plane, not just a simple output pixel color, but through a series of Boolean operations to output the color and output target position of the pixel point. The colors of the episodes are synthesized again. The logical arithmetic relationship used is determined by the DC's drawing mode. Use CDC: SETROP2 () to change the drawing mode (ROP2, Raster Operation TO). The default drawing mode is R2_copypen, and the pixel point is output directly to the display plane. Commonly used R2_NOT, used to implement Rubber technology at the time of mouse drawing.
2. Mapping mode: This is one of the properties of the DC to determine the transition from logic coordinates to device coordinates (or mapping, mapping) mode. The equipment coordinates are the corresponding pixel point position in the window. The unit is only a pixel. The logical coordinates are different from the mapping mode. If the default mm_text mode, a unit is precisely a pixel point. In mm_loenglish mode, a unit is equivalent to a length of one percent. Windows supports 8 mapping patterns, where mm_isotropic and mm_anisotropic are programmable. This means that users rather than Windows determines how to switch from logical coordinates to device coordinates. In other words, the user is from the actual size of the custom logical unit. These two modes are most commonly used to automatically adjust the output size of the drawing according to the window size. The only difference between the two is that the X direction and Y direction have the same zoom ratio factor in the X direction and Y directions. The function of setting the mapping mode is CDC :: setmapmode ().
3. Coordinate system common technology: 1) Coordinate transformation: Call CDC :: LPTODP to convert logical coordinate values to device coordinate values. Conversely, calling CDC :: DPTOLP to convert device coordinate values to logical coordinate values. These two functions will be used when the corresponding mouse click the area hit. Because the mouse clicks get the device coordinate, many GDI functions use logical coordinates, and must be converted to unified coordinates, and the operation is meaningful. 2) Original point movement: Default mode, the origin of the DC is located on the upper left corner of the corresponding display plane. The MFC provides two CDC class member functions to change the origin position. CDC :: SetWindoworg () Move Window Original, CDC :: SetViewportorg () Move View Original (see MSDN). It is generally only one of both. Original mobile technology is reflected in the frame scroll bar setting. 3) User coordinates and screen coordinates: The former is the device coordinate value set up in the upper left corner of the window client area, the latter is the origin of the device coordinate value at the upper left corner of the screen. The mutual conversion function of the two is CWnd :: ClientToscreen () and CWnd :: screenToClient (). 4.GDi Drawing Function 1) Picture: including Moveto, Lineto, Polyline, Polylineto, Arc, Arcto, Polybezier, etc. Both Polyline and Polylineto can draw multiple lines at a time, the difference is that PolylineTo uses DC's current location, while Polyline does not need. Arcto is not implemented under Win98. In addition, all GDI line functions have a common feature that never pains the last pixel point. 2) Draw a graphics: The GDI function of drawing the graph is parameter. Common functions have Rectangle, Ellipse, RoundRect, Pie, Chord, etc.
5. Brush and painting brush: In the MFC, the brush object is packaged for the CPEN class, and the painting object is packaged to the CBRUSH class. There are three ways to create both. The simplest is to construct a CPEN object and directly give the parameters to initialize. The second method is to call an uninitialized CPEN object and initialize it with Createpen (). Another way is to construct an unmelted CPEN object that generates a brush by populating the parameters describing the brush characteristics in the LOGPEN structure, then call CPEN :: CreatepenIndirect () to generate a brush. The painting brush is basically the same. 1) CPEN: Windows draws the line with the brush of the DC to select the DC and give the closed graphic strip frame. The brush has three characteristics, namely, widths, and colors. The style has PS_SOLID, PS_INSIDEFRAME, PS_NULL, and the like. PS_NULL is generally called "NULL brush", wants to draw a graphic without a border, use it. The pen width is given in a logical unit, and the actual meaning is the same as the current mapping mode. The color is determined by the RGB macro to determine the value of three independent color components to be passed to the ColorRef value of the GDI. 2) CBRUSH: The painting brush has three basic types, namely monochrome, with a shadow line and a pattern. Among them, the pattern draw brush allows the use of bitmap to fill the pattern inside (so, the window background is not fresh). Common functions have cdc :: setbkmode (), cdc :: setbkcolor (), etc.
6. Text and font: CDC has a text handler, and the important several is DrawText, TextOut, GetTextMetrics, SetTextColor, and SetTextAlign. Where GetTextMetrics () passed to the TextMetric structure about the font properties. The font is a set of characters with specific dimensions (height) and words; words indicate all attributes such as thickness. The font package is in the CFont class, build font objects, to create a GDI font resource after the CFont member function CreateFont or CreateFontIndirect is built after the CFont configuration. For fonts, there is a logfont structure that defines all features of the font. You can also create fonts by filling it. Postscript: There is a feeling that is taken by the author when writing this chapter. There are too many knowledge points, and there is a suspicion of a column or patchwork. I don't want this, but I want to think, the technology is like this. So I hope everyone can think about it, not to remember. Thinking about it, I only ask us to know the technical point, how to use it, use it, use more, remember. The essence is concentrated, as for how to open it, you can rely on yourself.