VC ++ development spam file cleanup software 4: Program Interface Design and Implementation-button control interface

Source: Internet
Author: User
Tags drawtext

In the above section, I briefly introduced the design and implementation of the dialog box interface. The following describes the implementation of button control re-painting:

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 you move the mouse away from 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 //  Drawing status-in the button Area  # Define St_moveout 1 // Drawing status-not in the button Area  Int M_drawstate; //  Drawing status      Int M_nbmpid; //  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 and their implementation:

 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 (); //  Text on the draw button  }  Void  Mydrawbutton: drawtext (){  //  The font size and style of the text on the Drawing button.  Cstring itemstring; crect clientrect; cclientdc (  This  ); Getclientrect ( &Clientrect); getwindowtext (itemstring );  If  (Itemstring) {csize size = Dc. gettextextent (itemstring ); //  Obtains 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 Location  //  Calculate the output location of the text X = (rectwidth-textwidth )/ 2 ; //  Horizontal Center Y = (rectheight-textheight )/ 2 ; //  Vertical center          Switch  (M_drawstate ){ Case St_movein: //  Move the mouse to the button Area M_cltext = M_clactivetext;  Break  ;  Case St_moveout: //  Move the mouse away from 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 , 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 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 (  14 , 94 , 157 )); //  Draw a bitmap in the window. RGB (157,) 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 (take the scan button as an example). Assume that its id value is idc_test. Go to the member variable attribute page of the Class Wizard, and add a variable m_btnbegin for IDC _ begin. As follows:

Mydrawbutton m_btnbegin;

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

M_btnbegin.setbkbmp (idb_btn210x95, idb_btn210x95_3); // idb_btn210x95 and idb_btn210x95 are the default bitmap and the bitmap of the mouse in the button area, respectively.

Up to now, the button class has been re-painted, and you can define your favorite style buttons at will. Now the software of a well-formed spam cleanup tool has been developed.

The main result interface of the software is as follows:

Open the preparation Interface

 

Running section chart

 

Scan completion page:

 

Interface after cleaning

 Source code: Http://download.csdn.net/detail/jczmdeveloper/4998982

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.