VC tool bar

Source: Internet
Author: User
Since Microsoft launched Windows 95, a large number of brand new controls have enabled our applications Program
More beautiful, more convenient to use. One significant change is that the toolbar is no longer one
A highlighted small 3D box is in the flat state, but you only need to move the mouse over it,
It automatically floats out, greatly facilitating users.
After a period of exploration, I finally found a way to make this tool bar. Original
Windows 95 encapsulates many common controls, most of which are placed in comctrl32.dll.
The toolbar control is used to create a toolbar. The following briefly introduces how to add
Add a toolbar.

As we all know, all controls are windows of a certain type, so the toolbar must also be created from
Port. The toolbar class of MFC does not support new functions, so we have to use the SDK method,
You can call an API to complete the entire process. This process is similar to creating a traditional tool bar.

Toolbar is an extension of comctrl32.dll. Therefore, you must call initcommoncontrolsex () first.
Letter count. This function has an important parameter that determines the support for toolbar. Its main function is to register
Toolbar window to create this window in future programs, while the common toolbar needs to call
Initcommandcontrols (). Note the writing of these two functions.

Initcommoncontrolsex icex;

DWORD dwstyle;

Icex. dwsize = sizeof (initcommoncontrolsex );

// Note that the following two parameters determine whether to register the toolbar.

Icex. dwicc = icc_cool_classes | icc_bar _ classes;

Initcommoncontrolsex (& icex );

Then you can call the createjavaswex function to create the toolbar window:

Hwnd hwndtb = createdomainwex (

Ws_ex_toolwindow, // extended toolbar Style

Toolbarclassname, // toolbar Class Name

Null,

Ws_child | ws_visible | tbstyle_flat,

// Window style

0, 0, 0, // size

Afxgetapp ()-> getmainwnd (), // parent window name

Null,

AfxGetInstanceHandle (), // instance

Null );

Judge the window handle. If it is not null, the window is successfully created. The toolbar does not
Is an empty window, we can add a button to it as needed. Add to Toolbar
The button is implemented by sending messages to it. The following process is basically the same as creating a traditional toolbar.
First, create an imagelist control, and then use the data structure of the predefined button to determine the type of each button.

// Create an imagelist control,

Hwnd himl;

// Myicon_cx, myicon_cy is the size of each button

Himl = imagelist_create (myicon_cx, myicon_cy, ilc_color4, 0, 4 );

// Add the pre-prepared toolbar bitmap idb_bitmap2

Imagelist_add (himl,

Loadbitmap (AfxGetInstanceHandle (), makeintresource (idb_bitmap2), null );

// Add bitmap to toolbar through message

Sendmessage (hwndtb, tb_setimagelist, 0, (lparam) himl );

Five common buttons are added below:

Tbbutton tbarray [5]; // data structure of the button

For (I = 0; I <5; I ++ ){

Tbarray [I]. ibitmap = I; // the I-th bitmap.

Tbarray [I]. idcommand = idm_buttonstart + I;

// Command ID

Tbarray [I]. fsstate = tbstate_enabled;

Tbarray [I]. fsstyle = tbstyle_button;

// Button style

Tbarray [I]. dwdata = 0;

Tbarray [I]. istring = I; // The displayed string

}

// Set the size of the button Structure

: Sendmessage (hwndtb, tb_buttonstructsize, sizeof (tbbutton), 0 );

// Add the button to the toolbar

: Sendmessage (hwndtb, tb_addbuttons, (uint) 5, (lparam) tbarray );

So far, a cool tool bar is basically created, and then you can call the showwindow () function:

Showwindow (hwndtb, sw_showmaximized );

When you click the button, the toolbar sends the message to the parent window, and the parent window responds to the message. Toolbar
The button ID is included in the wparam parameter of the message function. You can set it to schedule different modules. Then
The O ncommand () function of the parent window can be reloaded to determine which button is clicked Based on the wparam parameter. Assume that
The parent window is the main window frame,CodeAs follows:

Bool cmainframe: oncommand (wparam, lparam)

{

Switch (wparam ){

Case idm_buttonstart + 0:

Afxmessagebox ("You clicked the first button !! ", Mb_iconinformation );

Break;

Case idm_buttonstart + 1:

Afxmessagebox ("You clicked the second button !! ", Mb_iconinformation );

Break;

Case idm_buttonstart + 2:

Afxmessagebox ("You clicked the third button !! ", Mb_iconinformation );

Break;

}

Return cmainframe: oncommand (wparam, lparam );

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.