MFC Button control self-drawing----detailed explanation

Source: Internet
Author: User
Tags drawtext set background

VC under the interface is really ugly sometimes we have to do the control of the drawing of the past has not understood the last time to read the study again finally understand a little
Share with you ... Need source code Q I'm looking for a friend to learn VC together

For example


We're going to change the background of an edit box, we respond to the WM_CTLCOLOR function for OnCtlColor, but not with the button control.

At this time we have to do the self drawing correlation function virtual void DrawItem (lpDrawItemStruct lpdrawitemstruct);

To overwrite this virtual function and the type to be set to Bs_ownerdraw this time the application to initialize the interface will enter our

DrawItem function to draw a control so it's 2 steps from drawing.

ASSERT macros

Evaluate an expression and generate a debug, and is FALSE (Debug version only).
The calculation expression is to generate a debug report when the result is false (just under Debug)


1. Type to be set to Bs_ownerdraw


2. Rewrite virtual void DrawItem (lpdrawitemstruct lpdrawitemstruct); Function code we designed it ourselves.

Redraw required functions Note all functions in the SDK

BOOL Drawframecontrol (///This function draws a frame of the specified type control
HDC HDC,//Handle to device context DC
Lprect LPRC,//bounding rectangle holding area
UINT Utype,//Frame-control type
UINT ustate//Frame-control state status See MSDN specifically
);


int DrawText (//output text in the specified rectangular area)
HDC HDC,//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 HDC,//Handle to DC
COLORREF crcolor//Text color
);

int FillRect (////Fill rectangular area with a given brush)
HDC HDC,//Handle to DC
CONST RECT *LPRC,//Rectangle
Hbrush HBR//Handle to Brush
);


int SetBkMode (//Set background mode transparent transparent
HDC HDC,//Handle to DC
int Ibkmode//Background mode
);


typedef struct TAGDRAWITEMSTRUCT {   //view 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 area
  ULONG_PTR itemdata;  
} drawitemstruct 


Draw3drect
(
Lpcrect lprect,
COLORREF Clrtopleft,
COLORREF Clrbottomright
);
This function is used to implement the position size of the 3D rectangle, where the lprect is the location of the entire 3D rectangle,
Clrtopleft and Clrbottomright are the values of RGB colors in the upper left and lower right of the 3D effect respectively.


BOOL DrawFocusRect
(Draw a dotted rectangle
HDC HDC,//handle to device context
CONST rect* LPRC//logical coordinates
);
Number function: Draw a focus rectangle. This rectangle is done by XOR or operation in the style of the flag focus (the focus is usually represented by a dotted line).
If you call this function again with the same argument, the focus rectangle is deleted


Here's The program code:


void Cbtnxiaowei::D rawitem (lpdrawitemstruct lpdrawitemstruct)
{
CString btncaption; Save Button Title
GetWindowText (btncaption); Get button title
CRect DrawRect; Defining CRect Objects
HDC dc= lpdrawitemstruct->hdc;//Control DC
Cdc*pdc=cdc::fromhandle (DC);//Get CDC pointers through HDC
UINT nstyle=lpdrawitemstruct->ctltype;
Drawrect.copyrect (& (Lpdrawitemstruct->rcitem)); Copy control rectangular area to our CRect object
Drawframecontrol (Dc,&drawrect,dfc_menu,nstyle); Drawing a control frame
CBrush pbrush;//Create a painting brush

static int n=0;
Pbrush.createsolidbrush (RGB (100+n,130,n)); Create
Pdc->fillrect (Drawrect,&pbrush);//Draw a rectangle
Pdc->settextcolor (M_clo); Set Text color


CRect textrect;//defines a CRect for drawing text
Textrect.copyrect (&drawrect); Copy Rectangular Area
CSize sz=pdc->gettextextent (btncaption);//Get string size
textrect.top+= (Textrect.height ()-sz.cy)/2;//Adjust Text position Center
Pdc->setbkmode (transparent);//Set 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 (); is partially invalid to cause repaint
}


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.