Toolbar Control: 32-bit real color icon

Source: Internet
Author: User
1. Create a common toolbar

1. Create an MFC program and add a member variable ctoolbar m_toolbar of the ctoolbar class to the DLG header file;

2. Define the toolbar command ID in the header file.

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

3. In the resource dialog box, create a toolbar, and then create three buttons.

Note: adding a button in vs 2008 assigns a value to the ID attribute of the current button. A new button is automatically displayed.

4. the IDs of the modify buttons are id_button1, id_button2, and id_button3. You can also modify the button size and use the paint brush tool to modify the button's appearance.

5. Then you can initialize the toolbar:

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 );

6. Add button event:

First, declare the event handler in the header file:

Afx_msg void button1click ();

Declare the message function ing of the command event in the. cpp file: on_command (id_button1, ctoolbarnewdlg: button1click)

Implement the button1click function in. cpp.

 

2. Create a True Color toolbar

1. First, import 6 bitmaps (in BMP format) to the project. The size is 32*32 and the color is 32 bits. The default IDs are idb_bitmap1 and idb_bitmap2 ......

2. Add two variables to the DLG header file:

Ctoolbar m_toolbar;
Cimagelist m_imagelist;

3. Add a macro definition to the header 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

4. Add the initialization code:

M_imagelist.create (32, 32, ilc_color32 | ilc_mask, 1, 1 );

// The image size must be consistent. The parameters indicate 32*32 pixels and 32-bit true color.
Cbitmap BMP;
For (INT I = 0; I <6; I ++)
{
// Use an existing bitmap to assign a value to BMP

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 idnumber in sequence, corresponding to the elements of the narray Array

// It is equivalent to completing the work of manually setting the ID for the button.
M_toolbar.setsizes (csize (40, 40), csize (32, 32 ));

// The first function is the button size, and the second function is the image size. The button must be larger than the image. Specifically, the button size is 7 or more larger than the image size, and the button size is 6 higher.
M_toolbar.gettoolbarctrl (). setimagelist (& m_imagelist); // sets the image
Repositionbars (afx_idw_controlbar_first, afx_idw_controlbar_last, 0 );

Display Effect:

 

 

3. Add a background bitmap for the toolbar

1. MFC provides a class crebar, which is a container class. You can add a toolbar, edit box, and drop-down list to this class. Add a variable of the crebar class to the header file: crebar m_rebar;

2. Import a bitmap as the toolbar background. The ID is id_toolback.

3. Add the following code before the repositionbars statement in the above Code:

 

 

M_rebar.create (this); // create a window (Control)
M_rebar.addbar (& m_toolbar); // bind m_toolbar to the crebar
M_rebar.redrawwindow (); // redraw a window
Rebarbandinfo Info; // set the crebar style.
Info. cbsize = sizeof (Info );
Info. fmask = rbbim_background;
M_toolbar.modifystyle (0, tbstyle_transparent );

// Set the background color transparency of the toolbar to display the background color of the crebar.
Info. hbmback = loadbitmap (AfxGetInstanceHandle (), makeintresource (idb_toolback ));

// Load bitmap
M_rebar.getrebarctrl (). setbandinfo (0, & info );

4. if you want to set text for the button, you can use the setbuttontext function of the ctoolbar class, but this function must be placed before the setsize function; otherwise, the text cannot be displayed; and you must set the toolbar height to be sufficient, to display text.

5. The running effect is as follows:

 

6. Problem:

After performing the preceding operations, the crebar has been created, but the background color does not know why it cannot be added. If anyone has solved this problem, please kindly advise.

 

4. Add a mouse stop event

1. First add a string variable: cstring STR in the header file;

2. After m_toolbar calls the createex function, add the statement:

M_toolbar.enabletooltips (); // activates the information reminder function.

3. Add the ttn_needtext message processing function to the header file:

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

4. Add a message ing macro to. cpp:

On_policy_ex (ttn_needtext, 0, ctoolbarnewdlg: ondisplay)

5. ondisplay code:

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
Switch (NID)

{

Case id_button1: Str = "Travel Agency"; break;

Case id_button2: Str = ""; break;

Case id_button3: Str = "recruitment"; break;

Default: Str = "";

}

Pttt-> lpsztext = Str. getbuffer (Str. getlength (); // set the button prompt information
Pttt-> hinst = afxgetresourcehandle ();
Return true;

 

5. Use icons to build Toolbar

1. Just like creating a toolbar with BMP images, you can add images to m_imagelist in a different way:

For (INT I = idi_icon1; I <= idi_icon6; I ++)
M_imagelist.add (afxgetapp ()-> loadicon (I); // Add an icon

 

6. Set the toolbar hotspot icon

1. Prepare two sets of icons, one for normal display and the other for hotspot display after the mouse is placed.

2. the toolbar must have the tbstyle_flat (floating button) attribute, which is already created by default.

3. Define imagelist: cmagelist m_hotimagelist in the header file;

4. Add the following statement to the position corresponding to the _ imagelist creation process:

M_hotimagelist.create (48, 48, ilc_color24 | ilc_mask, 1, 1 );

M_hotimagelist.add (afxgetapp ()-> loadicon (idi_hot1 + I ));

M_toolbar.gettoolbarctrl (). sethotimagelist (& m_hotimagelist );

5. In this way, when the mouse is placed, it will show another way.

 

VII. ctoolbarctrl Toolbar Control class

1. usage is similar to that of ctoolbar:

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

Dwstyle: the style of the Toolbar Control

Rect: toolbar location and region

Pparentwnd: parent window handle of the toolbar (which window belongs)

NID: the ID of the Toolbar Control.

Addbuttons is used to add a group of buttons to the toolbar:

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;

2. Use ctoolbarctrl to add a toolbar:

Cimagelist m_imagelist;
Ctoolbarctrl m_tbarctrl;

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 );

3. Create a response event function for the button

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

 

In the for loop above, Add button [I]. idcommand = id_button1 + I; Set ID for the button

Then add the command event function.

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.