Expand the Visual C ++ MFC Library

Source: Internet
Author: User
Tags drawtext
Visual c ++ provides a class library with high performance. Based on the requirements for development, however, for some special requirements, for example, if the graph image and text are in a newline, the graph image is inserted into the column-Table box, and the Chinese-style table is reported, the Force remains unchanged, for this reason, it is necessary to create and expand the MFC library to meet the needs of real-time development.

----MFC supports the owner-draw concept. The control class of the Self-painting system implements the painting of the current controller by calling the drawitem () function, based on the control component painting system, the Information Consumption check and information consumption ratio code is implemented in the control component rather than in the window port with the control component, because it is called self-painting. The out-of-sight and row control of the control component is implemented by overloading the drawitem (lpdrawitemstruct function, all packages 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 parameters are itemaction, itemstate, HDC, and rcitem. They are in the out-of-sight plotting of the current controller, which is required for information cancellation.

----Itemaction: plotting, which has the following values: oda_drawentire, oda_focus, oda_select.

----Itemstate: State, which has the following values: ods_checked, ods_disabled, ods_focus, ods_grayed, ods_selected, ods_default.

----HDC: Set the sentence handle of the standby environment.

----Rcitem.

----The following shows a cbutton-based graph image. The button class is cmybtn.

----Use MFC Wizard to create a new cmybtn class. The base class is cbutton.

Mybtn. h: Class cmybtn: Public cbutton {// construction... public: Virtual void drawitem (lpdrawitemstruct );...... public: void setbitmapid (uint ID );... protected: uint m_bitmapid; // the ID of the image... declare_message_map ()}; mybtn. CPP cmybtn: cmybtn () {m_bitmapid = 0; // initialize m_bitmaoid} void cmybtn: drawitem (lpdrawitemstruct lpdis) {hbitmap = NULL; Assert (lpdis! = NULL); CDC * PDC = CDC: fromhandle (lpdis-> HDC); // lpdis-> HDC is the handle of the device environment, the fromhandle function returns the handle-> pointer crect R1; r1.copyrect (& lpdis-> rcitem); // obtain the control's rectangular range uint state = lpdis-> itemstate; // obtain the control state if (State & ods_selected) PDC-> draw3drect (R1, getsyscolor (color_3ddkshadow), getsyscolor (color_3dhilight); // when selected, use color_3ddkshadow to draw the left upper part and color_hilight to draw the right lower part, which is displayed as the else PDC-> draw3drect (R1, getsyscolor (color_3dhiligh T), getsyscolor (color_3ddkshadow); // when the image is normal, use color_3dhilight to draw the left upper part, and color_3ddkshadow to draw the right lower part, which is displayed as a protrusion. // todo: add your code to draw the specified itemif (m_bitmapid) hbitmap = (hbitmap) LoadImage (Region (), makeintresource (m_bitmapid), image_bitmap, lr_defaultcolor); // if there is an image, load the image, cstring S1; getwindowtext (S1); // get the caption if (! S1.isempty () {int mode1 = PDC-> setbkmode (transparent); If (! Hbitmap) PDC-> drawtext (S1, R1, dt_center | dt_vcenter | dt_singleline); // if there is no image, the text else {crect r2 = R1 is output in the entire button range; r2.deflaterect (2, 2); CDC memdc; cbitmap BMP; cbitmap * oldbitmap; BMP. attach (hbitmap); Bitmap bitmap; BMP. getbitmap (& Bitmap); // the height and width of the image can be obtained from the bitmap structure. createcompatibledc (PDC); oldbitmap = memdc. selectObject (& BMP); PDC-> stretchblt (r2.left, r2.top, r2.width ()/2, r2.height (), & memdc, 0, 0, bitmap. bmwidth, bitmap. bmheight, srccopy); // compress the image from the memory and copy it to the memdc in the button range. selectObject (oldbitmap); BMP. detach (); crect R3 = R2; r3.left = r2.left + r2.width ()/2; PDC-> drawtext (S1, R3, dt_center | dt_vcenter | dt_singleline );}}} void cmybtn: setbitmapid (uint ID) {m_bitmapid = ID ;}

----Cmybtn code can be used to implement image text buttons in the application system, add the buttons (idc_button1) to the dialog, and set their attributes to owner-draw (idc_button2. use resource editor to add two image files with the ID values: idb_bitmap1 and idb_bitmap2.

Class cws3dlg: Public cdialog {... public: // dialog data // {afx_data (cws3dlg) Enum {IDD = idd_ws3_dialog}; cmybtnm_btn1; cmybtnm_btn2; // define the image button //} afx_data... protected: Virtual void dodataexchange (cdataexchange * PDX); // DDX/DDV support //} afx_virtual .... //} configure ()}; 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} added: bool cws3dlg: oninitdialog (){... // todo: add extra initialization here m_btn1.setbitmapid (idb_bitmap1); m_btn2.setbitmapid (idb_bitmap2); Return true; // return true unless you set the focus to a control}

----In this article, the real-time code of the Self-painted class is provided. The reader can use this method to develop and expand the class library, fengfu has developed a fund source.

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.