MFC drawing basics.

Source: Internet
Author: User
Tags clear screen textout

From: http://blog.csdn.net/leolee82/article/details/6992590

First, let's take a look at some structure and class related to drawing in MFC.
1. Point
(1) point structure point data structure point is used to represent the X and Y coordinates of a point:
Typedef struct tagpoint {
Long X;
Long y;
} Point;

(2) Point-class cpoint
Point class cpoint is an independent class without a base class. It encapsulates the point structure and has member variables X and Y.
There are 5 constructor types:
Cpoint ();
Cpoint (INT initx, int inity );
Cpoint (point initpt );
Cpoint (size initsize );
Cpoint (lparam dwpoint); // set the low-text to X and the high-text to Y

The cpoint class also defines four translation and setting functions:
Void offset (INT xoffset, int yoffset );
Void offset (point );
Void offset (size );
Void setpoint (int x, int y );

The cpoint class also contains the +,-, + =,-=, =, and ,! =.

2. Size
(1) size and structure
Size (size) structure size is used to indicate the width of the rectangle CX and high Cy:
Typedef struct tagsize {
Long CX;
Long Cy;
} Size

(2) Large and small class csize
The large and small class csize is also an independent class without a base class. It encapsulates the size structure and has the member variables CX and Cy.
Its constructor also has five types:
Csize ();
Csize (INT initcx, int initcy );
Csize (size initsize );
Csize (point initpt );
Csize (DWORD dwsize); // set low-level characters to CX and high-level characters to cy

The csizet class also contains the +,-, + =,-=, =, and ,! = And other operators to support operations between csize objects and csize, point, size, and rect objects

3. rectangle
(1) rect of rectangular structure
Rect defines the coordinates between the upper left corner and the lower right corner of the rectangle:
Typedef struct tagrect {
Long left;
Long top;
Long right;
Long bottom;
} Rect;

(2) rectangular crect
The rectangular class crect is also an independent class without a base class. It encapsulates the rect structure and has the member variables left, top, right, and bottom.
There are 6 constructor types:
Crect ();
Crect (int l, int T, int R, int B );
Crect (const rect & srcrect );
Crect (lpcrect lpsrcrect );
Crect (point, size );
Crect (point topleft, point bottomright );

The crect class has been overloaded with =, +,-, + =,-=, = ,! =, &, |, & =, | =, And other operators to support computation between the crect object and the crect, point, size, and rect object.

We also define the Conversion characters "lpcrect" and "lprect" to automatically convert the crect object to the rectangular structure and class Pointer "lpcrect" and "lprect.

You can call the ptinrect function of the crect class to determine whether a vertex is in a rectangle or not:
Bool ptinrect (point) const;
This function returns true if the vertex is in its rectangle. Note: This rectangle area does not include the right and bottom boundary of the rectangle.

Then the device description table is obtained and released.ArticleOnce mentioned, I will not mention it here.
Let alone a special concept: "Safe DC handle"
You can also use the member functions of the CDC class:
HDC getsafehdc ();
To obtain the Security DC handle of the window corresponding to the CD (such as the customer zone). This handle remains valid when the window exists.
For example, you can first define the class variable HDC m_hdc; and then assign m_hdc = getdc ()-> getsafehdc () to it in a proper place. Then you can safely use it.

Because the pointer cannot be simply stored in the data member of the class, instead, it should be converted to a Windows handle using the getsafehdc member function (the unique GDI identifier that can survive)

For example, you can use a member function of the CDC class.
Bool attach (HDC); // if the call succeeds, a non-0 value is returned.
To connect the CDC object to the DC handle.

4. Set the drawing color
(1) color
Colors in windows are generally represented in four bytes. Win32 API defines a variable type colorref :( windef. h) that specifically represents the color index value)
Typedef DWORD colorref; // 0x00bbggrr
And a macro RGB consisting of three primary colors, red, green, and blue: (wingdi. h)
# Define RGB (R, G, B) (colorref) (byte) (R) | (Word) (byte) (g) <8 )) | (DWORD) (byte) (B) <16 )))

Here, R, G, and B are byte variables and the value range is 0 ~ 255. The functions are described as follows:
Colorref RGB (
Byte bred, // red weight
Byte bgreen, // green component
Byte bblue // blue weight
);

For example:
Colorref red, gray;
Red = RGB (255, 0, 0 );
Gray = RGB (1, 128,128,128 );

The API also defines the macro get? Value :( wingdi. h)
# Define getrvalue (RGB) (lobyte (RGB ))
# Define getgvalue (RGB) (lobyte (Word) (RGB)> 8 ))
# Define getbvalue (RGB) (lobyte (RGB)> 16 ))

(2) point color (pixel)
In Windows, the pixel (pixel) color is directly set by setpixel, a member function of the context-like CDC.
The function is prototype:
Colorref setpixel (int x, int y, colorref crcolor );
Colorref setpixel (point, colorref crcolor );
X and Y are the abscissa and ordinate of the pixel respectively, and crcolor is the color value of the pixel.

For example:
PDC-> setpixel (10, 10, RGB (0,255, 0 ));
In addition, you can also use the CDC member functions.
Colorref getpixel (int x, int y) const;
Colorref getpixel (point) const;
To obtain the color of the specified vertex (x, y) or point.

For example:
Colorref Col;
Col = PDC-> getpixel (10, 10 );

(3) line color (PEN)
In Windows, a line chart must be painted with a pen, so the color of the line is determined by the pen color.
In MFC, The Pen attributes and functions are provided by the cpen class (cpen is the derived class of cgdiobject ).
To create and use a pen, follow these steps:

#1 create a pen object:
You can use either of the following methods to create a pen cpen object:
Cpen (INT npenstyle, int nwidth, colorref crcolor );
Where:
Npenstyle is a pen style. Optional values: ps_solid, ps_dash, ps_dot, ps_dashdot, and psdashdotdot
Note: 1 ~ Pen 4 is valid only when the pen width is 0 or 1. When the pen width is greater than 1, it is always solid.
Bool createpen(INT npenstyle, int nwidth, colorref crcolor );

#2 select the pen object into the device context:
To use the pen object we created, you must first select the device context. This can be done by calling the SelectObject function of the CDC context class:
Cpen * SelectObject (cpen * ppen); The returned value is a pointer to the original pen object (usually save it for the next installation ).
For examplePoldpen = PDC-> SelectObject (& pen );

In addition, there are somePre-defined pen object, which can be selected by another CDC member function selectstockobject into DC
Its function is prototype: Virtual cgdiobject * selectstockobject (INT nindex );
Predefined pen objects include black_pen (black pen), white_pen (white pen), and null_pen (empty pen/colorless pen)
Example: PDC-> selectstockobject (black_pen );

#3 use the device context to draw a line chart:
The stroke object in the context of the current device.
Line charts include straight lines, line lines, rectangles, and (elliptical) circles (ARCs). For details, see

#4 release a pen object from the device context:
To delete a used pen object, you must release it from the device context before deleting it.
The release method is to load other pen objects (usually re-load the original pen object ). For example
PDC-> SelectObject (poldpen );

#5 delete a pen object:
To delete a pen object, you must first release it from the device context. The deletion methods are as follows:
Call the eobject function of the PEN type CDC to delete the current content of the PEN (but the pen object is not deleted, and the createpen function can be used to create a new pen in the pen object ).
For example
Pen. deleteobject ();
Delete the pen object using the delete operator, such as delete pen;
Automatic deletion: if the pen object is a local variable, it will be automatically deleted by the system when it leaves its scope.

(4) face color (Brush)
In Windows, a face image must be filled with a brush. Therefore, the face color is determined by the brush color.
The brush class in MFC is cbrush (which is also a derived class of cgdiobject). The creation and use steps of the brush are similar to those of the pen.
There are four constructors:
Cbrush (); // create an empty object for the brush
Cbrush (colorref crcolor); // create a solid brush whose color is crcolor.
Cbrush (INT nindex, colorref crcolor); // create a stripe (hatch) brush with a style specified by nindex and crcolor. The value of nindex can be hatch styles:
Symbol constant numeric constant Style
Hs_horizontal 0 horizontal line
Hs_vertical 1 vertical line
Hs_fdiagonal 2 forward slash
Hs_bdiagonal 3 backslash
Hs_cross 4 cross (positive grid)
Hs_diagcross 5 Oblique Cross Line (diagonal grid)

Cbrush (cbitmap * pbitmap); // creates a pattern brush with a bitmap of pbitmap.
For example: PDC-> fillrect (& rect, new cbrush (RGB (R, G, B )));

Corresponds to the constructor, which has multiple member functions that create different types of Brushes:
Bool createsolidbrush (colorref crcolor );
Bool createhatchbrush (INT nindex, colorref crcolor );
Bool createpatternbrush (cbitmap * pbitmap );
Bool createdibpatternbrush (hglobal hpackeddib, uint nusage );
Bool createdibpatternbrush (const void * lppackeddib, uint nusage );
Bool createbrushindirect (const logbrush * lplogbrush );
Bool createsyscolorbrush (INT nindex );

Pre-defined brush objects include black_brush (black brush), dkgray_brush (dark gray brush), gray_brush (gray brush), hollow_brush (empty brush), ltgray_brush (light gray brush), null_brush (empty brush), white_brush (white brush)
Empty click by default

Like a pen, you can use the SelectObject or selectstockobject function to select a custom brush or a pre-defined brush into the DC for use in Area painting.

(5) text color
You can use the settextcolor and setbkcolor functions of the CDC class to set the foreground color and background color of the output text respectively:
(The default foreground color is black and the background color is blank)
Colorref gettextcolor () const;
Virtual colorref settextcolor (colorref crcolor );
Colorref getbkcolor () const;
Virtual colorref setbkcolor (colorref crcolor );

For example:
PDC-> textout (10, 10, "test text ");
PDC-> settextcolor (RGB (0,128, 0 ));
PDC-> textout (10, 30, "test text ");
PDC-> setbkcolor (RGB (0, 0,128 ));
PDC-> textout (10, 50, "test text ");

5. Clear Screen
Windows does not provide a dedicated screen clearing function. You can call the following two functions of cwnd to complete this function:
Void invalidate (bool berase = true); void updatewindow ();
Or call the cwnd function bool redrawwindow (
Lpcrect lprectupdate = NULL,
Crgn * prgnupdate = NULL,
Uint flags = rdw_invalidate | rdw_updatenow | rdw_erase
.

5. In addition to the ing mode, you can set many drawing properties.
Such as the background, drawing mode, polygon filling mode, draw arc direction, brush origin, etc.

#1. Background
1) background color when the background mode is not transparent, the background color determines the gap color of the linear graph (such as gaps in the dotted line, gaps in the striped brush and gaps in the text)
You can use the getbkcolor and setbkcolor functions of the CDC class to obtain and set the current background color:
Colorref getbkcolor () const; // returns the current background color.
Virtual colorref setbkcolor (colorref crcolor); // returns the previous background color.
// If an error occurs, 0x800000002 is returned)
In the background mode, the background mode affects the padding of a line chart (such as gaps in the dotted line, gaps in the striped brush, and gaps in the text.
You can use the getbkmode and setbkmode functions of the CDC class to obtain and set the current background mode:
Int getbkmode () const; // returns the current background mode.
Int setbkmode (INT nbkmode); // return the value of the background mode in the previous background mode.

The opaque is not transparent (default value) and is filled with the background color.
Ensure that the original background image remains unchanged at the transparent gap of the transparent

2. Drawing mode: the drawing mode refers to the hybrid mode of the foreground view,
It determines how the pen and brush color (pbcol) of the new drawing combine with the color of the original image (sccol) to obtain the pixel color (pixel ).
1) set the drawing mode. You can use the CDC member function setrop2 (ROP = raster operation grating operation) to set the drawing mode:
Int setrop2 (INT ndrawmode );
The value of ndrawmode is the value of ndrawmode in the drawing mode.

operation result of symbolic constants
r2_black black pixel = black
r2_white white pixel = white
r2_nop unchanged pixel = sccol
r2_not reversed pixel = ~ Sccol
r2_copypen overwrite pixel = pbcol
r2_notcopypen reversed overwrite pixel = ~ Pbcol
r2_mergepennot reversed or pixel = ~ Sccol | pbcol
r2_mergenotpen or reversed pixel = sccol | ~ Pbcol
r2_masknotpen and reversed pixel = sccol &~ Pbcol
r2_mergepen or pixel = sccol | pbcol
r2_notmergepen or non-pixel = ~ (Sccol | pbcol)
r2_maskpen and pixel = sccol & pbcol
r2_notmaskpen and non-pixel = ~ (Sccol & pbcol)
r2_xorpen exclusive or pixel = sccol ^ pbcol
r2_notxorpen exclusive or non-pixel = ~ (Sccol ^ pbcol)
where r2_copypen (overwrite) is the default drawing mode, while r2_xorpen (exclusive or) is more commonly used.

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.