This article reproduced to (http://www.vckbase.com/bbs/prime/viewprime.asp? Id = 329)
Programming of 3D buttons in VC ++
Run Appwizard to generate a test project based on the dialog box, and add a cbutton control to the dialog box. On the general properties page of The cbutton control, change the control ID to idc_3dtextbtn, set caption to "who is crazy", select ownerdraw on the styles property page of the control, and keep the remaining settings by default.
Use classwizard to create a new class: c3dtextbutton. The base class is cbutton. Add a protected function void draw (CDC * PDC, const crect & rect, uint State) to the c3dtextbutton class ). Write the Code as follows:
Void c3dtextbutton: Draw (CDC * PDC, const crect & rect, uint state)
{
Cstring text; getwindowtext (text );
Int L = text. getlength ();
Crect rectclient = rect;
// Obtain the control font
Cfont * pfont = getfont ();
// Determine the valid font height and width
Logfont;
Pfont-> GetObject (sizeof (logfont), & logfont );
If (logfont. lfheight = 0) logfont. lfheight = 20;
Logfont. lfwidth = 0; // The width is set to 0, and the width value is determined by the height.
Logfont. lfweight = 1000;
Logfont. lfescapement = logfont. lforientation = 0;
Cfont tryfont; Verify (tryfont. createfontindirect (& logfont ));
Cfont * pfontold = PDC-> SelectObject (& tryfont );
// Adjust the font height based on the widget size to coordinate the text with the widget
Csize textsizeclient = PDC-> gettextextent (text, L );
If (rectclient. Width () * textsizeclient. Cy> rectclient. Height () * textsizeclient. CX)
{
Logfont. lfheight =: muldiv (logfont. lfheight, rectclient. Height (), textsizeclient. Cy );
}
Else {
Logfont. lfheight =: muldiv (logfont. lfheight, rectclient. Width (), textsizeclient. CX );
}
// Create and select the coordinated font
Cfont font; font. createfontindirect (& logfont );
PDC-> SelectObject (& font );
Textsizeclient = PDC-> gettextextent (text, L );
// Determine the distance between the text and the control boundary Minx and miny
Int Minx = rectclient. Left + (rectclient. Width ()-textsizeclient. CX)/2;
Int miny = rectclient. Top + (rectclient. Height ()-textsizeclient. Cy)/2;
Int oldbkmode = PDC-> setbkmode (transparent );
Colorref textcol =: getsyscolor (color_btntext );
Colorref oldtextcolor = PDC-> settextcolor (textcol );
Int Cx = Minx;
Int Cy = miny;
Int S = (State & ods_selected )? -1: + 1;
Cx + = 3; CY + = 3;
// 3D effect
PDC-> settextcolor (: getsyscolor (color_3ddkshadow ));
PDC-> textout (CX-S * 2, Cy + S * 2, text );
PDC-> textout (cx + S * 2, cy-S * 2, text );
PDC-> textout (cx + S * 2, Cy + S * 2, text );
PDC-> settextcolor (: getsyscolor (color_3dhilight ));
PDC-> textout (cx + S * 1, cy-S * 2, text );
PDC-> textout (CX-S * 2, Cy + S * 1, text );
PDC-> textout (CX-S * 2, cy-S * 2, text );
PDC-> settextcolor (: getsyscolor (color_3dshadow ));
PDC-> textout (CX-S * 1, Cy + S * 1, text );
PDC-> textout (cx + S * 1, cy-S * 1, text );
PDC-> textout (cx + S * 1, Cy + S * 1, text );
PDC-> settextcolor (: getsyscolor (color_3dlight ));
PDC-> textout (CX, cy-S * 1, text );
PDC-> textout (CX-S * 1, Cy, text );
PDC-> textout (CX-S * 1, cy-S * 1, text );
PDC-> settextcolor (textcol );
// Output the title
PDC-> textout (CX, Cy, text );
// Restore the device description table
PDC-> settextcolor (oldtextcolor );
PDC-> setbkmode (oldbkmode );
PDC-> SelectObject (pfontold );
}
Use classwizard to overload the drawitem function of the c3dtextbutton class. Write the Code as follows:
Void c3dtextbutton: drawitem (lpdrawitemstruct)
{
CDC * PDC = CDC: fromhandle (lpdrawitemstruct-> HDC );
Assert_valid (PDC );
Crect rectclient = lpdrawitemstruct-> rcitem;
Draw (PDC, rectclient, lpdrawitemstruct-> itemstate );
}
Use classwizard to create a c3dtextbutton control variable m_3dtextbutton1 for idc_3dtextbtn.
Add "3dtextbutton. H" to the header file testdlg. Compile and test the application. OK! Let's see the effect.