First, let's talk about DC (device description table)
Solution:Windows ApplicationsProgramCreate a device context table (DC) for a specified device (screen, printer, etc.) to draw a picture on the canvas of the logical meaning represented by DC. DC is a data structure that contains device information. It contains various status information required by physical devices.
. Before drawing a graph, the Win32 program needs to obtain the DC handle HDC and release it when it is not used.
Classes such as HDC, CDC, cclientdc, cpaintdc, and cwindowdc are often seen in C ++ programming.
HDC is the handle of DC, and a data type similar to pointer in API.
CDC is a class of mfc dc.
Devices such as CDC are classified up and down and contain the member variable m_nhdc; that is, the HDC type handle.
Inheritance view of CDC and Its Derived classes:
Cobject
Public | ------ CDC
Public | ------ cclientdc
Public | ------ cpaintdc
Public | ------ cwindowdc
Public | ------ cmetafiledc
(Note: The three Derived classes except cmetafiledc are used for drawing .)
The CDC class defines a class related to the device description table, which provides the member function to operate the device description table for work, such as display, printer, and display description table related window customer areas.
Connect
Member functions that have passed the CDC can perform all plotting operations. CDC provides member functions to perform basic operations on the device description table, using a drawing tool,
Select a type-safe graphical device structure (GDI), color, and color palette. In addition, it also provides member functions to obtain and set drawing properties, ing, control the view, form range, conversion coordinates, and regional operations,
Trim, strip, and draw simple graphics (oval, polygon, etc ). The member function also provides text rendering, font setting, printer Code changing, scrolling, and metadata processing.
Its derived class:
1. paintdc: encapsulate the call of beginpaint and endpaint APIs.
(1) The message (wm_paint) is used to redraw the Drawing Output in the response window.
(2) cpaintdc
Call beginpaint () in the constructor to obtain the device context, and call endpaint () in the destructor to release the device context. In addition to releasing devices
The wm_paint message is also cleared from the message queue. Therefore, you must use cpaintdc when repainting the window; otherwise, the wm_paint message cannot be cleared from the message queue.
In addition, it will cause repeated windows.
(3) cpaintdc can only be used in wm_paint message processing.
2. cclientdc (customer zone device context): process the customer region of the form related to the display description table.
It is used for the output of the customer area and is associated with a specific window to allow developers to access the customer area in the target window. Its constructor contains getdc and the Destructor contains releasedc.
3. cwindowdc: process the entire form area related to the display description table, including the frame and controller (subform ).
(1) images can be drawn in non-customer areas, while cclientdc and cpaintdc can only be drawn in customer areas.
(2) The coordinate origin is in the upper left corner of the screen, and the coordinate origin under cclientdc and cpaintdc is in the upper left corner of the customer zone.
(3) associating a specific window allows developers to draw any part of the target window, including the border and title. This DC is sent together with the wm_ncpaint message.
4. cmetafiledc: The device description table associated with the Metafile.
CDC provides two functions: getlayout and setlayout, which are used to reverse the layout of the device description table. It is used to facilitate the design of Arabic and Hebrew writing culture habits and the layout of fonts in non-European tables.
CDC
The table contains two device descriptions. m_hdc and m_hattribdc correspond to the same device. CDC specifies all output GDI calls for m_hdc.
M_hattribdc control. (For example, gettextcolor is a property call, while settextcolor is an output call .)
Below we use some simpleCodeSee if you use these classes
HDC is used, and the HDC parameters are not added to the class encapsulated by MFC for each draw line operation.
The device description table in which the operation is performed.
HDC =: getdc (m_hwnd); // m_hwnd = This-> m_hwnd is the current window handle
Movetoex (HDC, m_ptorigin.x, m_ptorigin.y, null );
Lineto (HDC, point. X, point. y );
: Releasedc (m_hwnd, HDC); // must be paired with getdc
It can be seen that HDC is troublesome to use, and if: getdc and: releasedc are not paired, it will cause an error.
CDC * PDC = getdc ();
PDC-> moveTo (m_ptorigin );
PDC-> lineto (point );
Releasedc (PDC );
Cclientdc DC (this );
DC. moveTo (m_ptorigin );
DC. lineto (point );
Cwindowdc DC (this );
Cwindowdc DC2 (getdesktopwindow (); // obtain the handle of the entire desktop.
DC. moveTo (m_ptorigin );
DC. lineto (point );
Cpaintdc DC (this );
DC. moveTo (m_ptorigin );
DC. lineto (point );
We can see that the MFC classes are much easier to use, because they call the Response Function in the constructor and destructor to obtain and release the DC.
The following describes some detailed knowledge points.
The difference between cclientdc and cwindowdc is not big. It can be said that cwindowdc contains cclientdc. Take notepad for example.
Cclientdc is just white. the area where we can edit text is the customer zone.
In addition to the White areas mentioned above, cwindowdc also includes the menu bar and toolbar.
Differences between cclientdc and cwindowdc and cpaintdc
In terms of DC acquisition, cclientdc and cwindowdc only use getdc and releasedc.
Cpaintdc only uses beginpaint and endpaint.
Cpaintdc can only be used to respond to wm_paint events
Cclientdc and cwindowdc can only be used to respond to non-wm_paint events
about the wm_paint event
System
the system sends the wm_paint message at multiple times: When a window is created for the first time, when the window is changed for an hour, when the window is removed from the back of another window, when the window is maximized or minimized
, and so on, these actions are managed by the system, and the application only passively receives the message, draw in message processing functions. In most cases, the application also needs to be able to actively draw in the window.
for example, when the data displayed in the window changes, this is generally done through the invalidaterect and invalidatergn functions. Invalidaterect and
invalidatergn Add the specified region to the update Region Window. If the application's message queue has no other messages, if the window's update
region is not empty, the system automatically generates the wm_paint message.
Why didn't the system send the message
wm_paint when invalidate is called? Why do I have to send the wm_paint message when the application message queue is empty? This is because the system regards the painting operation in the window as a low-priority operation, and
tries its best to push back. However, this will also improve the rendering efficiency: the areas where two wm_paint messages expire through invalidaterect and invaliatergn will be accumulated and updated once in a wm_paint message, this not only avoids repeated updates to the same region, but also optimizes the application update operations. For example,
invalidaterect and invalidatergn are used to invalidate the window region, and the system relies on the mechanism that the system sends the wm_paint message at the right time.
, that is to say, there is a delay between the Invalid Window area and the wm_paint message. Sometimes this delay is not what we want, in this case, you can use sendmessage to send a wm_paint message after the Invalid Window area to force re-painting immediately, but it is better to use Windows
GDI provides us with more convenient and powerful functions: updatewindow and redrawwindow. Updatewindow checks the update
region of the window and sends the wm_paint message only when it is not empty. redrawwindow gives us more control over whether to redraw non-customer regions and backgrounds, whether to always send the
wm_paint message, regardless of whether the update region is empty or not.