Dialog Box program dynamically add toolbar and status bar

Source: Internet
Author: User

Abstract: This article describes how to dynamically add toolbar and status bar through program control on the basis of the dialog box program.

I. Introduction

For programs that do not require document/view structure support, the dialog box is generally used as the basic framework of the program. Although you can add a system menu by modifying its properties in the dialog box program, comparing the tool bar and status bar of SDI and MDI programs makes the interface simple and monotonous. Although Visual Basic provides independent toolbar and status bar standard controls that can be directly used in the dialog box, however, Visual C ++ 6.0 does not include 26 Basic Windows standard controls. Therefore, the toolbar and status bar must be implemented dynamically in the dialog box program through programming.

II. Implementation Process of tool bar

MFC provides two basic class libraries ctoolbar and cstatusbar for the toolbar and status bar respectively. However, due to the fact that MFC encapsulates the toolbar too much, it cannot understand some internal core technologies. Therefore, this article abandoned the relatively convenient use of the ctoolbar and cstatusbar classes in the implementation process, but through the SDK (software developers kit, software development toolkit) by using winapi APIs. The window handle and current instance handle of the dialog box are often used in API functions. In SDK programming, the above two handles can be directly extracted from the winmain () function of the entry, in MFC, It is encapsulated and cannot be obtained directly. However, MFC also provides its interface: getsafehwnd () provided by the cwnd window class can return the window handle of the dialog box; function AfxGetInstanceHandle () can get the instance handle of the current application. As the dialog box and status bar are part of the program interface, they must be displayed when the program starts, therefore, the code for obtaining the handle and all the code for creating the display toolbar and the status bar should be put in the oninitdialog () response function of the message wm_initdialog initialized in the dialog box:

Hwnd hdlg = getsafehwnd ();
Hinstance = AfxGetInstanceHandle ();

The toolbar buttons to be added can be divided into two types as needed:

One is some tool bar buttons of Windows standard, such as opening files, printing and previewing, online help, etc. Such tool bar buttons can directly use the ID of the predefined button icon, which is included in the commctrl of VC. the H header file is defined in detail;

The other is the toolbar button added by the user, which can only be specified by the user in the resource view. Both the toolbar buttons are created by setting the tbbutton structure to determine the status of each toolbar button. You can use createmedilbarex () to create the first toolbar button () to directly add it to the toolbar, and return the window handle pointing to the toolbar. To add a toolbar button in the future, you can only send the tb_addbuttons message to the toolbar:

......
// Fill in the toolbar button structure:
Tbbutton ptoolbar [30] ={{ std_help, // specify the standard help icon for Windows
Mu_one, // the ID of the toolbar button
Tbstate_enabled, // available status
Tbstyle_button, // specify a button that can be pressed
0, // reserved, which is defined by the application
0}, // button string Index
// Create a vertical line for the split button
{0, tbstate_enabled, tbstyle_sep, 0, 0 }};

// Dynamically create a toolbar in the dialog box and add the toolbar button:
Hwnd htoolswindow =: createmedilbarex (hdlg, // specify the dialog box as the parent window, and create the toolbar on the dialog box
Ws_child | ws_visible | tbstyle_wrapable | tbstyle_tooltips |
Tbstyle_flat | ccs_adjustable, // specify the toolbar creation style
Idb_toolbar, // pre-defined toolbar resource ID
30, hinst_commctrl, // instance handle of executable files containing image resources
Idb_std_small_color, // resource ID of the image
Ptoolbar, // button to be added
2, // The number of buttons added to the toolbar
0, 0, 0, sizeof (tbbutton ));
......
// Load the button icon from the idr_toolbar1 resource to the toolbar of the dialog box
Tbaddbitmap tab;
Tab. hinst = hinstance;
Tab. nid = idr_toolbar1;
IBMP =: sendmessage (htoolswindow, tb_addbitmap, (wparam) 3, (lparam) & tab );

The key to adding a toolbar to a toolbar is filling in the tbbutton data structure, which is also defined in the commctrl. h header file. The original form is:

Typedef struct _ tbbutton {
Int ibitmap;
Int idcommand;
Byte fsstate;
Byte fsstyle;
DWORD dwdata;
Int istring;
} Tbbutton, near * ptbbutton, far * lptbbutton;

The data member of this structure contains the information about the buttons in the toolbar: The member ibitmap is the index of the button image counted from 0; The idcommand identifies the matched button, this identifier is used when the button is pressed to generate the wm_command message. fsstate specifies the button status flag, it can be a logical combination of the following eight symbols: tbstate_checked, tbstate_ellipses, tbstate_enabled, tbstate_hidden, tbstate_indeterminate, tbstate_marked, tbstate_pressed, and tbstate_wrap. The meanings of the above logos are described in detail in the msdn online help; The fsstyle Member specifies the button style; dwdata is the value defined by the application, usually 0; istring is the index of the button string starting from 0. The following code adds a custom toolbar button to a toolbar:

Tbbutton TB;
TB. ibitmap = IBMP + 0;
TB. idcommand = mu_two;
TB. fsstate = tbstate_enabled;
TB. fsstyle = tbstyle_button;
TB. dwdata = 0;
TB. istring = 0;

After setting the tbbutton structure, you can use the window handle htoolswindow to send the tb_addbuttons message to add a button to the toolbar. To add a split bar between buttons, you only need to set the fsstyle member variable of the tbbutton structure to tbstyle_sep:

: Sendmessage (htoolswindow, tb_addbuttons, (wparam) 1, (lparam) & TB );

3. Implementation of the status bar

The implementation of the status bar is much simpler than the implementation of the toolbar. You only need to set its parameters in the createstatuswindow () function:

Hwnd hstatuswindow = createstatuswindow (ws_child | ws_visible | ws_border,
Text ("status bar"), // information displayed on the status bar
Hdlg, // parent window handle
Ids_status); // the predefined resource ID.

The created status bar is just a long bar located at the bottom of the dialog box. If you want to split it into several parts, you can set the X coordinate of the split point in the array, then, send the sb_setparts message to the status bar. The wparam parameter of the message specifies the parts of the status bar. The lparam parameter specifies the coordinate values of each split point:

Int pint [4] = {110,250,300,-1}; // 110,250,300 set the interval
: Sendmessage (hstatuswindow, sb_setparts, 4, (lparam) pint );

To enter information in the split status bar, you can use hstatuswindow to send the sb_settext message to the status bar, the two parameters of the message are used to identify the information displayed in the following pane and the information to be displayed:

: Sendmessage (hstatuswindow, sb_settext, 1, (lparam) text ("info 1 "));
......

Summary: This article uses sdks to dynamically add toolbar and status bar under the MFC Dialog Box program, this allows normal dialog box programs to have custom toolbar and status bar like SDI and MDI programs. The focus of the entire implementation process is to set the relevant structure, send messages, and dynamically create controls. For more information about the structures and functions involved in this article, see Microsoft's msdn library 6.0. The program described in this article is compiled by Microsoft Visual C ++ 6.0 under Windows 98.

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.