VS2010-MFC (Graphic Image: CDC class and its screen drawing function)

Source: Internet
Author: User
Tags textout

Transferred from: http://www.jizhuomi.com/software/244.html

The previous section speaks of the knowledge of text output, the main content of this section is the CDC class and its screen drawing functions.

Introduction to CDC classes

The CDC class is a device context class.

The CDC class provides member functions to handle device contexts such as displays or printers, as well as member functions that handle the display context associated with the window client area. The CDC member function allows you to do all of the drawing operations, including working with drawing tools, selection of GDI objects, processing of colors and palettes, getting and setting drawing properties, mapping, window extents, coordinate transformations, clipping, and drawing lines, simple graphics, ellipses, and polygons, as well as text output, processing fonts, member functions are provided using printer jumps and scrolling, and so on.

As mentioned above, the CDC class almost encapsulates all of the Windows GDI functions, and in addition, MFC has several subclasses derived from the CDC class, including CWINDOWDC, CPaintDC, CClientDC, CMetaFileDC, which are used for some specific drawing operations.

in general, we should remember to delete the CDC object after we have finished using it, otherwise there will be a memory leak . In many cases we can call the CWND::GETDC () function to get the device context pointer, the CDC pointer, and this time remember to call the Cwnd::releasedc () function to release the device context.

screen drawing member functions for the CDC class

The CDC class has a number of member functions, and this is about more commonly used drawing functions, including member functions for drawing points, lines, rectangles, ellipses, polygons, text, and bitmaps.

colorref setpixel (int x,int y,colorref crcolor);
COLORREF SetPixel (Point point,colorref crcolor);

The above two member functions are used to set the pixel of the specified coordinate point to the specified color, thus achieving the draw point function. The parameter x is the logical x-coordinate of the point, the parameter y is the logical y-coordinate of the point, the parameter crcolor is the color to set for the point, and the parameter point refers to the logical x-coordinate and logical y-coordinate of the point, which can be passed to the dot struct-body variable or CPoint object.

CPoint MoveTo (int x,int y);
CPoint MoveTo (point point);

Moves the current point to the specified position. The parameter x specifies the logical x-coordinate of the new position, the parameter y specifies the logical y-coordinate of the new position, and the parameter point specifies the logical x-coordinate and logical y-coordinate of the new position, which can be passed to the points struct-body variable or the CPoint object.

BOOL LineTo (int x,int y);
BOOL LineTo (point point);

Draws a line from the current point to the specified point (excluding the specified point). The parameter x is the logical x-coordinate of the specified point, the parameter y is the logical y-coordinate of the specified point, and the parameter point is the logical x-coordinate and the logical y-coordinate of the specified points. In general, when we draw a straight line, we can call the MoveTo function to move the current point to a position, and then call LineTo to draw a line.

BOOL Rectangle (int x1,int y1,int x2,int y2);
BOOL Rectangle (Lpcrect lpRect);

Draws a rectangle using the current brush. The X1 parameter specifies the x-coordinate of the upper-left corner of the rectangle, y1 the y-coordinate of the upper-left corner of the rectangle, x2 The x-coordinate of the lower-right corner of the rectangle, and y2 the y-coordinate of the lower-right corner of the rectangle, and the coordinates are logical units. The parameter lprect is a pointer to a rectangular object that can be passed to the CRect object or a pointer to a RECT struct variable.

BOOL Ellipse (int x1,int y1,int x2,int y2);
BOOL Ellipse (Lpcrect lpRect);

Draws an ellipse. The X1 parameter specifies the x-coordinate of the upper-left corner of the bounding rectangle of the ellipse, and the parameter y1 the y-coordinate of the upper-left corner of the bounding rectangle of the ellipse, and the parameter x2 the x-coordinate of the lower-right corner of the bounding rectangle of the ellipse, and the y-coordinate of the bounding rectangle of the ellipse. The lprect parameter specifies the bounding rectangle of the ellipse, which can be passed into the CRect object or a pointer to a RECT struct variable.

BOOL Polyline (lppoint lppoints,int ncount);

Draws a polygon from the specified polygon vertex. The parameter lppoints is a pointer to an array of point struct variables or an array of CPoint objects, where the dot struct variable or CPoint object represents the coordinates of the polygon vertex, and the parameter ncount the number of midpoints of the array, at least 2.

virtual BOOL TextOut (int x,int y,lpctstr lpszstring,int ncount);
BOOL TextOut (int x,int y,const cstring& str);

Outputs text at the specified location using the currently selected font. parameter x Specifies the x-coordinate of the starting point of the text, parameter y specifies the y-coordinate of the starting point of the text, the parameter lpszstring is the text string to output, the parameter ncount the number of bytes in the specified string, and the parameter str is the CString object that contains the characters to output. These two functions are actually mentioned in the previous section.

BOOL BitBlt (
int x,
int y,
int nwidth,
int nheight,
cdc* PSRCDC,
int XSRC,
int YSRC,
DWORD Dwrop
);

Copies a bitmap from the source device context to the current device context. Parameter x Specifies the logical x-coordinate of the upper-left corner of the target rectangle, parameter y Specifies the logical y-coordinate of the upper-left corner of the target rectangle, and the parameter nwidth specifies the width (logical units) of the target rectangle area and source bitmap; parameter nheight Specifies the height of the destination rectangle area and the source bitmap (logical units) The parameter PSRCDC is a pointer to the CDC object that points to the source device context, and if dwrop specifies a raster operation that does not contain a source, the PSRCDC can be null, the parameter XSRC specifies the logical x-coordinate of the upper-left corner of the source bitmap, and the parameter ysrc specifies the logical y-coordinate of the upper-left corner of the source bitmap The parameter dwrop specifies the raster operation to be performed, and the raster opcode defines how GDI will combine the current brush color, source bitmap color, and target bitmap color to form a new color, and here are some common raster operation codes and meanings:

blackness: Indicates that the target rectangle area is populated with the color associated with index 0 of the physical palette (which is black for the default physical palette).
Dstinvert: Indicates that the target rectangle area color is reversed.
mergecopy: Indicates that the color of the source rectangle area is combined with a specific pattern using the and (and) operator of the Boolean type.
mergepaint: Merges the color of the inverse source rectangle area with the color of the destination rectangle area by using the or (or) operator of the Boolean type.
notsrccopy: The color of the source rectangle is reversed and copied to the target rectangle area.
notsrcerase: Combines the color values of the source and destination rectangular regions using the or (or) operator of the Boolean type, and then deserializes the color of the composition.
Patcopy: Copies a specific pattern to the target bitmap.
patpaint: Merges the color values of the source rectangle region with the color of a particular pattern by using the Boolean or (or) operator. The or (or) operator is then used to merge the result of the operation with the color within the target rectangle area.
Patinvert: Merges the colors within the source and destination rectangular regions by using the XOR operator.
Srcand: Merges the colors in the source and destination rectangular areas by using the and (with) operator.
Srccopy: Copies the source rectangle area directly to the destination rectangle area.
srcerase: Merges the color values of the region of the source rectangle by using the and (and) operator to reverse the color of the target rectangle area.
Srcinvert: Merges the colors of the source and destination rectangle regions by using the XOR operator of the Boolean type.
srcpaint: Merges the colors of the source and destination rectangle regions by using the or (or) operator of the Boolean type.
whiteness: Fills the target rectangle area with the color associated with index 1 in the physical palette. (This color is white for the default physical palette).

Well, this section is about here, mainly about the CDC class, and also introduces some common drawing functions of the CDC class. If you want to know more information, you can check MSDN.

VS2010-MFC (Graphic Image: CDC class and its screen drawing function)

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.