VC ++ pressing a 10D text

zhaozj2021-02-11  188

Use the OwnerDraw style to achieve the above purposes.

1. Run AppWizard Generate a dialog-based TEST project and add a CButton control in the dialog. CAPTION changes to IDC_3DTextBTN, caption changes to IDC_3DTextBTN, CAPTION changes to "Who and Crazy", and the remaining settings remain default.

2. Create a new class with ClassWizard: C3DTextButton, the base class is CButton.

3. Add a protected function Void Draw (CDC * PDC, Const CRECT & Recorplication to the C3DTextButton class. Writing code as follows:

Void C3DTextButton :: Draw (CDC * PDC, Const CRECT & Recorpted UINT STATE)

{

Cstring text; getWindowText (Text);

INT L = text.getlength ();

CRECT RectClient = Rect;

// Get the font of the control

Cfont * pfont = getFont ();

/ / Determine the efficient height and width of the selected font

Logfont logfont;

PFONT-> GetObject (Sizeof (Logfont), & logfont;

IF (logfont.lfheight == 0) logfont.lfheight = 20;

Logfont.lfwidth = 0; // The width is set to 0, 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);

/ / According to the size of the control, adjust the height of the font, make the text and control coordination

CSIZE TEXTSIZECLIENT = PDC-> GetTextExtent (Text, L);

IF (RectClient.width () * textsizeclient.cy> RectClient.Height () * TextSizeClient.c

X) {

Logfont.lfheight = :: muldiv (logfont.lfheight, RectClient.Height (), TextSizec

Lient.cy);

}

Else {

Logfont.lfheight = :: muldiv (logfont.lfheight, RectClient.width (), Textsize

Client.cx);

}

// Create and select a coordinated font

CFont Font; Font.createFontIndirect (& Logfont);

PDC-> SELECTOBJECT (& FONT);

TextsizeClient = PDC-> GetTextExtent (Text, L);

/ / Determine the distance between the text and the control border 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 = :: 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;

// Implement 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);

PDC-> Textout (CX, CY-S * 1, TEX);

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 device description table

PDC-> SetTextColor (OldTextColor);

PDC-> setBkmode (OldBkmode);

PDC-> SelectObject (pfontold);

}

4. Rebate the DrawItem function of the C3DTextButton class with the ClassWizard. The write code is as follows:

Void C3dTextButton :: DrawItem (LPDrawItemstruct LPDrawItemstructure)

{

CDC * PDC = CDC :: fromHandle (LPDrawItemstruct-> HDC);

Ask_VALID (PDC);

CRECT RectClient = lpdrawItemstruct-> rcitem;

Draw (PDC, RectClient, LPDRAWITEMStruct-> itemState);

}

5. Create a C3DTextButton control variable m_3dtextButton1 with ClassWizard for IDC_3DTextBTN.

6. Add "3DTextButton.h" to the TestDLG header file.

7. Compile and test the application. Ok, you can enjoy the work of labor.

转载请注明原文地址:https://www.9cbs.com/read-3945.html

New Post(0)