CLKing31----------------MFC brush CPen class

MFC brush CPen class

Benjaminpcm 2017-03-13 21:35:07 8084 Collection 15
classification column: windows programming article tags: mfc function
copyright
The CPen class of MFC encapsulates the pen object, and the member function CreatePen realizes the function of creating a pen. The function prototype is as follows:

BOOL CreatePen(
    int nPenStyle, //brush style
    int nWidth, //brush width
    COLORREF crColor //brush color
);
1
2
3
4
5
Once the brush is no longer needed, remember to delete it with the DeleteObject function

Set the line style In
the function of creating a pen, the parameter nPenStyle represents the style of the pen, and one of the following values ​​can be set. nPenStyle —— Long, specifies the pen style, which can be one of the following constants:
PS_SOLID: The pen draws a solid line.
PS_DASH: The pen draws a dotted line (nWidth must not be greater than 1).
PS_DOT: The pen draws a dotted line (nWidth must not be greater than 1).
PS_DASHDOT: The pen draws a dotted line (nWidth must not be greater than 1).
PS_DASHDOTDOT: The pen draws a dot-dot-dash line (nWidth must not be greater than 1).
PS_NULL: Empty pen, pen cannot draw pictures.
PS_INSIDEFRAME: When the inner solid line pen is a closed object frame generated by an ellipse, rectangle, rounded rectangle, pie chart, chord, etc., the line width expands inward. If the specified accurate RGB color does not exist, dithering is performed.

Set the line width In
the function of creating the pen, nWidth represents the line width of the pen, which is a logical value. The smaller the value, the thinner the drawn graphic line; the larger the value, the thicker the drawn graphic line

Set the pen color The
pen color is controlled by the RGB macro, for example, you need to draw red graphics, RGB (255,0,0). There are many reference color macros RGB in MFC. Here is a summary of some common color macros for your reference.

There are three basic colors: red, green and blue. The other colors are composed of these three basic colors. For example, red RGB (255,0,0), green RGB (0, 255,0), blue RGB (0,0, 255), light pink #FFB6C1 255,182,193, purple #800080 128,0,128, etc.

For example:
CPen pen(PS_SOLID, 10, RGB(0, 0, 255));
1
The brush created now will not take effect. You need to apply the brush to the DC. MFC provides the SelectObject function to complete this function. It should be noted that SelectObject The parameter of the function is the pointer of the pen object, and the return value is the pointer of the replaced pen. We need to create a CPen object to save the old pointer to ensure that the style of the pen is restored to the initial value after the pen is used up. The code:

CClient dc(this);
CPen *oldPen=dc.SelectObject(&pen);
1
2
Then draw a line:

dc.MoveTo(m_ptOrigin);
dc.LineTo(point);
1
2
Finally, restore the original style of the brush:

dc.SelectObject (& pOldPen);
1
Reference 1 link
reference link
2 ----------------
Disclaimer: This article is CSDN blogger original article "Benjaminpcm", and follow CC 4.0 BY- SA copyright agreement, please attach the original source link and this statement for reprinting.
Original link: https://blog.csdn.net/yexudengzhidao/article/details/61930069

Guess you like

Origin blog.csdn.net/qq_43662480/article/details/114992119