Changing the Toolbar State

Start with the project you created in Implementing Toolbars - Initializing, Creating and Freeing.

1.

Select the File->New menu option, and in the dialog box, select Resource Script and press Enter.

2.

Right click the script you just created. Select Insert and then Menu from the dialog box that appears, then click OK.

3.

Create this Menu structure:

IDR_MENU1

   &View
      &Toolbar with ID = IDM_VIEW_TOOLBAR

   &Rows
      &1 with ID = IDM_ROWS_1
      &2 with ID = IDM_ROWS_2
      &3 with ID = IDM_ROWS_3
      &4 with ID = IDM_ROWS_4
      &5 with ID = IDM_ROWS_5
      &6 with ID = IDM_ROWS_6
      &8 with ID = IDM_ROWS_8

   &Set Position
      Test &1 with ID = IDM_SETPOSITION_TEST1
      Test &2 with ID = IDM_SETPOSITION_TEST2
      Test &3 with ID = IDM_SETPOSITION_TEST3
      Test &4 with ID = IDM_SETPOSITION_TEST4
 

4.

Select File->Save from the menu, enter Tutorial.rc in the Save As box and click Save.

5.

Select Insert->Files into Project, browse to the Tutorial.rc file just created and click Add.

6.

Add the following line right under #include "c:\lead\include\lttlb.h".

#include "resource.h"

7.

In the WinMain function definition change the line

WndClass.lpszMenuName = NULL;

In the WinMain function to:

WndClass.lpszMenuName = MAKEINTRESOURCE( IDR_MENU1 ) ;

8.

Add the following function calls to the WndProc function definition, after the L_TBSetVisible function call in the WM_CREATE message:

CheckMenuItem ( GetMenu ( hWnd ),
    IDM_VIEW_TOOLBAR,
    MF_BYCOMMAND | MF_CHECKED ) ;

CheckMenuRadioItem ( GetMenu ( hWnd ),
    IDM_ROWS_1,
    IDM_ROWS_8,
    IDM_ROWS_1,
    MF_BYCOMMAND ) ;

CheckMenuRadioItem ( GetMenu ( hWnd ),
    IDM_SETPOSITION_TEST1,
    IDM_SETPOSITION_TEST4,
    IDM_SETPOSITION_TEST1,
    MF_BYCOMMAND ) ;

9.

Add the following functions before the WndProc function definition:

static L_VOID OnViewToolbar ( pTOOLBARHANDLE pToolbar )
{
    L_BOOL bVisible ;

    L_TBIsVisible ( pToolbar, &bVisible ) ;

    L_TBSetVisible ( pToolbar, ! bVisible ) ;
}

static L_VOID OnRows ( pTOOLBARHANDLE pToolbar, L_INT nRows )
{
    L_TBSetRows ( pToolbar, nRows ) ;
}

static L_VOID OnSetPoistionTest ( HWND hWnd, pTOOLBARHANDLE pToolbar, L_INT nTest )
{
    RECT rcClient ;
    POINT pt ;
   
    GetClientRect ( hWnd, &rcClient ) ;

    switch ( nTest )
    {
       case 1:

          pt.x = rcClient.left ;
          pt.y = rcClient.top ;

          ClientToScreen ( hWnd, &pt ) ;

          L_TBSetPosition ( pToolbar, &pt, TOOLBAR_POSITION_TOP | TOOLBAR_POSITION_LEFT ) ;

          break ;

       case 2:

          pt.x = rcClient.right ;
          pt.y = rcClient.top ;

          ClientToScreen ( hWnd, &pt ) ;

          L_TBSetPosition ( pToolbar, &pt, TOOLBAR_POSITION_TOP | TOOLBAR_POSITION_RIGHT ) ;

          break ;

       case 3:

          pt.x = rcClient.right ;
          pt.y = rcClient.bottom ;
         
          ClientToScreen ( hWnd, &pt ) ;

          L_TBSetPosition ( pToolbar, &pt, TOOLBAR_POSITION_BOTTOM | TOOLBAR_POSITION_RIGHT ) ;

          break ;

       case 4:

          pt.x = rcClient.left ;
          pt.y = rcClient.bottom ;
         
          ClientToScreen ( hWnd, &pt ) ;

          L_TBSetPosition (pToolbar, &pt, TOOLBAR_POSITION_BOTTOM | TOOLBAR_POSITION_LEFT);

          break ;
    }
}

10.

Add the following variable declaration in the WndProc function after "static pTOOLBARHANDLE pToolbar ;" declaration:

L_BOOL fState ;

11.

Add the following code to the WndProc function definition after the "return 0L ;" statement in the case WM_DESTROY message:

case WM_COMMAND:
{
    switch ( LOWORD ( wParam ) )
    {
       case IDM_VIEW_TOOLBAR:

          OnViewToolbar ( pToolbar ) ;

          L_TBIsVisible ( pToolbar, &fState ) ;

          CheckMenuItem ( GetMenu ( hWnd ),
             IDM_VIEW_TOOLBAR,
             MF_BYCOMMAND | ( fState ? MF_CHECKED : MF_UNCHECKED ) ) ;
          return 0L ;

       case IDM_ROWS_1:
          OnRows ( pToolbar, 1 ) ;

          CheckMenuRadioItem ( GetMenu ( hWnd ),
             IDM_ROWS_1,
             IDM_ROWS_8,
             IDM_ROWS_1,
             MF_BYCOMMAND ) ;
          return 0L ;

       case IDM_ROWS_2:
          OnRows ( pToolbar, 2 ) ;

          CheckMenuRadioItem ( GetMenu ( hWnd ),
             IDM_ROWS_1,
             IDM_ROWS_8,
             IDM_ROWS_2,
             MF_BYCOMMAND ) ;
          return 0L ;

       case IDM_ROWS_3:
          OnRows ( pToolbar, 3 ) ;

          CheckMenuRadioItem ( GetMenu ( hWnd ),
             IDM_ROWS_1,
             IDM_ROWS_8,
             IDM_ROWS_3,
             MF_BYCOMMAND ) ;
          return 0L ;

       case IDM_ROWS_4:
          OnRows ( pToolbar, 4 ) ;
         
          CheckMenuRadioItem ( GetMenu ( hWnd ),
             IDM_ROWS_1,
             IDM_ROWS_8,
             IDM_ROWS_4,
             MF_BYCOMMAND ) ;
          return 0L ;

       case IDM_ROWS_5:
          OnRows ( pToolbar, 5 ) ;

          CheckMenuRadioItem ( GetMenu ( hWnd ),
             IDM_ROWS_1,
             IDM_ROWS_8,
             IDM_ROWS_5,
             MF_BYCOMMAND ) ;
          return 0L ;

       case IDM_ROWS_6:
          OnRows ( pToolbar, 6 ) ;

          CheckMenuRadioItem ( GetMenu ( hWnd ),
             IDM_ROWS_1,
             IDM_ROWS_8,
             IDM_ROWS_6,
             MF_BYCOMMAND ) ;
          return 0L ;

       case IDM_ROWS_8:
          OnRows ( pToolbar, 7 ) ;

          CheckMenuRadioItem ( GetMenu ( hWnd ),
             IDM_ROWS_1,
             IDM_ROWS_8,
             IDM_ROWS_8,
             MF_BYCOMMAND ) ;
          return 0L ;

       case IDM_SETPOSITION_TEST1:

          OnSetPoistionTest ( hWnd, pToolbar, 1 ) ;

          CheckMenuRadioItem ( GetMenu ( hWnd ),
             IDM_SETPOSITION_TEST1,
             IDM_SETPOSITION_TEST4,
             IDM_SETPOSITION_TEST1,
             MF_BYCOMMAND ) ;

          return 0L ;

       case IDM_SETPOSITION_TEST2:

          OnSetPoistionTest ( hWnd, pToolbar, 2 ) ;

          CheckMenuRadioItem ( GetMenu ( hWnd ),
             IDM_SETPOSITION_TEST1,
             IDM_SETPOSITION_TEST4,
             IDM_SETPOSITION_TEST2,
             MF_BYCOMMAND ) ;
          return 0L ;

       case IDM_SETPOSITION_TEST3:
         
          OnSetPoistionTest ( hWnd, pToolbar, 3 ) ;

          CheckMenuRadioItem ( GetMenu ( hWnd ),
             IDM_SETPOSITION_TEST1,
             IDM_SETPOSITION_TEST4,
             IDM_SETPOSITION_TEST3,
             MF_BYCOMMAND ) ;
          return 0L ;

       case IDM_SETPOSITION_TEST4:

          OnSetPoistionTest ( hWnd, pToolbar, 4 ) ;

          CheckMenuRadioItem ( GetMenu ( hWnd ),
             IDM_SETPOSITION_TEST1,
             IDM_SETPOSITION_TEST4,
             IDM_SETPOSITION_TEST4,
             MF_BYCOMMAND ) ;
       return 0L ;
    }
}

break ;

12.

Compile and run the project by selecting Build->Execute tutorial.exe from the menu.