Implementing Toolbars - Initializing, Creating and Freeing

The following tutorials are designed to guide you, step by step through creating toolbars, setting toolbar and button characteristics and receiving event notifications. These tutorials were developed using a Visual C++ version 4.2 compiler and they are designed for the WIN32 API platform.

Take the following steps to start a project and add some code that will create a new toolbar handle:

1.

Start Visual C++, version 4.2.

2.

Select the File->New menu option, and in the dialog box, select Project Workspace.

3.

In the New Project Workspace dialog box, enter the location (c:\lead\examples\toolbar). Make sure to select Application (Win32 Application) as the project type. Give the project the name "tutorial" and press Enter.

4.

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

5.

Copy and paste the following code into the text file you have just created:

#include <windows.h>
#include <windowsx.h>
#include "l_bitmap.h"
#include "lttlb.h"

static LRESULT WINAPI WndProc ( HWND hWnd, L_UINT uMessage, WPARAM wParam, LPARAM lParam )
{
   static HWND hwndMain ;
   static pTOOLBARHANDLE pToolbar ; 

   switch( uMessage )
   {
      case WM_CREATE:
         if ( SUCCESS == L_TBInit ( &pToolbar ) )
         {
            POINT pt = { 0, 0 } ;

             hwndMain = hWnd ;

            // Create the toolbar
            L_TBCreate ( pToolbar, hWnd, TEXT("LEAD Toolbar"), TOOLBAR_PAINT ) ;

            // Initiate the point will be used to align the toolbar at the top-left of its owner client area */
            ClientToScreen ( hWnd, &pt ) ;
      
            // Set the toolbar position
            L_TBSetPosition ( pToolbar, &pt, TOOLBAR_POSITION_TOP | TOOLBAR_POSITION_LEFT ) ;

            // Show the toolbar
            L_TBSetVisible ( pToolbar, TRUE ) ; 

            return 0L ;
         }
         else
         {
            return -1L ;
         } 

      case WM_DESTROY:
         if ( SUCCESS == L_TBIsValid ( pToolbar ) )
         {
            L_TBFree ( pToolbar ) ;
         }

         PostQuitMessage( 0 ) ;
         return 0L ;

      default:
         break ;
   }

   return DefWindowProc ( hWnd, uMessage, wParam, lParam ) ;
}

L_INT WINAPI WinMain ( HINSTANCE hInst, HINSTANCE hInstPrev, LPSTR pCmdLine, L_INT nCmdShow )
{
   const L_TCHAR szClassName [ ] = TEXT("lead_toolbar_tutorial") ;
   WNDCLASSEX WndClass ;
   HWND hWnd ;
   MSG Msg ;

   UNREFERENCED_PARAMETER ( hInstPrev ) ;
   UNREFERENCED_PARAMETER ( pCmdLine ) ;

   // unlock lead file formats support
   UNLOCKSUPPORT ( ) ;

   WndClass.cbSize = sizeof ( WNDCLASSEX ) ;
   WndClass.style = CS_HREDRAW | CS_VREDRAW ;
   WndClass.lpfnWndProc = WndProc ;
   WndClass.cbClsExtra = 0 ;
   WndClass.cbWndExtra = 0 ;
   WndClass.hInstance = hInst ;
   WndClass.hIcon = LoadIcon ( NULL, IDI_APPLICATION ) ;
   WndClass.hCursor = LoadCursor ( NULL, IDC_ARROW ) ;
   WndClass.hbrBackground = ( HBRUSH ) ( COLOR_WINDOW + 1 ) ;
   WndClass.lpszMenuName = NULL ;
   WndClass.lpszClassName = szClassName ;
   WndClass.hIconSm = NULL ;

   RegisterClassEx ( &WndClass ) ;

   hWnd = CreateWindowEx( 0L, 
                      szClassName, 
                      TEXT("Toolbar Tutorial"), 
                      WS_OVERLAPPEDWINDOW,
                      CW_USEDEFAULT, 
                      0, 
                      CW_USEDEFAULT, 
                      0,
                      NULL, 
                      NULL, 
                      hInst, 
                      NULL ) ;

   ShowWindow ( hWnd, nCmdShow ) ;
   UpdateWindow ( hWnd ) ;

   while ( GetMessage ( &Msg, NULL, 0, 0 ) != 0 )
   {
      TranslateMessage ( &Msg ) ;
      DispatchMessage ( &Msg ) ;
   }

   UnregisterClass ( szClassName, hInst ) ;

   return Msg.wParam ;
}

6.

Select File->Save from the menu and give the text file the name "tlbtut.c".

7.

Select Insert->Files into Project from the menu. In the dialog box that appears, browse to the tlbtut.c file you have just created, select it and click Add.

8.

Select Insert->Files into Project from the menu. In the dialog box that appears, browse to c:\lead\lib\ltkrn_n.lib, select it and click Add. Please note that "c:\lead" represents the directory where the LEADTOOLS 14.5 Toolkit was installed.

9.

Select Insert->Files into Project from the menu. In the dialog box that appears, browse to c:\lead\lib\lttlb_n.lib, select it and click Add.

10.

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