Exposion Visual C ++ MFC library

zhaozj2021-02-11  224

Visual C provides powerful class libraries, basic application development requirements, but for some special requirements, such as image and text, inserted images, Chinese-style reports, etc., still appear to be heart, It is therefore necessary to create an extended MFC class library to meet the needs of actual development.

---- MFC supports the OWNER-DRAW concept, self-drawing control class, by calling the drawItem () function implementing the control of the control, due to control drawing, message detection and message comparison code is implemented in the control Realization of the window of the control, is therefore called self-drawing. Because of the reloaded DrawItem (LPDrawItemStruct function controls the external views and lines of control controls, the required parameters needed to control the control system are included in the LPDrawItemstruct structure.

---- LPDRAWITEMSTRUCT Structure:

Typedef struct tagdrawitemstruct

{

Uint CTLTYPE;

Uint CTLID;

Uint itemid;

Uint itemaction;

Uint itemState;

HWND HWNDITEM;

HDC HDC;

Rect rcitem;

DWORD ITEMDATA;

DrawItemstruct;

---- The most important parameter is Itemaction, ItemState, HDC, RCITEM, which is a realistic control, and the message response must be required.

---- Itemaction: Painting, there are few more values: ODA_DRAWENTIRE, ODA_FOCUS, ODA_SELECT.

---- ItemState: State, there are fewer values: ODS_CHECKED, ODS_DISABED, ODS_FOCUS, ODS_GRAYED, ODS_SELECTED, ODS_DEFAULT.

---- HDC: The handle of the ring.

---- Rcitem: Remote rectangle of the control outside the control.

---- Bring a CB /TON class with a lower CButton class: CMYBTN

---- Use MFC Wizard to create a new class CMYBTN, the base class is CButton

Mybtn.h:

Class CMYBTN: Public CButton

{

// construction

...

PUBLIC:

Virtual Void DrawItem

LPDrawItemstruct LPDrawItemstruct;

...

...

PUBLIC:

Void setBitmapid (UINT ID);

...

protected:

Uint m_bitmapid;

// Image of the button ID value

...

Declare_message_map ()

}

mybtn.cpp

CMYBTN :: CMYBTN ()

{

m_bitmapid = 0;

// Initialize M_bitmaoID in the constructor

}

Void CMYBTN :: DrawItem (LPDrawItemstruct LPDIS)

{

Hbitmap hbitmap = null;

Assert (lpdis! = Null); CDC * PDC = CDC :: fromHandle (LPDIS-> HDC);

// lpdis-> HDC is the handle of the equipment environment,

The fromHandle function will handle-> pointer

CRECT R1;

R1.copyRect (& lpdis-> rcitem);

// Get the rectangular range of the control

Uint State = lpdis-> itemstate;

// Get the status of the control

IF (state & ods_selected))

PDC-> DRAW3DRECT (R1, Getsyscolor

(Color_3ddkshadow),

Getsyscolor (Color_3DHIGHT));

When // selected, use color_3ddkshadow to draw the upper left upper part,

Color_hilight, the lower part of the right, the performance is depressed

Else

PDC-> Draw3DRect (R1, GetsysColor (Color_3Dhilight),

Getsyscolor (color_3ddkshadow);

// Normal, use color_3dhilight to draw the upper left upper part,

Color_3ddkshadow, the lower right lower part, appears to protrude

// Todo: add your code to draw the specified item

IF (m_bitmapid)

Hbitmap = (hbitmap) loadimage (AfxGetInstanceHandle (),

Makeintresource (m_bitmapid), image_bitmap, 0,0,

LR_DEFAULTCOLOR);

// If there is an image, the image is loaded.

CSTRING S1;

GetWindowText (S1);

// Get the CAPTION of Button

IF (! s1.isempty ())

{

INT MODE1 = PDC-> setbkmode (transparent);

IF (! hbitmap)

PDC-> DrawText (S1, R1,

DT_Center | DT_VCenter | DT_SINGLINE);

// If there is no image, the text is output throughout the Button range.

Else

{

CRECT R2 = R1;

R2.deflatecture (2, 2);

CDC MEMDC;

CBITMAP BMP;

CBITMAP * OldBitMap;

Bmp.attach (hbitmap);

Bitmap bitmap;

Bmp.GetBitmap (& Bitmap);

// The Bitmap structure can be obtained from the high, wide

MEMDC.CREATECOMPALEDC (PDC);

OldbitMap = MEMDC.SELECTOBJECT (& BMP);

PDC-> StretchBLT (R2.LEFT, R2.TOP, R2.WIDTH () / 2,

R2.height (), & MEMDC, 0, 0, Bitmap.bmwidth,

Bitmap.Bmheight, Srccopy;

// Put the image from memory to the Button range

MEMDC.SELECTOBJECT (OLDBITMAP);

bmp.detach ();

CRECT R3 = R2;

R3.Left = r2.Left r2.width () / 2;

PDC-> DrawText (S1, R3,

DT_Center | DT_VCenter | DT_SINGLINE);

}

}

}

Void CMYBTN :: SetBitmapid (UINT)

{

m_bitmapid = ID;

}

---- Use the CMYBTN class code to implement image text in the application, add the button (idc_button1), (idc_button1), (idc_button2), (idc_button2), (idc_button2) to set it with the Resource Editor to join two images. File, ID value is: IDB_bitmap1, IDB_bitmap2. Class Cws3dlg: Public CDialog

{

...

PUBLIC:

// Dialog Data

// {{AFX_DATA (CWS3DLG)

ENUM {IDD = IDD_WS3_DIALOG};

CMYBTN M_BTN1;

CMYBTN M_BTN2;

// Define image button

//}} AFX_DATA

...

protected:

Virtual Void DodataExchange (CDataExchange * PDX);

// DDX / DDV Support

//}} AFX_VIRTUAL

....

//}} AFX_MSG

Declare_message_map ()

}

Void cws3dlg :: DODATAEXCHANGE (CDATAEXCHANGE * PDX)

{

CDIALOG :: DODATAEXCHANGE (PDX);

// {{AFX_DATA_MAP (CWS3DLG)

DDX_Control (PDX, IDC_Button2, M_BTN2);

DDX_CONTROL (PDX, IDC_Button1, M_BTN1);

//}} AFX_DATA_MAP

}

Add in initialization code:

Bool cws3dlg :: oninitdialog ()

{

...

// Todo: Add Extra Initialization Here

m_btn1.setbitmapid (idb_bitmap1);

m_btn2.setBitmapId (idb_bitmap2);

Return True; // Return True UnsS

You set the focus to a control

}

.

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

New Post(0)