Self-drawing of the MFC Button control-Detailed Description

Source: Internet
Author: User
Tags drawtext

The interface under VC is really ugly. Sometimes we have to draw the control by ourselves. We never understood it before. Recently, I read it again. I finally understood a little bit and shared it with you... Q for source code I am looking for friends who want to learn VC together

For example

We want to change the background of an editing box. We will respond to the WM_CTLCOLOR function to modify the OnCtlColor, but it will not work with the Button control .. in this case, we need to perform the self-drawing function virtual void DrawItem (LPDRAWITEMSTRUCT lpDrawItemStruct );

To overwrite this virtual function and set the type to BS_OWNERDRAW, the system will enter our

The DrawItem function is used to draw controls. Therefore, self-painting takes two steps.

ASSERT macro Evaluate an expression and generate a debug report when the result is FALSE (debug version only). the computation expression is to generate a debug report when the result is false (only under debug)

1. Set the type to BS_OWNERDRAW.

2. Override virtual void DrawItem (LPDRAWITEMSTRUCT lpDrawItemStruct). The function code is designed by ourselves.

Note that all the functions required for repainting are the SDK function BOOL DrawFrameControl (// This function draws a framework HDC hdc for a specified type of control, // handle to device context dc lprect lprc, // bounding rectangle: UINT uType, // frame-control type UINT uState // frame-control state. For details, refer to MSDN );

Int DrawText (// output text HDC hDC in the specified rectangle, // handle to dc lpctstr lpString, // text to draw int nCount, // text length LPRECT lpRect, // formatting dimensions UINT uFormat // text-drawing options );

COLORREF SetTextColor (// set the text color of the specified dc hdc, // handle to dc colorref crColor // text color );

Int FillRect (// fill the HDC hDC In the rectangle with the given image brush, // handle to dc const rect * lprc, // rectangle HBRUSH hbr // handle to brush );

Int SetBkMode (// set the background mode to transparent hdc hdc, // handle to DC int iBkMode // background mode );

Typedef struct tagDRAWITEMSTRUCT {// For details, refer to msdn uint CtlType; // control type UINT CtlID; // id UINT itemID; // item id uint itemAction; behavior UINT itemState; // status HWND hwndItem; // control handle HDC hDC; // dc handle RECT rcItem; // hold region ULONG_PTR itemData;} DRAWITEMSTRUCT;

Draw3dRect (LPCRECT lpRect, COLORREF clrTopLeft, COLORREF clrBottomRight); this function is used to plot the location size of a 3D rectangle, where lpRect is filled with the location size of the entire 3D rectangle, clrTopLeft and clrBottomRight are color RGB values in the upper left and lower right of the 3D effect, respectively.

BOOL DrawFocusRect (draw a dotted line HDC hDC, // handle to device context const rect * lprc // logical coordinates); number function: Draw a focal rectangle. This rectangle is completed by an exclusive or operation in the logo focus style (the focus is usually expressed by a dot line ). If you call this function again with the same parameter, the focus rectangle is deleted.

 

The following is the program code:

Void CBtnXiaoWei: DrawItem (LPDRAWITEMSTRUCT lpDrawItemStruct) {CString btnCaption; // Save the button title GetWindowText (btnCaption); // obtain the button title CRect drawRect; // define the CRect object HDC dc = lpDrawItemStruct-> hDC; // Control dc cdc * pDC = CDC: FromHandle (dc ); // obtain the CDC pointer through hdc uint nStyle = lpDrawItemStruct-> CtlType; drawRect. copyRect (& (lpDrawItemStruct-> rcItem); // copy the control rectangular area to our CRect object DrawFrameControl (dc, & drawRect, DFC_MENU, nStyle ); // draw the control frame CBrush pBrush; // create a paint brush

Static int n = 0; pBrush. createSolidBrush (RGB (100 + n, 130, n); // create pDC-> FillRect (drawRect, & pBrush); // draw rectangular pDC-> SetTextColor (m_clo ); // set the text color

CRect textRect; // defines a CRect used to draw a text textRect. copyRect (& drawRect); // copy the rectangle area CSize sz = pDC-> GetTextExtent (btnCaption); // obtain the string size textRect. top + = (textRect. height ()-sz. cy)/2; // adjust the text position to center pDC-> SetBkMode (TRANSPARENT); // set the text background TRANSPARENT pDC-> DrawText (btnCaption, & textRect, DT_RIGHT | DT_CENTER | DT_BOTTOM); // draw Text n + = 10 ;}

 

Void CBtnXiaoWei: SetTextColer (COLORREF clo) {m_clo = clo; Invalidate (); // indicates that the image is repainted due to local invalidity}

 

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.