GDI Drawing in MFC (4)

Source: Internet
Author: User

3. Create a drawing tool and select INTO DC

With the canvas, we have to have a paintbrush brush to draw. With GDI objects such as Hpen, Hbrush, and so on in Windows, MFC encapsulates GDI objects nicely, providing classes that encapsulate GDI objects, such as CPen, CBrush, CFont, CBitmap, and CPalette, These classes are derived classes of the GDI object class CGdiObject.

Usually first create a brush (brush), and then call the CDC::SelectObject function to select the brush (brush) into the device environment the most current drawing tool, drawing finished to restore the device environment before the brush (brush) object, and finally call CGdiObject::D The Eleteobject function deletes a brush (brush) object.

It is important to note that CGdiObject: the:D eleteobject function completely removes the underlying GDI objects (base classes for CPen and CBrush classes). In MFC, when an object is destroyed, the destructor of the object is called automatically to delete the object, and it is generally not necessary to call CGdiObject::D eleteobject To delete the GDI object, because if the device environment is still using a GDI object, the application crashes or an incomprehensible run error occurs.

(1) Creating brushes

BOOL CPen::CreatePen(int npenstyle, int nwidth, COLORREF cfcolor);

Npenstyle Specifies the style of the brush. See the Npenstyle parameter in the CPen constructor for a list of possible values.

nwidth Specifies the width of the brush. If this value is 0, the width in device units is always one pixel, regardless of the mapping mode.

The crcolor contains an RGB color for the brush, which is the COLORREF structure.

In addition, the Cdc::selectstockobject function can be used to invoke the CGdiObject object corresponding to the system's pre-defined inventory pen.

Poldpen = (cpen*) pdc->selectstockobject (Black_pen);

(2) Creating a paint brush

BOOL CBrush::createsolidbrush (colorref crcolor);

BOOL CBrush::createhatchbrush(int nIndex, COLORREF crcolor);

Parameter: nIndex Specifies the shaded line style of the brush. The following values are desirable:

Hs_horizontal * * = = =

Hs_vertical/* | | | | | * *

Hs_fdiagonal/ * \\\\\ * /

Hs_bdiagonal/ */////* /

Hs_cross/ * +++++ * /

Hs_diagcross/ * xxxxx * /

Return value: Returns a value other than 0 if the call succeeds, otherwise 0.

In addition, the Cdc::selectstockobject function can be used to invoke the CGdiObject object corresponding to the system's pre-defined inventory brush.

Poldbrush = (cbrush*) pdc->selectstockobject (Black_brush);

(3) Select the brush (brush) into the device environment.

The following is the module for GDI drawing in the default mapping mode in MFC:

Get the device environment first PDC

CPen *poldpen,newpen;

CBrush *poldbrush,newbrush1,newbrush2;

// Create A solid white line brush with a width of pixel

Newpen.createpen (Ps_solid,1,rgb (0,0,0));

// Create a red solid line paint brush

Newbrush1.createsolidbrush (RGB (255,0,0));

// creates a shadow brush of the downward (right-to-left) shadow of a solid red line

Newbrush2.createhatchbrush (Hs_bdiagonal,rgb (255,0,0));

// Selecting Newpen brushes and NewBrush1 brush objects into the device environment

Poldpen = Pdc->selectobject (&newpen);

Poldbrush = Pdc->selectobject (&NEWBRUSH1);

// calling a DC drawing function drawing

// ...

// finish drawing, restore the original brush, paint brush

Pdc->selectobject (Poldpen);

Pdc->selectobject (Poldbrush);

// Delete a created brush, paint brush

Newpen.deleteobject ();

Newbrush1.deleteobject ();

Newbrush2.deleteobject ();

(4) When the text is drawn, you can generally call the Cdc::setbkcolor function to set the background color, call the Cdc::settextcolor function to set the text color, call the Cdc::settextalign function to set the text alignment tag.

4. Call the DC drawing function drawing

GDI provides member functions for drawing basic graphics, which are encapsulated in the CDC class in MFC.

Note: The coordinates used by the drawing function are logical coordinates.

Common CDC drawing functions

Function

Function

Line output function

GetCurrentPosition

Gets the current position of the pen, expressed in logical coordinates

MoveTo

Move Current position

LineTo

Draw a line from the current position to a point, but do not include that point

Arc

Draw an elliptical arc

ArcTo

Draw an elliptical arc. In addition to updating the current location, this function is similar to ARC

Polypolyline

Draw multiple groups of connected segments. This function does not use or update the current position

PolylineTo

Draw one or more lines and move the current position to the end of the last line

Polybezier

Draw one or more Bezier splines. Do not use or update the current location

PolyBezierTo

Draw one or more Bezier splines and move the current position to the end of the last Bezier spline

Ellipse and Polygon functions

Chord

Draws an elliptical arc (an ellipse and a closed shape that intersects a segment)

DrawFocusRect

Draws the style rectangle used to represent the focus

Ellipse

Draw Ellipse

Pie

Draw a pie chart

Polygon

Draws a polygon, containing one or more points (vertices) connected by a segment

Polypolygon

Create two or more polygons that use the current polygon fill mode, and polygons can be separated or superimposed on each other

Polyline

Draws a polygon that contains a set of segments that connect to a specified point

Rectangle

Draws a rectangle with the current pen, fills it with the current paint brush

RoundRect

Draws a rounded rectangle with the current pen and fills it with the current paint brush

Bitmap functions

BitBlt

Copy a bitmap from a specified device context

StretchBlt

Moves the bitmap from the source rectangle and device to the target rectangle, stretching or compressing the bitmap if necessary to fit the dimension of the destination rectangle

GetPixel

Gets the RGB color value for the specified point pixel

SetPixel

Sets the approximate value of the specified point pixel to the nearest specified color

Text functions

TextOut

Writes a string at the specified position with the currently selected font

ExtTextOut

Writes a string in the rectangular area with the currently selected font

TabbedTextOut

Writes a string at the specified position, and the tab extension specifies the value in the tab stop position array

DrawText

Draws formatted text within the specified rectangle

For-------------------details, refer to MSDN, MFC class Library-----------------

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

GDI Drawing in MFC (4)

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.