SAMPLE: Customizing the TOOLBAR Control

Last reviewed: April 5, 1996
Article ID: Q125683
The information in this article applies to:
  • Microsoft Win32 Application Programming Interface (API) included with:

        - Microsoft Windows 95 version 4.0
    

SUMMARY

The sample BARSDI demonstrates how to provide Customization features for the Toolbar Common Control. The Toolbar Common Control under Windows 95 provides Customization features that are useful when the user needs to change the toolbar control's buttons dynamically (add, delete, interchange, etc. buttons).

Download BARSDI.EXE, a self-extracting file, from the Microsoft Software Library (MSL) on the following services:

  • Microsoft Download Service (MSDL)

          Dial (206) 936-6735 to connect to MSDL
          Download BARSDI.EXE (size: 50255 bytes) 
    
  • Internet (anonymous FTP)

          ftp ftp.microsoft.com
          Change to the SOFTLIB\MSLFILES directory
          Get BARSDI.EXE (size: 50255 bytes) 
    

MORE INFORMATION

There are two ways the user can customize the toolbar:

First, the user can use the Drag Drop Customization process to delete or change the position of buttons on the toolbar. This method does not allow the user to add buttons to the toolbar dynamically.

The second method involves displaying the Customize dialog box through which the user can add, remove, interchange buttons on the toolbar.

To provide Customization, the toolbar control has to be created with the CCS_ADJUSTABLE style, and the parent of the toolbar control has to process a series of TBN_XXXX notifications. The BARSDI sample implements both methods of Customization.

Method 1: Drag Drop Customization

This method of toolbar customization allows the user to reposition or delete buttons on the toolbar. The user initiates this operation by holding down the SHIFT key and begins dragging a button. The toolbar control handles all of the drag operations automatically, including the cursor changes.

To delete a button, the user has to release the drag operation outside the Toolbar control. The Toolbar control sends the TBN_QUERYDELETE message to its parent window. The parent window can return TRUE to allow the button to be deleted and FALSE to prevent the button from being deleted.

If the application wants to do custom dragging, it has to process the TBN_BEGINDRAG and TBN_ENDDRAG notifications itself and perform the drag/drop process, which involves more coding.

Method 2: Customization Dialog Box

This method of customization allows users to add buttons to the toolbar dynmacally in addition to deleting and rearranging buttons on the toolbar. For example, if the toolbar has N total buttons, and displays only 10 of those buttons initially, the bitmap that was used to create the toolbar, should contian all N buttons (where N > 10).

There are two ways in which the Toolbar control dislpays the customize dialog box. The user can bring up the Customization dialog box by double-clicking the left mouse button on the toolbar control or the application can send the TB_CUSTOMIZE message to the toolbar control.

The Customize dialog box displayed by the Toolbar control has two list boxes. One, on the left contains the list of N-10 Buttons that were not displayed on the initial toolbar, and the one on the right will have the currently displayed buttons on the toolbar. The toolbar control provides the add, remove and other features in the Customize dialog box.

Here is a code sample that shows how the Customization feature is implemented:

SAMPLE CODE

// The initial set of toolbar buttons.

TBBUTTON tbButton[] = {
    {0,   IDM_FILENEW,     TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
    {1,   IDM_FILEOPEN,    TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
    {2,   IDM_FILESAVE,    TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
    {3,   IDM_EDITCUT,     TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
    {0,   0,               TBSTATE_ENABLED, TBSTYLE_SEP,    0, 0},
    {4,   IDM_EDITCOPY,    TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
    {5,   IDM_EDITPASTE,   TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
    {6,   IDM_FILEPRINT,   TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
    {0,   0,               TBSTATE_ENABLED, TBSTYLE_SEP,    0, 0},
    {7,   IDM_ABOUT,       TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
};

// Buttons that can be added at a later stage.

TBBUTTON tbButtonNew[] = {
    { 8,  IDM_ERASE,       TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
    { 9,  IDM_PEN,         TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
    {10,  IDM_SELECT,      TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
    {11,  IDM_BRUSH,       TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
    {12,  IDM_AIRBRUSH,    TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
    {13,  IDM_FILL,        TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
    {14,  IDM_LINE,        TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
    {15,  IDM_EYEDROP,     TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
    {16,  IDM_ZOOM,        TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
    {17,  IDM_RECT,        TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
    {18,  IDM_FRAME,       TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
    {19,  IDM_OVAL,        TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0},
};

// The bitmap that is used to create the toolbar should have all
// tbButtonNew + tbButton buttons = 20 in this case.

// Use tbButtons array to create the initial toolbar control.

// Once the user starts to customize the toolbar, process the WM_NOTIFY
// message and the following notifications.
// The toolbar control sends a WM_NOTIFY message to the parent window
// during each process of the customization.

LRESULT OnMsgNotify(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam) {
    LPNMHDR         lpnmhdr;
    lpnmhdr =       (LPNMHDR)lparam;

// process the QUERYINSERT And QUERYDELETE notifications
// to allow the drag/drop operation to succeeed.
    if (lpnmhdr->code == TBN_QUERYINSERT)
        return TRUE;
    else if (lpnmhdr->code == TBN_QUERYDELETE)
        return TRUE;
    else if (lpnmhdr->code == TBN_GETBUTTONINFO)
// The user has brought up the customization dialog box,
// so provide the the control will button information to
// fill the listbox on the left side.
    {
        LPTBNOTIFY lpTbNotify = (LPTBNOTIFY)lparam;
        char  szBuffer [20];
        if (lpTbNotify->iItem < 12) // 20 == the total number of buttons
        {                           // tbButton and tbButtonNew
                                    // Since initially we displayed
                                    // 8 buttons
                   // send back information about the rest of
                   // 12 buttons that can be added the toolbar.

            lpTbNotify->tbButton = tbButtonNew[lpTbNotify->iItem];

            LoadString(hInst,
                       NEWBUTTONIDS + lpTbNotify->iItem, // string
                                                         //ID == command ID
                       szBuffer,
                       sizeof(szBuffer));

            lstrcpy (lpTbNotify->pszText, szBuffer);
            lpTbNotify->cchText = sizeof (szBuffer);
            return TRUE;
        }
        else
        return 0;
    }
}


Additional reference words: 4.00 BARSDI
KBCategory: kbui kbcode kbfile
KBSubcategory: UsrCtl


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: April 5, 1996
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.