Re-draw 3-button re-painting in the dialog box in VC ++

Source: Internet
Author: User

Programming in MFC is often not very satisfied with the standard button control and wants to make it more beautiful. This requires button re-painting. The Common Implementation of the redraw button is to override the cbutton class.

First, add a self-painted button class mydrawbutton to the project. The base class is cbutton. To enable the button to have the auto-drawing function, you must add the bs_ownerdraw attribute to the button. Overload the presubclasswindow virtual function for the class cbutton. Add the following line to the function:Code:

Setbuttonstyle (getbuttonstyle () | bs_ownerdraw );

After the button control has the self-painting function, the drawitem function is triggered every time the control state changes. In this function, the form and appearance of the button are drawn. Therefore, the drawitem virtual function must be reloaded in step 2. This function can be used freely, such as drawing the background, background color, button title, and text font style.

You can define the appearance of a button in several different states, such as the status when the cursor slides out of date, the status when the button is pressed, the status when the button is disabled, and the normal status of the button. This requires several important message responses for the new button. For example, wm_mouseleave message, wm_mousehover message, and wm_mousemove message. It is worth mentioning that the response functions of the first two messages must be manually added, microsoft provides a trackmouseevent function to deliver wm_mouseleave messages when the cursor leaves a window, and wm_mousehover messages when the cursor slides over the window. Generally, you can call the trackmouseevent function in the wm_mousemove message response function to deliver wm_mouseleave messages and wm_mousehover messages. Then, mark "the cursor has left button" in the response function of the wm_mouseleave message, and call the invalidaterect function to re-paint the button. In the response function of the wm_mousehover message, mark "the cursor is above the button" and call the invalidaterect function to re-paint the button.

In this article, the redraw button is divided into three parts.

(1) Draw the button background style, that is, to draw the background BMP bitmap, so that the button has a custom style. At the same time, the transparentblt () function is used to draw the output bitmap of the button background, this function integrates the background of the displayed bitmap on the form with the background color of the form. It not only displays the button BMP bitmap style, but also makes the background transparent.

(2) It is the text on the Drawing button. It mainly draws the text style on the button, including the font size, Font Style, font color and other attributes.

(3) Implement the appearance style of buttons in different states, including the message processing functions of wm_mousemove and wm_mouseleave messages. The mouse is in the button area and not in the button area. To move the mouse to the button area, you need to use a timer to mark whether the mouse is still in the button area. Start the timer in wm_mousemove, and terminate the timer when the wm_mouseleave message is triggered. The main code of the timer is as follows:

Void mydrawbutton: ontimer (uint_ptr nidevent)

{

// Todo: Add message processing hereProgramCode and/or call Default Value

If (nidevent! = 24)

Return;

Cpoint point;

Crect rect;

Getwindowrect (& rect );

Getcursorpos (& Point );

// If the mouse leaves the button area, redraw the button

If (! Rect. ptinrect (point) & m_bmove)

{

Killtimer (24 );

M_drawstate = st_moveout;

M_bmove = false;

Draw ();

}

Cbutton: ontimer (nidevent );

}

The main implementation code of the redraw button class mydrawbutton is as follows:

The following variables are used for repainting:

# Define st_movein 0 // draw status-in the button Area

# Define st_moveout 1 // draw status-not in the button Area

Int m_drawstate; // draw status

Int m_nbmpid; // The resource ID of the currently displayed background BMP bitmap

Bool m_bmove; // whether the mouse enters the button Area

Colorref m_cltext; // current text color

Colorref m_clactivetext; // text color when the mouse enters the button Area

Colorref m_clnormaltext; // text color when the mouse leaves the button Area

Message Processing functions and defined functions:

Void mydrawbutton: presubclasswindow ()

{

Setbuttonstyle (getbuttonstyle () | bs_ownerdraw );

}

Void mydrawbutton: drawitem (lpdrawitemstruct)

{

Draw (); // draw button

}

Void mydrawbutton: Draw () // draw button

{

Drawbackground (); // draw button BMP bitmap and make the background transparent

Drawtext (); // The text on the draw button

}

Void mydrawbutton: drawtext ()

{// Font size and style of the text on the Drawing button

Cstring itemstring;

Crect clientrect;

Cclientdc DC (this );

Getclientrect (& clientrect );

Getwindowtext (itemstring );

If (itemstring)

{

Csize size = Dc. gettextextent (itemstring); // obtain the height and width of the specified string in the selected font.

Int rectwidth = clientrect. Width ();

Int rectheight = clientrect. Height ();

Int textwidth = size. CX;

Int textheight = size. Cy;

Int X, Y; // text position

// Calculate the output position of the text

X = (rectwidth-textwidth)/2; // horizontally centered

Y = (rectheight-textheight)/2; // center vertically

Switch (m_drawstate)

{

Case st_movein: // enter the button Area

M_cltext = m_clactivetext;

Break;

Case st_moveout: // move the cursor out of the button Area

M_cltext = m_clnormaltext;

Break;

Default:

M_cltext = m_clnormaltext;

Break;

}

DC. settextcolor (m_cltext );

DC. setbkmode (transparent );

Cfont * font;

Font = new cfont ();

Int fontsize = 14; font-> createfont (fontsize, 0, 0, fw_bold, false, false, 0, ansi_charset, out_default_precis, clip_default_precis, default_quality, ff_swiss, _ T (" "));

DC. SelectObject (font );

DC. textout (X, Y, itemstring );

}

}

Void mydrawbutton: setbkbmp (INT nbmpid)

{// Set the button BMP bitmap Style

M_nbmpid = nbmpid;

}

Void mydrawbutton: drawbackground ()

{// Draw button BMP bitmap and make the background transparent

Crect winrc;

CDC * PDC = getwindowdc ();

CDC memdc;

Memdc. createcompatibledc (PDC );

Bitmapinfo BMP Info;

Cbitmap BMP;

Getwindowrect (& winrc );

BMP. loadbitmap (m_nbmpid );

BMP. GetObject (sizeof (bitmapinfo), & BMP info );

Int nbmpcx = BMP info. bmiheader. biwidth;

Int nbmpcy = BMP info. bmiheader. biheight;

Memdc. SelectObject (BMP );

PDC-> transparentblt (0, 0, nbmpcx, nbmpcy, & memdc, 0, 0,

Nbmpcx, nbmpcy, RGB (157, 157); // draw a bitmap in the window, RGB (,) is transparent

BMP. deleteobject ();

Releasedc (PDC );

}

At this point, the custom re-painting of the button is complete, and then you can use the self-Repainting button class mydrawbutton. First, add a button control to the dialog box, assuming its id value is idc_test. Go to the member variables property page of the Class Wizard, and add a variable M _ testbutton for IDC _ test. As follows:

Mydrawbutton m_testbutton;

Then you can call the mydrawbutton method to set the button style. As follows:

M_testbutton.setbkbmp (idb_test); // idb_test indicates the ID of the configured BMP bitmap resource.

Up to now, the button class has been re-painted, and you can define your favorite style buttons at will. The dialog box with custom buttons is repainted as follows:

 

Now, the re-drawing of the dialog box is complete. I hope it will help you.

DownloadSource codeCan be downloaded to my csdn: http://download.csdn.net/detail/jczmdeveloper/5004541

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.