MFC Window Programming (second edition) essence notes (2)

Source: Internet
Author: User
Tags polyline

Ladies and gentlemen, let's take a look at the new big auction!

Chapter 2. Drawing in a window

Essence concentration:

In Windows, GDI (Graphic device interface and graphical device interface) is used to output images ). This is an embodiment of Windows's Hardware-independent graphic output mode. Based on the hardware abstraction layer (HAL), GDI shields the differences between different output devices and provides users with a unified "standard output device ". However, unlike dos, Windows has multiple tasks and independent processes. Each window should have an independent output channel. In this way, GDI uses a simple mechanism to ensure that different programs drawing in the window can share "devices" without interfering with each other. This mechanism is DC (Device
Context, device description table ).
Someone compared DC to a painter's studio. There are many tools such as canvas, paint brush, and paint brush.

In terms of the canvas, the canvas format can be different. Yes, I can draw on windows on the desktop or on the desktop, you can still draw the picture on the wall! ^_^ ).
Therefore, Windows MFC provides four different DC environments (encapsulated as C ++ classes) to indicate different drawing permissions, namely:
Cpaintdc, used for drawing in the customer area of the window (only used in the onpaint processing function );
Cclientdc is also used for drawing in the customer area of the window (only used outside the onpaint processing function );
Cwindowdc, used for drawing any place in the window, including non-customer areas;
Cmetafiledc, used to draw a GDI Metafile.

These classes can be directly instantiated, such:
Cpaintdc DC (this );//This indicates that the current DC window is
A cpaintdc object DC is created.
Cwindowdc is not commonly used. To draw a picture in a non-customer area of a window, you can use the onncpaint () processing function to capture the wm_ncpaint message.

As I said just now, there are also paint brushes and brushes in DC. These are the attributes of DC, which can be obtained through DC itself (calling its member functions.
DC attributes include text color, background color, ing mode, drawing mode, current position, current paint brush, and current font.
Pen and brush are independent GDI objects. You can use the SelectObject () function of the CDC member function to select DC. Objects in the same operation method include font, bitmap, palette, and region ).
About SelectObject (): After a GDI object is created, SelectObject () receives the pointer of this object. The returned value is the same type of object pointer of the DC (usually used to restore the DC ).
Windows also pre-defines some paint brushes, paint brushes, fonts, and other GDI objects. These objects are called slave objects and are selected using cgdiobject: selectstockobject. Cgdiobject is the base class that represents the cpen, cfont, and other classes of the GDI object. You can check msdn for the description of the attributes of the standby object.
The paint brushes, paint brushes, and other objects created by the cgdiobject derived class occupy memory resources. Therefore, you must delete them after use. The processing method is similar to that of other window objects. The object created in the stack. When this cgdiobject is out of the range, it is automatically destructed. You can call cgdiobject: deleteobject to explicitly delete a newly created cgdiobject In the heap (this will cause a call to the GDI object destructor) or use the corresponding Delete operator. If it is a backup object, you do not need to delete it. Leave it to Windows.
VC ++ has a simple method to determine whether the GDI object is successfully deleted: you only need to run the debug version of the application in the debugging state. When the application ends, resources that have not been released are displayed in the debugging window.


Some knowledge points:

1. drawing mode: When GDI outputs pixel points to the logical display plane, it is not just a simple output of pixel colors, instead, the color of the output pixel point and the color of the pixel point at the output target position are synthesized and then displayed through a series of Boolean operations. The logical operation relationships used are determined by the drawing mode of DC. Use CDC: setrop2 () to change the drawing mode (ROP2, raster operation ). The default drawing mode is r2_copypen, which directly outputs pixel points to the display plane. There are also commonly used r2_not, which is used to implement the rubber technology when drawing the mouse.

2. ing mode: This is one of DC attributes and is used to determine the conversion (OR Mapping) mode from logical coordinates to device coordinates. The device coordinate refers to the corresponding pixel position in the window. The Unit is only pixel. The logical coordinates vary according to the ing mode. For example, in the default mm_text mode, a unit is exactly a pixel point. In mm_loenglish mode, a unit is equivalent to 1 inch long. Windows supports eight ing modes, in which mm_isotropic and mm_anisotropic are programmable. This means that the conversion from logical coordinates to device coordinates is determined by the user rather than by windows. In other words, it is the actual size of the custom logic unit of the user. These two modes are most commonly used when the Drawing Output size is automatically adjusted proportionally according to the window size. The only difference between the two is that the X and Y directions in the former have the same scaling factor. The function used to set the ing mode is CDC: setmapmode ().

3. Common coordinate system technologies:
1) Coordinate Transformation: call CDC: lptodp to convert the logical coordinate value to the device coordinate value. Otherwise, you can call CDC: dptolp to convert the device coordinate value to the logical coordinate value. The two functions are used when the mouse clicks the area to hit the test. Because the device coordinates are obtained by clicking the mouse, many GDI functions use logical coordinates and must be converted to uniform coordinates for the operation to make sense.
2) origin movement: by default, the DC origin is located in the upper left corner of the corresponding display plane. MFC provides two member functions of the CDC class to change the origin location. CDC: setjavasworg () Move the origin of the window, CDC: setviewportorg () Move the origin of the view (see msdn for details ). Generally, only one of the two can be used. The origin mobile technology is embodied in the border scroll bar settings.
3) User coordinates and screen coordinates: the former is the device coordinate value in the upper left corner of the window customer area, and the latter is the device coordinate value in the upper left corner of the screen at the origin. The functions used for mutual conversion are cwnd: clienttoscreen () and cwnd: screentoclient ().

4. GDI plotting Functions
1) Draw line: Includes moveTo, lineto, polyline, polylineto, arc, arcto, polybetiller, etc. Both polyline and polylineto can draw multiple lines at a time. The difference between the two is that polylineto uses the current location of DC, while polyline does not. Arcto is not implemented in Win98. In addition, all GDI draw functions share a common feature that never draws the last pixel point.
2) drawing: the coordinates of the rectangle connected beyond the GDI function of the closed image are parameters. Common functions include rectangle, ellipse, roundrect, pie, and chord.

5. Paint Brush and paint brush: In MFC, the paint brush object is encapsulated as the cpen class, and the paint brush object is encapsulated as the cbrush class. There are three methods to create both. The simplest thing is to construct a cpen object and directly give the parameters used for initialization. The second method is to call an uninitialized cpen object and initialize it with createpen. Another way is to construct an uninitialized cpen object. by filling in the logpen structure with parameters describing the paint brush feature, call cpen: createpenindirect () to generate a paint brush. Paint brushes are basically the same.
1) cpen: in windows, draw a line with the current DC paint brush and set a border for the closed image. A paint brush has three features: style, width, and color. Styles include ps_solid, ps_insideframe, and ps_null. Ps_null is generally called "null paint brush". It is used to draw a graph without borders. The pen width is given in logical units. The actual meaning is the same as the current ing mode. The color is determined by combining the values of the three independent color components into a colorref value that can be passed to GDI through the RGB macro.
2) cbrush: There are three basic types of paint brushes: monochrome, Shadow Line, and pattern. The Pattern Painting Brush allows the use of Bitmap to fill the image (in this way, the window background will not be fresh ). Common functions are:
CDC: setbkmode (), CDC: setbkcolor (), etc.

6. Text and Font: CDC has a set of text processing functions, including drawtext, textout, gettextmetrics, settextcolor, and settextalign. Gettextmetrics () is used to send information about the font properties of the textmetric structure. A font is a group of characters with a specific size (height) and font. The font indicates that the font has common attributes, such as width. The font is encapsulated in the cfont class. To create a font object, call the cfont member function createfont or createfontindirect after the cfont is constructed. For fonts, there is a logfont structure, which defines all the features of the font. You can also fill it in to create a font.

Note: there is a feeling of being led by the author when writing this chapter. There are too many knowledge points, and there is a suspicion of listing or piecing together. I don't want this, but think about it, that's the technology. So I hope you can think about it, instead of remembering it. Thinking, we only need to know the technical points, how to use them, how to use them, and how to use them.The essence is concentratedAs for how to turn it out, it is up to you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.