VC MFC button (CButton) control

Source: Internet
Author: User

VC MFC button (CButton) control November 11, 2012 19:15 TCEO Category: Technical Articles Views: 4,298

Button controls

1 How to display a picture on a button control

First determine what type of picture you want to display in the button control, here I am the ICO icon, because you want to display the ICO icon in the button, so to change the properties of the button control,

The method is to right-click the button control, select Properties, click the Style tab, and tick the icon.

The button control is then associated with a variable such as: M_quit, the type is "control", and the type is "control" if you do not specify it later.

The Button control class (CButton) class has a member function SetIcon can set the icon that the button displays, which has only one parameter, which is the icon handle.

Then add the following statement in the initialization function (Cfirstdlg::oninitdialog) of the dialog class:

M_quit.seticon ((HICON):: LoadImage (NULL, "E:\i.ico", Image_icon,48,48,lr_loadfromfile));//Suppose there is a i.ico icon under the E-disk

Above is the load icon file from disk, about the use of the LoadImage function, you can see the VC commonly used functions 28th function.

The second type is loaded according to the icon ID:

First import an icon from the disk into the project, assuming this icon is named Idi_icon1, will not be imported?

First go into ResourceView, then right click on icon to select Insert, then select the icon you want.

Then add this statement:

M_quit.seticon (LoadIcon (AfxGetResourceHandle (), Makeintresource (Idi_icon1)));

Remember that the statement is added in the OnInitDialog function after all code, return TRUE;

2. Display the message when the mouse hovers over the button

As I said earlier, if you do not know what to press a button control in the Control Panel, hover over the button on the Control panel and you will get a hint. How does this feature work? The method of implementation is actually very simple.
First, in the dialog Class (Cfirstdlg), add a M_tooltip class object (public: publicly), such as: CToolTipCtrl M_tooltip; then add the following statement in the OnInitDialog function in the dialog class:
M_tooltip.create (this);
M_tooltip.addtool (&m_quit, "text message");
Where m_quit is the variable associated with the button control
Then add a virtual function to the dialog class by right-clicking the dialog class and choosing Add virtual functions. Then double click on the Left list box PreTranslateMessage, add it to the right of the list box, and then double-click on the right list box PreTranslateMessage, so we add a virtual function, this virtual function has a parameter msg *pmsg; The MSG structure is explained in the API's common functions. This is just a function of saying that this function intercepts all messages sent to the corresponding window.
In this function add this statement: M_tooltip.relayevent (PMSG);
The whole thing is:
BOOL Cfirstdlg::P retranslatemessage (msg* pMsg)
{
Todo:add your specialized code here and/or call the base class
M_tooltip.relayevent (PMSG);
Return CDialog::P retranslatemessage (PMSG);
}
Then compile, run, mouse over the corresponding button, to see if there is a hint message?

3 button control self-painting

The principle of drawing a button control is very simple, get the device context (DC) of the control window, and then call the corresponding API paint function for control self-painting, all API functions that can operate on the DC can be applied to the control, such as rectangle (draw a rectangle), BitBlt (display picture) function, etc.

First we import two bitmaps to the project, one for the button press (ID number: Idb_select), and the other for the normal picture (Idb_normal)

Two bitmaps:


We know that the corresponding class for the button control is CButton, so we have to derive a class from this class.
The way to derive the CButton class is to go to the ClassView tab, right-click First Classes, select New Class ..., and then pop up a new Classes dialog box, where the name entry is your new class names, where the class name is Cnewbutton. base class (Base) Select CButton, click OK. At this point, there is an extra Cnewbutton class under first classes, which is the class we derive from the button class (CButton).
Then add the virtual function DrawItem to Cnewbutton, add the virtual function method when adding PreTranslateMessage virtual function has been explained.
This function is defined as follows:
void Cnewbutton::D rawitem (lpdrawitemstruct lpdrawitemstruct)
lpDrawItemStruct is a DRAWITEMSTRUCT structure pointer, this structure in the "online collection (reprint)" is introduced.

The code in the DrawItem letter is as follows:

Todo:add your code to draw the specified item
CDC DC;
dc. Attach (LPDRAWITEMSTRUCT->HDC); the Attach function in the//CDC class is used to convert HDC to CDC
UINT state=lpdrawitemstruct->itemstate;
CRect Buttonrect;
GetClientRect (&buttonrect);//Call the GetWindowRect function in the button class to get the size of the button area
CDC MEMDC;
Memdc.createcompatibledc (&DC);//Create a compatible DC
CBitmap bmp;
if (state&ods_selected)//If the button is pressed
{
Bmp. LoadBitmap (idb_select);//load per piece
}
Else
{
Bmp. LoadBitmap (Idb_normal);
}
BITMAP Bmpinfo;
Bmp. Getbitmap (&bmpinfo);//Get bitmap information
Memdc.selectobject (&bmp);//SELECT INTO picture
dc. StretchBlt (0,0,BUTTONRECT.RIGHT,BUTTONRECT.BOTTOM,&MEMDC,
0,0,bmpinfo.bmwidth,bmpinfo.bmheight,srccopy);//Display picture
CString str;
GetWindowText (str);//Get button text
dc. SetBkMode (TRANSPARENT);//Set text background transparent
dc. DrawText (str,&buttonrect,dt_center| dt_vcenter| Dt_singleline);//Output button text

Memdc.deletedc ();
Bmp. DeleteObject ();

dc. Detach ();

Then we add a button to the dialog box, and for the associated variable, in the Add Member Variable dialog box, the Variable Type field is selected as Cnewbutton. and set the button control's style to "owner draw"

Then include the NewButton.h (Derived button class header file) in the dialog class: #include "NewButton.h"

Then we compile, run, effects such as:

Since we only dealt with the button in the above two states, pressed, and normal state, so when the button to get the focus, or in other states, there is no corresponding action.

VC MFC button (CButton) control

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.