Buttons with 3D text in VC ++

Source: Internet
Author: User
Tags textout
Use the ownerdraw style to achieve the above purpose.

1. 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.

2. Use classwizard to create a new class: c3dtextbutton. The base class is cbutton.

3. 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 height and width of the selected font logfont; pfont-> GetObject (sizeof (logfont), & logfont); If (logfont. lfheight = 0) logfont. lfheight = 20; logfont. lfwidth = 0; // set the width to 0, and the width value is determined by the height of logfont. lfweight = 1000; logfont. lfescapement = logfont. lforientat Ion = 0; cfont tryfont; Verify (tryfont. createfontindirect (& logfont); cfont * pfontold = PDC-> SelectObject (& tryfont); // adjust the font height based on the control size, coordinate the text with the control 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. lfhei Ght, 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, 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 =: getsy Scolor (color_btntext); colorref oldtextcolor = PDC-> settextcolor (textcol); int Cx = Minx; int Cy = miny; int S = (State & ods_selected )? -1: + 1; cx + = 3; CY + = 3; // implement the 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 title PDC-> textout (CX, Cy, text ); // restore the device description table PDC-> settextcolor (oldtextcolor); PDC-> setbkmode (oldbkmode); PDC-> SelectObject (pfontold );}

4. Use classwizard to reload the drawitem function of the c3dtextbutton class. Write the Code as follows:

void C3dTextButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) {    CDC* pDC=CDC::FromHandle(lpDrawItemStruct->hDC);    ASSERT_VALID(pDC);    CRect rectClient=lpDrawItemStruct->rcItem;    Draw(pDC,rectClient,lpDrawItemStruct->itemState);}

5. Use classwizard to create a c3dtextbutton control variable m_3dtextbutton1 for idc_3dtextbtn.

6. Add "3dtextbutton. H" to the header file testdlg.

7. Compile and test the application. Now, let's take a look at the fruits of our work.

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.