GDI drawing in MFC (1)

Source: Internet
Author: User
Tags message queue

A About the basic concepts of GDI

What is GDI

The essence of Windows drawing is to take advantage of the graphics device interface provided by Windows GDI(Graphics devicesInterface ) to draw the graphic on the display.

In the WINDOWS operating system, GDI functions are defined in the dynamic-link library C:\WINDOWS\system32\gdi32.dll(GDI Client DLL) to implement device-independent including on-screen output pixels, Prints hard copies on the printer and draws Windows user interface features. header files in Visual c++6.0 C:\Program Files\Microsoft Visual Studio\vc98\include\wingdi.h and header file C in Visual Studio 2005 : \program Files\Microsoft Visual Studio 8\vc\platformsdk\include\WinGDI.h is the key to access Gdi32.dll library files. Let's take a general look at the wingdi.h(included in Windows.h) header file:

/ * Bitmap Header definition * /defines the BITMAP bitmap structure

/ * Mapping Modes * * defines the way coordinates are mapped in a DC, including the following common functions:

Setmapmode, Setviewportextex, Setviewportorgex, Setwindowextex, Setwindoworgex.

/ * Stock Logical Objects * /System pre-defined heap (stock) objects, including brush, pen, and font objects

/ * Brush Styles * * defines the brush format, including solid, hollow, hatched and other formats

/ * Hatch Styles * * defines the brush shadow format, including:

Hs_vertical/* | | | | | * *

Hs_fdiagonal/ * \\\\\ * /

Hs_bdiagonal/ */////* /

Hs_cross/ * +++++ * /

Hs_diagcross/ * xxxxx * /

/ * Pen Styles * * defines the brush format, including solid, DASH, dot and other formats

What is a DC

Device Environment DC (device context), also known as a unit description table or device context.

The device environment preserves some common information that needs to be set in the drawing operation, such as drawing objects and properties such as current brushes, brushes, fonts, and bitmaps, as well as drawing patterns that affect the graphics output, such as coordinate mapping, color, and background. The image says, a device environment provides a canvas and some painting tools, we can use different formats, colors of painting tools on the above graffiti. Here, the device environment "device" refers to any type of display or printer output device, when drawing, we do not have to care about the principle and method of programming the device used, all the drawing operations must be handled indirectly through the device environment, Windows automatically maps the structure described by the device environment to the appropriate physical device.

Fundamentally, DCs is a data structure used internally by Windows that stores information that is needed to output to a device, which the application uses to define graphical objects and their properties, and to transform the drawing commands between applications, device drivers, and output devices. To invoke a GDI function to output text or draw graphics to a range, you must first obtain or establish a device environment handle, each time the drawing operation for the application is based on the drawing properties of the settings in the device environment.

Unlike other Windows structures, the device environment does not directly access the device environment structure, and can be indirectly obtained or set through a series of functions provided by the system or by using the device environment's handle HDC, which includes the display height and width, the number of colors supported, and the resolution.

GDI-related classes in MFC

To support GDI drawing, MFC provides two important classes: The device context DC, which sets drawing properties and drawing graphics, and a drawing object class that encapsulates a variety of GDI drawing objects, including brushes, brushes, fonts, bitmaps, palettes, and regions.

In MFC, the CDC is the base class for the device environment class, which is used for desktop-based full-screen drawing and non-screen display printer output, in addition to the General window display. The CDC class encapsulates all graphics output functions, including vector, raster, and text output. The CDC's derived classes include CClientDC, CPaintDC, WINDOWDC, CMetaFileDC.

(1) TheCPaintDC class is a device ring from the CDC
and environmental categories. It executes cwnd::beginpaint during construction, and Cwnd::endpaint,endpaint () is responsible for purging WM_PAINT messages from the message queue in addition to releasing the device environment during the destruction. A CPaintDC object is used only in response to a window redraw message (WM_PAINT), usually in your OnPaint message processing member function. Therefore, when processing a window redraw, you must use CPAINTDC, otherwise the WM_PAINT message cannot be purged from the message queue, causing a continuous window redraw.

CPaintDC Class Members:

Data members

m_ps: Contains the paintstruct for drawing the customer area

m_hwn D  : HWND attached to the CPaintDC object

Constructor CPaintDC: Constructs a CPaintDC object attached to the specified CWnd

(2)cclientdc(Window client area device environment) class is used to manage the display context for the user area of the window, which calls the Windows function GetDC when it is constructed, and calls the ReleaseDC when it is destructor. This means that the device context associated with the CClientDC object is the client area of the window. This is typically used in response to a non-window redraw message (such as drawing text when keyboard input, mouse drawing).

Members of the CCLIENTDC class:

Constructor CClientDC: Constructs a CClientDC object data member attached to a CWnd

data member m_hwnd: hWnd of the valid window in which it is located

(3) TheCWINDOWDC(Window device environment) class is used to manage the display context that corresponds to the entire window, including its structure and controls. It calls the Windows function GETWINDOWDC when it is constructed, and calls ReleaseDC when it is destroyed. This means that the CWINDOWDC object can access all the screen areas of CWnd, including the customer area and the non-client area. It is used for drawing windows (including window borders, title bars, control buttons, and so on), unless you want to draw window borders and buttons (such as some CD players, etc.).

CWINDOWDC Class Members:

Construction CWINDOWDC: Constructs a CWINDOWDC object

Data members m_hwnd: hWnd handle associated with this CWINDOWDC

(4)CMetaFileDC is specially used for drawing metafile. The metafile records a set of GDI commands that can be reconstructed from the graphical output by this set of GDI commands. When using CMETAFILEDC, all graphical output commands are automatically recorded in a CMetaFileDC-related metafile.

(5) In addition, we can draw with Windows memory DC, which involves a screen DC and a memory DC. Draw everything you want to draw first in the memory DC, then all moved to the screen DC, so that all the tedious drawing process is done in the memory DC, we see on the screen is a complete picture, so there is no possibility of flashing.

--------------------to Be Continued------------------

GDI drawing in MFC (1)

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.