Article Reference address: http://blog.csdn.net/yue7603835/article/details/6649458
VC interface is really ugly sometimes we have to do our own control of the drawing has not been understood recently once again to learn again finally understand a little
Share with you ... Need source code Q I am looking for a friend to learn VC
For example
We're going to change the background of an edit box. We respond to the WM_CTLCOLOR function for OnCtlColor modification but not to the button control.
At this time we are going to do self-drawing related functions virtual void DrawItem (lpDrawItemStruct lpdrawitemstruct);
To overwrite this virtual function and type to be set to Bs_ownerdraw when the application initializes the interface, it enters our
The DrawItem function makes the control draw so that self-drawing is 2 steps
- 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 a specified type of controlHDC HDC,//handle to device context DCLPRECT LPRC,//bounding rectangle holding areaUINT Utype,//Frame-control TypeUINT ustate//Frame-control State Status specifically see MSDN );intDrawText (//outputs text in the specified rectangular areaHDC HDC,//Handle to DCLPCTSTR lpstring,//text to draw intNcount,//Text LengthLPRECT LPRECT,//Formatting DimensionsUINT Uformat//text-drawing Options ); COLORREF SetTextColor (//sets the text color of the specified DCHDC HDC,//Handle to DCCOLORREF Crcolor//text color );intFillRect (//fills a rectangular area with a given paint brushHDC HDC,//Handle to DCCONST RECT *LPRC,//RectangleHbrush HBR//Handle to Brush );intSetBkMode (//set Background mode transparent transparentHDC HDC,//Handle to DC intIbkmode//Background Mode); typedefstructtagdrawitemstruct {//specifically see MSDNUINT Ctltype;//control typeUINT Ctlid;//IDUINT ItemID;//Item IDUINT itemaction; Behavior UINT itemState; //StatusHWND Hwnditem;//Control HandleHDC hdc;//DC handleRECT RcItem;//Hold Areaulong_ptr ItemData; } drawitemstruct;D Raw3drect (lpcrect lpRect, Colorref clrtopleft, Colorref clrbottomright); //This function is used to implement the position size of the 3D rectangle, where lprect is filled with the position size of the entire 3D rectangle,//Clrtopleft and Clrbottomright are the RGB values in the upper left and bottom right of the 3D effect, respectively. BOOL DrawFocusRect (draw a dashed rectangular hdc hdc,//handle to device contextCONST rect* LPRC//logical Coordinates ); //function function: Draw a focus rectangle. This rectangle is done by an XOR operation in the style of the flag focus (the focus is usually represented by a dot line). //If the function is called again with the same parameters, it means that the focus rectangle is removed
Here is the program code:
voidCbtnxiaowei::D rawitem (lpdrawitemstruct lpdrawitemstruct) {CString btncaption; //Save Button TitleGetWindowText (btncaption);//Get button titleCRect DrawRect;//Defining CRect ObjectsHDC dc= lpdrawitemstruct->hdc;//Control DCCdc*pdc=cdc::fromhandle (DC);//get CDC pointer through HDCUINT nstyle=lpdrawitemstruct->Ctltype; Drawrect.copyrect (& (Lpdrawitemstruct->rcitem));//Copy Control rectangle area to our CRect objectDrawframecontrol (Dc,&drawrect,dfc_menu,nstyle);//drawing the control frameCBrush Pbrush;//Create a paint brush Static intn=0; Pbrush.createsolidbrush (RGB ( -+n, the, n));//CreatePdc->fillrect (Drawrect,&pbrush);//Draw a rectanglePdc->settextcolor (M_clo);//Set Text colorCRect Textrect;//defines a crect for drawing textTextrect.copyrect (&drawrect);//Copy Rectangular AreaCSize sz=pdc->gettextextent (btncaption);//Get string Sizetextrect.top+= (Textrect.height ()-sz.cy)/2;//Adjust Text Position CenterPdc->setbkmode (TRANSPARENT);//set text background transparencyPdc->drawtext (btncaption,&textrect,dt_right| dt_center| Dt_bottom);//Draw Textn+=Ten; }voidCbtnxiaowei::settextcoler (colorref clo) {M_clo=CLO; Invalidate (); //is partially invalid causing redraw}
MFC button control self-painting