Vc mfc Toolbar Control

Source: Internet
Author: User

From: http://hi.baidu.com/rainminism/item/f2ad45d45883af2c39f6f71c

Toolbar

The Toolbar Control does not have the corresponding option (icon) in the control panel, but there is a toolbar control class ctoolbar, so if we want to create a toolbar control and display it in the window, you can only use code. In fact, any control can be created using code, such as the cbutton button class. Here is an example of creating a button control with code and displaying it in a window.

The create member function of the cbutton class can create a button control. The format of this function is as follows:
Bool cbutton: Create (
Lpctstr lpszcaption, // text displayed by the button
DWORD dwstyle, // button style
Const rect & rect, // The area (size) displayed by the button control relative to the parent window
Cwnd * pparentwnd, // The parent window of the button (which window does the button belong)
Uint NID // specifies the ID of the button control

);
The second parameter of this function, dwstyle, is the style of the button control, that is, when you add a button control visually, right-click the button control and select Properties. A dialog box is displayed, this dialog box has a style option. This parameter corresponds to this style.

Now that we understand the above, let's create a button dynamically. First, add a member variable cbutton m_button in the corresponding class;

In the dialog box editing area, add a button control to the dialog box (click this button to create a button control), add this button control, click the message processing function, and add the following statement to the function:

Rect buttonrect; // The area (size) displayed by the button control relative to the parent window
Buttonrect. Left = 10;
Buttonrect. Top = 10;
Buttonrect. Right = 80;
Buttonrect. Bottom = 30;
M_button.create ("dynamic creation", ws_child, buttonrect, this, 1115 );
M_button.showwindow (sw_show); // display the button control

Now that you know how to dynamically create a button control, you can create a toolbar control.

First, add a ctoolbar class member variable ctoolbar m_toolbar to the dialog class;

Define the toolbar command ID in the dialog file.

# Define id_button1 501
# Define id_button2 502
# Define id_button3 503

Next, we will create toolbar resources for displaying images of toolbar buttons.

You can enter the resourceview tab, select an option, right-click Insert, select the toolbar in the displayed dialog box, and click Create.

Create three buttons. As shown in:

Now, change the three buttons to the ones defined in the macro above. If you want to change the "day" button, select the button and press enter, a dialog box will pop up,

Enter id_button1 in the ID field. Then, change the two idnumbers to id_button2 and id_butoon3. In this dialog box, you can also change the toolbar button size.

Next, after all the code in the oninitdialog function of the dialog box class, add the following statement between return true and:
M_toolbar.create (this); // create a Toolbar Control
M_toolbar.loadtoolbar (idr_toolbar1); // load toolbar Resources
Crect rect (100,100,200,200 );//
M_toolbar.movewindow (rect); // move the toolbar to its parent window
M_toolbar.showwindow (sw_show); // display the toolbar

However, it is not very convenient to display the toolbar, especially when the window size is changed and the position of the toolbar in the window needs to be calculated. Is there any way to automatically adjust the toolbar to the appropriate position based on the window size? The answer is yes. The repositionbars function can. The implementation method is as follows:

M_toolbar.create (this); // create a Toolbar Control
M_toolbar.loadtoolbar (idr_toolbar1); // load toolbar Resources
// This function is used to display the toolbar and automatically adjust the position of the toolbar according to the window size.
Repositionbars (afx_idw_controlbar_first, afx_idw_controlbar_last, 0 );

WhileResponse toolbar button click message, Similar to the previous Add button, click the message to enter the Class Wizard, find the toolbar button ID to be added, and add the Command Message response function, such:

Display the true color bitmap on the toolbar button

The preceding example has a defect, that is, the figure displayed by the toolbar button cannot exceed 256 colors. If I want to display the true color bitmap in the toolbar button, the display method above cannot be implemented, how can this function be implemented?

The setimagelist function can do this. to display a true color bitmap, you cannot use the toolbar resources (256 colors). Therefore, you can use code to implement everything.

First, introduce six bitmaps to the project (images used for toolbar buttons). Assume that the idnumbers are idb_bitmap1, idb_bitmap2, idb_bitmap3, idb_bitmap4, idb_bitmap5, idb_bitmap6, And the size is 50x38.

Step 1: Add two member variables in the dialog box:

Ctoolbar m_toolbar;
Cimagelist m_imagelist;

Step 2: Add the following macro definition to the dialog file:
# Define id_button1 501
# Define id_button2 502
# Define id_button3 503
# Define id_button4 504
# Define id_button5 505
# Define id_button6 506

Next, add the following statement after the oninitdialog function in the dialog box initialization function:

M_imagelist.create (50, 38, ilc_color24 | ilc_mask,); // The image size must be consistent.
Cbitmap BMP;
For (INT I = 0; I <6; I ++)
{
BMP. loadbitmap (idb_bitmap1 + I );
M_imagelist.add (& BMP, RGB (0, 255,255,255 ));
BMP. deleteobject ();
}
Uint narray [6];
For (I = 0; I <6; I ++)
{
Narray [I] = id_button1 + I;
}
M_toolbar.createex (this); // create a toolbar control. The toolbar created by the createex function has the floating button attribute by default.
M_toolbar.setbuttons (narray, 6); // create six tool buttons, and set the ID number in sequence, corresponding to the elements of the narray Array
M_toolbar.setsizes (csize (60, 56), csize (50, 38); // The first function is the button size, the second is the image size, the button must be larger than the image, specifically the button

// The image size is 7 or higher and 6 higher than that of the image.
M_toolbar.gettoolbarctrl (). setimagelist (& m_imagelist); // sets the image
Repositionbars (afx_idw_controlbar_first, afx_idw_controlbar_last, 0 );

The running effect is as follows:

If you want to click the message button in the response toolbar, you must manually add the message ing, as shown in figure

On_command (id_button1, button1) // button1 is the message processing function.

If you want to set the button to display text, it is the setbuttontext function of the ctoolbar class.

Set toolbar background bitmap

You can use the crebar class to display bitmaps In the toolbar background. You can set the toolbar to a subwindow corresponding to the crebar class. The above project is an example, and then add a member variable to the dialog class: crebar m_rebar; then, import a bitmap and use it as the background of the toolbar. the ID number is id_toolback, and then in the oninitdialog Function

Repositionbar (afx_idw_controlbar_first, afx_idw_controlbar_last, 0); Add the following statement before the statement:
M_rebar.create (this); // create a window (Control)
M_rebar.addbar (& m_toolbar); // Add m_toolbar as a subwindow
M_rebar.redrawwindow (); // redraw a window
Rebarbandinfo Info;
Info. cbsize = sizeof (Info );
Info. fmask = rbbim_background;
M_toolbar.modifystyle (0, tbstyle_transparent); // you can specify the transparent background color of the toolbar.
Info. hbmback = loadbitmap (AfxGetInstanceHandle (), makeintresource (idb_toolback); // load the bitmap
M_rebar.getrebarctrl (). setbandinfo (0, & info );

Compile and run as follows:

Add a mouse stop prompt for the toolbar button

The preceding project is used as an example to add a member variable cstring STR in the dialog class;

Then, after m_toolbar calls the createex function, it calls this statement: m_toolbar.enabletooltips (); // activates the information prompt function.

Then add a member function (for ttn_needtext message processing function) to the dialog class. The format is as follows:

Bool ondisplay (uint ID, nmhdr * pnmhdr, lresult * presult)

Then add the ttn_needtext message ing macro.

On_policy_ex (ttn_needtext, 0, ondisplay)

The ondisplay Function Code is as follows:

Tooltiptext * pttt = (tooltiptext *) pnmhdr;
Uint nid = pnmhdr-> idfrom; // obtain the toolbar button ID
Uint nindex = m_toolbar.commandtoindex (NID); // obtain the index based on the button ID
Str. Format ("toolbar button % d", nindex );
Pttt-> lpsztext = Str. getbuffer (Str. getlength (); // set the button prompt information
Pttt-> hinst = afxgetresourcehandle ();
Return true;

Icon displayed on the toolbar button

Display the icon on the button. The method is no different from the display bitmap. Just add the icon to the cimagelist. First, introduce the six icons to the project, assume that the ID numbers are idi_icon1 and idi_icon2...idi_icon6, respectively.

The above project is used as an example: Add the following code at the end of the oninitdialog function:

M_imagelist.create (48, 48, ilc_color24 | ilc_mask,); // The image size must be consistent.
For (INT I = idi_icon1; I <= idi_icon6; I ++)
M_imagelist.add (afxgetapp ()-> loadicon (I); // Add an icon

Uint narray [6];
For (I = 0; I <6; I ++)
{
Narray [I] = id_button1 + I;
}
M_toolbar.createex (this); // create a Toolbar Control
M_toolbar.enabletooltips (); // activate the information prompt Function
M_toolbar.setbuttons (narray, 6 );
M_toolbar.setsizes (csize (60, 56), csize (48, 48 ));
M_toolbar.gettoolbarctrl (). setimagelist (& m_imagelist );
M_rebar.create (this );
M_rebar.addbar (& m_toolbar );
M_rebar.redrawwindow ();
Rebarbandinfo Info;
Info. cbsize = sizeof (Info );
Info. fmask = rbbim_background;
M_toolbar.modifystyle (0, tbstyle_transparent); // you can specify the transparent background color of the toolbar.
Info. hbmback = loadbitmap (AfxGetInstanceHandle (), makeintresource (idb_toolback ));
M_rebar.getrebarctrl (). setbandinfo (0, & info );
Repositionbars (afx_idw_controlbar_first, afx_idw_controlbar_last, 0 );

The running effect is as follows:

Set the toolbar button hotspot Image

For example, the preceding setimagelist function sets the image when the toolbar button is normally displayed. The sethotimagelist function sets the image of the tool hotspot button, that is, the image displayed when the mouse stays on the button, the usage is the same as that of the setimagelist function, but the image associated with the cimagelist class is different.

To display the hotspot button image, we have to prepare two groups of images, one for normal display and the other for hotspot display. Here, my image format is an icon. Note that the toolbar must have the tbstyle_flat (floating button) attribute, which is already included by default when the createex function is used to create the toolbar.

Two groups of images:

Normal display:

Hotspot display:

Toolbar background bitmap:

First, introduce these 12 icons to the project: Assuming the normal display of the icon ID numbers are: idi_normal1, idi_normal2 ,... Idi_normal6

Hotspot icon IDs are: idi_hot1, idi_hot2, and ,... Idi_hot6

Then introduce the background bitmap with the idb_toolback ID.

After doing this, we will add four member variables to the dialog class:

Ctoolbar m_toolbar;
Cimagelist m_imagelist;
Cimagelist m_hotimagelist;

Crebar m_rebar;

Then add five macro definitions:

# Define id_button1 501
# Define id_button2 502
# Define id_button3 503
# Define id_button4 504
# Define id_button5 505

Then add the following code in the oninitdialog function:

M_imagelist.create (48, 48, ilc_color24 | ilc_mask,); // The image size must be consistent.
M_hotimagelist.create (48, 48, ilc_color24 | ilc_mask, 1, 1 );
For (INT I = 0; I <5; I ++)
{
M_imagelist.add (afxgetapp ()-> loadicon (idi_normal1 + I ));
M_hotimagelist.add (afxgetapp ()-> loadicon (idi_hot1 + I ));
}
Uint narray [5];
For (I = 0; I <5; I ++)
{
Narray [I] = id_button1 + I;
}
M_toolbar.createex (this); // create a Toolbar Control
M_toolbar.setbuttons (narray, 5 );
M_toolbar.setsizes (csize (60, 56), csize (48, 48 ));
M_toolbar.gettoolbarctrl (). setimagelist (& m_imagelist );
M_toolbar.gettoolbarctrl (). sethotimagelist (& m_hotimagelist );
M_rebar.create (this );
M_rebar.addbar (& m_toolbar );
M_rebar.redrawwindow ();
Rebarbandinfo Info;
Info. cbsize = sizeof (Info );
Info. fmask = rbbim_background;
M_toolbar.modifystyle (0, tbstyle_transparent); // you can specify the transparent background color of the toolbar.
Info. hbmback = loadbitmap (AfxGetInstanceHandle (), makeintresource (idb_toolback ));
M_rebar.getrebarctrl (). setbandinfo (0, & info );
Repositionbars (afx_idw_controlbar_first, afx_idw_controlbar_last, 0 );

Running effect:

Ctoolbarctrl Toolbar Control class

The usage of this class is similar to that of ctoolbar, And the modes are similar. You only need to know the create and addbuttons functions in this class. The create function is defined as follows:

Bool create (DWORD dwstyle, const rect & rect, cwnd * pparentwnd, uint NID );

Dwstyle: Toolbar Control style rect: toolbar position and region pparentwnd: toolbar parent window handle (which window belongs) NID: Toolbar Control ID

The addbuttons function is used to add a group of buttons to the toolbar. The function is defined as follows:

Bool addbuttons (INT nnumbuttons, lptbbutton lpbuttons );
Nnumbuttons: the number of buttons to be added, that is, the array size of the second parameter of the function. Lpbuttons: address of a tbbutton structure array

The tbbuton structure is defined as follows:
Typedef struct _ tbbutton
{
Int ibitmap; // The image index displayed by the button. No image is null,
Int idcommand; // command identifier associated with this button. This parameter must be set to 0 when fsstyle has the tbstyle_sep attribute.
Byte fsstate; // The status flag of the button
Byte fsstyle; // button style
DWORD dwdata; // user-defined data

Int istring; // text content index displayed by the button. The value is null.
} Tbbutton;

The following describes the fsstate and fsstyle parameters in the "MFC class library explanation:

The status flag of the fsstate button. It can be a combination of values listed below: · tbstate_checked this button has the tbstyle_checked style and is pressed. · The tbstate_enabled button receives user input. A button that does not have this status does not receive user input and changes to Gray. · The tbstate_hidden button is invisible and cannot receive user input. · The tbstate_indeterminate button is grayed out. · The tbstate_pressed button is pressed. · The tbstate_wrap button is followed by a separator line. This button must also be in the tbstate_enabled status.
Sstyle button style. It can be a combination of the following values: · tbstyle_button creates a standard button. · Tbstyle_check creates a button that can be switched between the press and the pop-up status when each user clicks. This button has a different background color when it is pressed. · Tbstyle_checkgroup creates a check button, which remains in the pressed state after being selected until another button in the same group is pressed. · Tbstyle_group: a button that is selected and pressed until another button in the same group is pressed. · Tbstyle_sep
Create a separator to provide a small gap between button groups. Buttons with this style do not receive user input.

To use the ctoolbarctrl class to display a toolbar in a window, follow these steps:

Introduce four Icon files to the project, and then define two variables in the dialog box class:

Cimagelist m_imagelist;
Ctoolbarctrl m_tbarctrl;

Then add the following code in the oninitdialog function:

Tbbutton button [4];
M_imagelist.create (32, 32, ilc_color32 | ilc_mask, 0, 0 );
M_imagelist.add (afxgetapp ()-> loadicon (idi_icon1 ));
M_imagelist.add (afxgetapp ()-> loadicon (idi_icon2 ));
M_imagelist.add (afxgetapp ()-> loadicon (idi_icon3 ));
M_imagelist.add (afxgetapp ()-> loadicon (idi_icon4 ));
M_tbarctrl.create (ws_child | ws_visible, crect (1200,), this );
M_tbarctrl.setimagelist (& m_imagelist );
For (INT I = 0; I <4; I ++)
{
Button [I]. fsstate = tbstate_enabled;
Button [I]. fsstyle = tbstyle_button;
Button [I]. ibitmap = I;
}
M_tbarctrl.addbuttons (4, button );
M_tbarctrl.autosize ();
M_tbarctrl.setstyle (tbstyle_flat | ccs_top );

Running effect:

Click the Add button on the toolbar created for the ctoolbarctrl class to process messages.

In the above tbbuton structure, there is an idcommand member. This member is the same as the id value of the menu item and is used to identify the toolbar button item. That is to say, idcommand is the toolbar item ID, select a button when setting toolbar resources at the beginning, and press enter to bring up a dialog box with an ID item, which is the same as this one.

So, we just need to change the above statement to assign values to the tbbutton structure members to the following:
For (INT I = 0; I <4; I ++)
{
Button [I]. fsstate = tbstate_enabled;
Button [I]. fsstyle = tbstyle_button;
Button [I]. ibitmap = I;

Button [I]. idcommand = 1100 + I; // newly added statement
}

In this way, you only need to add on_command message ing to process a button and click the message. For example, if the ID number of the first button is 1100, the statement to manually add this button and click message ing is:

On_command (1100, ontoolbar1)

Ontoolbar1 is a message processing function, which can be added by yourself.

Some references (tomorrow Technology)

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.