Add a toolbar and status bar for dialog box - Interface class programming -vc

xiaoxiao2021-04-11  152

I. Introduction

For programs that do not require document / view structures, the dialog is generally used as the base framework for the program. Although the system menu can be added by modifying its properties on the dialog program, the toolbar and status bar of the SDI and MDI programs are not avoided, and the interface is simple, monotonous. Although there are independent toolbars and status bar standard controls in Visual Basic, you can use them directly on dialogments, but the 26 basic Windows standard controls provided by Visual C 6.0 do not contain them. Therefore, implement the toolbar and status bar in the dialog program can only be dynamically implemented by programming.

Second, the implementation of the tool bar

The MFC provides both CToolbar and CStatusbar two basic class libraries for the toolbar and status bar, but because MFC has done too much package, so that some core technologies cannot be understood. Therefore, this article gives up the use of relatively convenient CToolBar and CSTATUSBAR classes during implementation, but through the SDK (Software Developers Kit, Software Development Toolbox) WinAPI application interface. In the API function, you often need the window handle of the dialog and the current instance handle, and the above two handles in the SDK program can be directly taken from the entrance function WinMain (), and it is also packaged under the MFC. Can not be obtained directly. However, MFC also returns an interface: getSafehWnd () provided by the CWND window class, can return the window handle of the dialog; function AFXGetInstanceHandle () can get an instance handle of the current application. Since the dialog box and status bar belong to a part of the program interface, it is displayed when the program is started. Therefore, the code of the handle is acquired, and the display toolbar is created later. All the code of the status bar should be placed in the dialog initialization message WM_INITDIALOG response function onInitDialog () In progress:

HWND HDLG = GetsafehWnd ();

Hinstance hinstance = afxgetinstancehandle ();

The toolbar buttons to be added can be divided into two according to the situation:

One is some toolbar buttons for Windows standards such as open files, print previews, online help, and more, this type of toolbar button can directly use the predefined buttons icon ID number, in the CommCtrl.h header file that comes with the VC. There is a detailed definition;

The other is the toolbar button you add by the user, only by the user to specify the corresponding icon in the resource view. Whether it is the design of the toolbar button to determine the status of each toolbar button through the settings of the TBButton structure, you can use CreateToolbarex () to use CreateToolbarex () to directly add it to the toolbar for the first filled On, and return to the window handle pointing to the toolbar, and the addition of the toolbar button will only be implemented by sending TB_ADDBUTTONS messages to the toolbar:


// Fill the toolbar button structure:

TBButton PToolbar [30] = {{std_help, // Specify Windows standard help icon

MU_ONE, / / ​​Toolbar Button ID

TBSTATE_ENABLED, / / ​​available

TBStyle_Button, / / ​​Specify the button to create a button

0, // Reserved, defined by the application

0}, // Button string index

// Create a vertical line for split button


/ / Dynamically create the toolbar on the dialog and add the toolbar button:

HWND HTOOLSWINDOW = :: CreateToolbarex (HDLG, // Specify dialog is the parent window, create the toolbar on the dialog


IDB_TOOLBAR, / / ​​Predefined Toolbar Resource ID

30, hinst_commctrl, // instant handle with the executable of the image resource


Ptoolbar, // The button to be added

2, // Number of buttons to the toolbar

0,0,0,0, sizeof (tbutton);


/ / On the toolbar of the load button icon to the dialog from the IDR_Toolbar1 resource


Tab.hinst = hinstance;

Tab.nid = idR_toolbar1;

IBMP = :: SendMessage (HtoolsWindow, TB_ADDBITMAP, (WPARAM) 3, (LPARAM) & Tab);

The key to adding the toolbar button to the toolbar is to fill the TbButton data structure, which is also defined in the command of the command, original shape:

Typedef struct _tbbutton {


Int idcommand;




Int istring;

} TBButton, Near * PTBB / FAR * LPTBB / Download

The structure of the structure contains information about the button in the toolbar: member ibitmap is the index of the button image from 0 starts; IDCommand identifies the matching button, when the button is pressed to generate the WM_COMMAND message The identity is used; FSState specifies the status flag of the button, which can be the following 8 logical combination TBSTATE_CHECKED, TBSTATE_ELLIPSES, TBSTATE_ENABED, TBSTATE_HIDDEN, TBSTATE_DETERMINATE, TBSTATE_MARKED, TBSTATE_PRESSED, TBSTATE_WRAP. As for the specific meaning of the above flags in the online help of MSDN; FSStyle members specify the style of the button; DWDATA is the value defined by the application, usually 0; istring is the index of the buttons string from 0. The following segment is used to add a custom toolbar button to the toolbar:

TBButton TB;

Tb.IBitmap = IBMP 0;

Tb.idcommand = MU_TWO;


TB.FSStyle = TBStyle_Button;

Tb.dwdata = 0;

Tb.istring = 0;

After the setting of the TBButton structure, you can send a TB_ADDBUTTONS message to the toolbar to the toolbar to add a button to the toolbar. If you want to add a split bar between buttons, simply take the value of the TbButton structure to be TBStyle_sep. :

:: SendMessage (HtoolsWindow, TB_ADDButtons, (WPARAM) 1, (LPARAM) & TB);

Third, the realization of the status bar

The implementation of the status bar is very simple compared to the implementation of the toolbar, just set the parameters in the createStatusWindow () function:


Text ("Status Bar"), // Displays the information on the status bar

HDLG, // parent window handle IDS_STATUS); // Pre-defined resource ID

The status bar created at this time is just a strip located at the bottom of the dialog. If you need to split it into a few parts, you can set the X coordinate of the split point in the array, and then send the SB_SETPARTS message to the status bar, the message The WPARAM parameter specifies that the status bar is split into several parts, and the LPARAM parameter specifies the coordinate value of each split point:

INT PINT [4] = {110, 250, 300, -1}; // 110, 250, 300 Setting interval

:: SendMessage (hstatuswindow, sb_setparts, 4, (lparam) PINT);

If you need to fill in the information in the split status bar, you can send a message SB_SETTEXT to the status bar through the HStatusWindow. The two parameters of the message are used to identify the information content in the first pane display and to be displayed:

:: SendMessage (HStatusWindow, Sb_Settext, 1, (LPARAM) Text ("Information One"));



New Post(0)