Drawing in windows and graphical device interface (GDI) in Windows)

Source: Internet
Author: User

Graphics Device Interface (GDI) isOne executableProgramIt accepts drawing requests from Windows applications (expressed as GDI function calls) and sends them to the corresponding device driver to complete hardware-specific output, such as printer output and screen output.GDI is responsible for all the Windows Graphics output, including the output pixels on the screen, the hard copy output on the printer, and the drawing of the Windows user interface.

Applications can use GDI to create three types of graphics output: vector output, grating graphics output, and text output.

Vector Graphics output
Vector Graphics output refers to the creation of lines and fill graphics, including points, straight lines, curves, polygon, slices and rectangles.
Grating output
The output of a raster image refers to the operations performed by the Raster Graphic function on the data stored in the form of a bitmap. It includes various bitmaps and the output of a graph. On the screen, the operation is performed on pixels of several rows and columns. On the printer, the operation is the dot matrix output of several rows and columns.
The advantage of raster graphics output is that it is fast, and it is a direct copy operation from memory to memory. The disadvantage is that extra memory space is required. Windows uses a large number of grating outputs when drawing the interface.
Text output
Different from the output in DOS character mode, Windows outputs data in graphical mode. In this way, the output position of the text must be calculated in units of logical coordinates when outputting the text, instead of outputting the text in units of text behavior in DOS. This is more difficult than the text output in DOS.

 

 

GDI-related classes in MFC

To support GDI plotting, MFC provides two important classes: the device context class, which is used to set drawing properties and Drawing Graphics. The drawing object class encapsulates various GDI drawing objects, includes paint brush, brush, Font, bitmap, color palette, and area.

Device context

The device context class includes the CDC and Its Derived classes cclientdc, cpaintdc, cwindowdc, and cmetefiledc.

CDC is the base class of the device context class. In addition to common window display, it is also used for desktop-based full screen rendering and non-Screen Printer output. The CDC class encapsulates all graphic output functions, including vector, grating, and text output.

The cpaintdc is used in response to the window re-painting message (wm_paint) which is the drawing output. Cpaintdc calls beginpaint () in the constructor to obtain the device context, and CALLS endpaint () in the destructor to release the device context. In addition to releasing the device context, endpaint () is also responsible for clearing the wm_paint message from the message queue. Therefore, you must use cpaintdc when processing the window re-painting. Otherwise, the wm_paint message cannot be cleared from the message queue and will cause constant window re-painting. Cpaintdc can only be used in wm_paint message processing.

Cwindowdc is used to draw window customer and non-customer areas (including window borders, title bars, and control buttons. Unless you need to draw a window border and button (such as some CD players), you generally do not need it.

Cmetefiledc is used to draw metafiles. The Metafile records a set of GDI commands, which can be used to recreate the graphic output. When cmetefiledc is used, all graphic output commands are automatically recorded in a metadata file related to cmetefiledc.

Graphical object class

Graph object types include cgdiobject, paint brush, brush, Font, bitmap, color palette, and area. Cgdiobject is the base class of the graphic object class, but it cannot be used directly by applications. To use a GDI object, you must use its derived class: paint brush, brush, Font, bitmap, area, and so on.

Note the following two points when using graphical objects:

1. like other MFC objects, the creation of a GDI object can be divided into two steps: the first step is to define an instance of the GDI Drawing Object Class; step 2: Call the object creation method to create an object.

2. Create an object: to use this object, first call CDC: SelectObject () to select it from the device context, and save the original settings to a GDI object pointer, such as poldobject. After use, use SelectObject (poldobject) to restore the original settings. However, if the context of the device is created by the user, you do not have to restore the original settings because the framework will delete the context of the device at the end of the context lifetime, in addition, the settings for drawing objects originally stored in the context of the device are deleted.

 

 

 

Access the current activity view and activity document

For SDI programs, the main framework window is the document window. You can use the following methods to obtain the current document and view:

Get activity documents:

Cmydocument * pdoc;

Pdoc = (cmydocument *) (cframewnd *) afxgetapp ()-> m_pmainwnd)

-> Getactivedocument ();

Or

Pdoc = (cmydocument *) (cframewnd *) afxgetmainwnd ());

The two are equivalent.

Get activity View:

Cmyview * pview;

Pview = (cmyview *) (cframewnd *) afxgetapp ()-> m_pmainwnd)-> getactiveview ();

For an MDI program, because the child window is the document window, you must first use getactiveframe () to obtain the activity child frame window, and then obtain the activity document and view through the Child window:

Cmdichildwnd * pchild = (cmdichildwnd *) (cframewnd *) afxgetapp ()-> m_pmainwnd)-> getactiveframe ();

Get activity documents:

Cmydocument * pdoc = pchild-> getactivedocument ();

Cmyview * pview = (cmyview *) pchild-> getactiveview ();

You can make the preceding function snippet a static member function, for example:

Static cmydocument: getcurrentdoc ()

{

Cmdichildwnd * pchild = (cmdichildwnd *) (cframewnd *) afxgetapp ()-> m_pmainwnd)-> getactiveframe ();

Cmydocument * pdoc = pchild-> getactivedocument ();

}

In this way, you can obtain the activity document (or view) in the following ways ):

Cmydocument: getcurrentdoc ();

Note: When calling a static member function, you do not need to associate a specific object with it.

Related Article

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.