home *** CD-ROM | disk | FTP | other *** search
/ CICA 1993 August / CICA.cdr / unzipped / programr / atbsb001 / demotool.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-19  |  10.7 KB  |  348 lines

  1. /*
  2.     DEMOTOOL.C -- program to demonstrate the abilities of
  3.         Tool Bar control implemented in ESTOOLS.DLL
  4.     Copyright (C) Eugene Sokolov 1992-93, (516)632-7892,
  5.     esokolov@sbchm1.chem.sunysb.edu
  6.  
  7.     You can freely copy, change or redistribute this file as long
  8.     as this notice remains intact.
  9. */
  10.  
  11. #define STRICT    
  12.  
  13. #include <windows.h>
  14. #include "estools0.h"
  15. #include "esdefs.h"
  16. #include "error0.h"
  17.  
  18.  
  19. #define STATBAR_HEIGHT 22
  20.  
  21. LRESULT CALLBACK     MainWndProc    ( HWND, UINT, WPARAM, LPARAM );
  22.  
  23. HINSTANCE hInst;
  24.  
  25. BOOL InitApplication(HINSTANCE hInstance)
  26. {
  27.    WNDCLASS  wc;
  28.  
  29.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  30.    wc.lpfnWndProc     = (long (FAR PASCAL*)())MainWndProc;
  31.                                                         // windows of this class.
  32.    wc.cbClsExtra     = 0;    
  33.    wc.cbWndExtra     = 0;    
  34.    wc.hInstance     = hInstance;
  35.    wc.hIcon         = LoadIcon( hInstance, MAKEINTRESOURCE(TBICON) );
  36.    wc.hCursor         = LoadCursor(NULL, IDC_ARROW);
  37.    wc.hbrBackground     = GetStockObject(WHITE_BRUSH);
  38.    wc.lpszMenuName     = MAKEINTRESOURCE(TBMENU);
  39.    wc.lpszClassName     = "ESDemoTools";
  40.  
  41.  
  42.    return (RegisterClass(&wc));
  43. }
  44.  
  45.  
  46. /************************************************************************/
  47. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  48. {
  49.     HWND    hWnd;
  50.  
  51.     hInst = hInstance;
  52.  
  53.     hWnd = CreateWindow(
  54.         "ESDemoTools",    // See RegisterClass() call.
  55.         "ES Toolbar Demo",    // Text for window title bar.
  56.         WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,    // Window style.
  57.         CW_USEDEFAULT,            // Default horizontal position.
  58.         CW_USEDEFAULT,            // Default vertical position.
  59.         CW_USEDEFAULT,            // Default width.
  60.         CW_USEDEFAULT,            // Default height.
  61.         NULL,        
  62.         NULL,        
  63.         hInstance,    
  64.         NULL        
  65.     );
  66.  
  67.     if (!hWnd)
  68.         return (FALSE);
  69.  
  70.     ShowWindow(hWnd, nCmdShow);    // Show the window
  71.     UpdateWindow(hWnd);        // Sends WM_PAINT message
  72.     return (TRUE);        // Returns the value from PostQuitMessage
  73.  
  74. }
  75.  
  76.  
  77. LRESULT CALLBACK MainWndProc( HWND hWnd, UINT message,
  78.                      WPARAM wParam, LPARAM lParam )
  79. {
  80.     static HWND hwndTBar;
  81.     static HWND hwndSBar;
  82.  
  83.     switch (message)
  84.     {
  85.  
  86.     case WM_CREATE:
  87.     {
  88.        POINT pnt;
  89.        RECT    rc;
  90.            GetClientRect( hWnd, &rc );
  91.            pnt.x=pnt.y=0;
  92.        hwndTBar=CreateToolBar( hInst, (LPSTR)MAKEINTRESOURCE(TOOLBAR), hWnd, pnt );
  93.        if( !hwndTBar )
  94.        {
  95.           MessageBox( hWnd, "Could not create Tool Bar window", NULL, MB_OK );
  96.           PostMessage( hWnd, WM_CLOSE, 0, 0L );
  97.        }
  98.        hwndSBar=CreateStatusWindow( WS_CHILD | WS_VISIBLE, 0, rc.bottom-STATBAR_HEIGHT,
  99.              rc.right, STATBAR_HEIGHT, hWnd, hInst );
  100.  
  101.        if( !hwndSBar )
  102.        {
  103.           MessageBox( hWnd, "Could not create Status Bar window", NULL, MB_OK );
  104.           PostMessage( hWnd, WM_CLOSE, 0, 0L );
  105.        }
  106.  
  107.            break;
  108.         }
  109.     case WM_SIZE:
  110.        MoveWindow( hwndSBar, 0, HIWORD(lParam)-STATBAR_HEIGHT,  //Position
  111.                         //the SB window on the
  112.                         //bottom of the parent
  113.           LOWORD(lParam), STATBAR_HEIGHT, TRUE );
  114.        break;
  115.  
  116.     case WM_COMMAND:
  117.  
  118.     /* The following WM_COMMAND message processing was NOT intended to be
  119.        nicely written and serves as an example only to show the abilities
  120.        of the Tool Bar control. */
  121.         
  122.        switch( wParam )
  123.            {
  124.  
  125.           case ID_CMD1:
  126.           case ID_CMD2:
  127.           case ID_CMD3:
  128.           case ID_CMD4:
  129.           case ID_CMD5:
  130.               {
  131.        /* This simply prints the wParam of the WM_COMMAND message
  132.           which is equal to the ID number of the pressed TB button
  133.        */
  134.                  /* Old way: */
  135.              HDC hdc;
  136.                  char buffer[64];
  137.              hdc = GetDC( hWnd );
  138.          wsprintf( buffer,"Message WM_COMMAND, wParam=0x%X (%u), button # %d",
  139.             wParam, wParam, GetButtonNumber( hwndTBar, wParam ) );
  140.              TextOut( hdc, 10, 20, buffer, lstrlen( buffer ) );
  141.          ReleaseDC( hWnd, hdc );
  142.  
  143.          /* New way throught Status Bar, look how much simpler
  144.          it is */
  145.          ErrorHandler( hwndSBar, "Message WM_COMMAND, wParam=0x%X (%u), button # %d",
  146.             wParam, wParam, GetButtonNumber( hwndTBar, wParam ) );
  147.          /* ErrorHandler is an actual export from ERROR.DLL */
  148.          break;
  149.           }
  150.           case 1100:
  151.           case 1200:
  152.           case 1300:
  153.           case 1400:
  154.           case 1500:
  155.           /* Toggle disable/enable button */
  156.           {
  157.          int button=(wParam-1100)/100;
  158.          HMENU hm;
  159.          hm=GetMenu( hWnd );
  160.          CheckMenuItem( hm, wParam,
  161.             (GetMenuState( hm, wParam, MF_BYCOMMAND )&MF_CHECKED)?
  162.                     MF_UNCHECKED:MF_CHECKED );
  163.          ES_TB_Toggle_Enable_Disable_Demo( hwndTBar, button );
  164.          // This function actually calls SendMessage API which sends
  165.          // a message to hwndTBar window to change the corresponding
  166.          // style (explanation and source for these functions is sent to
  167.          // the registred users only).
  168.          // 
  169.          // Of course you can try to 'Spy' the messages. But I do
  170.          // not think the effort would be worth $15. Anyway, you are
  171.          // not allowed to use it in your programs unless you are
  172.          // registered.
  173.           }
  174.           break;
  175.  
  176.           case 1101:
  177.           case 1201:
  178.           case 1301:
  179.           case 1401:
  180.           case 1501:
  181.           /* Toggle standard/auto 2 state button */
  182.           {
  183.          int button=(wParam-1100)/100;
  184.          HMENU hm;
  185.          hm=GetMenu( hWnd );
  186.          CheckMenuItem( hm, wParam,
  187.             (GetMenuState( hm, wParam, MF_BYCOMMAND )&MF_CHECKED)?
  188.                     MF_UNCHECKED:MF_CHECKED );
  189.          ES_TB_Toggle_Standard_Auto2State_Demo( hwndTBar, button );
  190.          // you need to register to get the documentation on this
  191.          // option. Read the explanation above.
  192.           }
  193.           break;
  194.  
  195.           case 1102:
  196.           case 1202:
  197.           case 1302:
  198.           case 1402:
  199.           case 1502:
  200.           /* Toggle standard/2 state button */
  201.           {
  202.          int button=(wParam-1100)/100;
  203.          HMENU hm;
  204.          hm=GetMenu( hWnd );
  205.          CheckMenuItem( hm, wParam,
  206.             (GetMenuState( hm, wParam, MF_BYCOMMAND )&MF_CHECKED)?
  207.                     MF_UNCHECKED:MF_CHECKED );
  208.          ES_TB_Toggle_Standard_2State_Demo( hwndTBar, button );
  209.          // you need to register to get the documentation on this
  210.          // option. Look up there εεε
  211.          break;
  212.           }
  213.           case 2001:
  214.           case 2002:
  215.           case 2003:
  216.           case 2004:
  217.           case 2005:
  218.           /*
  219.          This function calls SendMessage which sends the corresponding
  220.          message to hwndTBar to set the number of buttons per row.
  221.          Although you could specify 0 in resource header to get horizontal
  222.          tool bar, here you have to supply the actual number, 0 does not
  223.          work - the call will be ignored
  224.           */
  225.          ES_TB_Set_Number_of_Controls_Demo( hwndTBar, wParam-2000 ); //Register!
  226.          ShowWindow( hwndTBar, SW_SHOW );
  227.           /*
  228.          Calls to this and following functions will hide the TB from
  229.          the screen. You need to show the window explicitly. I removed
  230.          this call to ShowWindow from DLL to give a programmer the
  231.          possibility to move the window after it's style was changed before
  232.          the window is displayed. Thus you can override the default
  233.                  positions of window and so on.
  234.                */
  235.          break;
  236.                /*
  237.           case 2006:
  238.          I want to point out that there is no example on something
  239.          like
  240.          ES_TB_Toggle_Popup_Child_Demo( hwndTBar );
  241.          It is not because it is undocumented but because it is
  242.          not implemented. This simply does not work, Windows 3.x
  243.          cannot convert WS_POPUP to WS_CHILD and back. Or at least I
  244.          can say I do not know about this possibility. If you want
  245.          this, you need to destroy the TB with one style and create
  246.          with another. Or tell me how to make it work.
  247.             I may include an additionla function to the ESTOOLS.DLL
  248.          to have this work done, but it won't be done throught a SendMessage
  249.                  API but simply by destruction of one window and creation of another.
  250.               */
  251.           case 2007:
  252.           {
  253.                  /* Toggles caption/no caption TB */
  254.          HMENU hm;
  255.          hm=GetMenu( hWnd );
  256.          CheckMenuItem( hm, wParam,
  257.             (GetMenuState( hm, wParam, MF_BYCOMMAND )&MF_CHECKED)?
  258.                     MF_UNCHECKED:MF_CHECKED );
  259.  
  260.          ES_TB_Toggle_Movable_Fixed_Demo( hwndTBar ); //Register!
  261.          ShowWindow( hwndTBar, SW_SHOW );
  262.                  break;
  263.           }
  264.           case 2008:
  265.           {
  266.                  /* Toggles border/no border TB styles */
  267.          HMENU hm;
  268.          hm=GetMenu( hWnd );
  269.          CheckMenuItem( hm, wParam,
  270.             (GetMenuState( hm, wParam, MF_BYCOMMAND )&MF_CHECKED)?
  271.                     MF_UNCHECKED:MF_CHECKED );
  272.  
  273.          ES_TB_Toggle_BorderStyle_Demo( hwndTBar );   //Register!
  274.          ShowWindow( hwndTBar, SW_SHOW );
  275.                  break;
  276.           }
  277.  
  278.           default:;
  279.        }
  280.        break;
  281.     case WM_CLOSE:
  282.        DestroyWindow(hWnd);
  283.        break;
  284.  
  285.     case WM_QUIT:
  286.     case WM_DESTROY:
  287.        PostQuitMessage(0);
  288.        break;
  289.  
  290.     /*
  291.        The following two message control the style of TB caption
  292.        It is intended to be active always when the parent is active.
  293.        It cannot be done without a cooperation from parent's side.
  294.        It may have some bugs. Try, if you have problems, contact me.
  295.     */  
  296.     case WM_NCACTIVATE:
  297.     /* The following is required in order to prevent DefWindowProc
  298.        from redrawing the caption bar of this window in inactive style
  299.        when tool bar window is activated. Try to remove this and you
  300.        will see caption bar flashing when you click in TB window.
  301.        Whenever TB becomes active it passes the active state to it's
  302.        parent and becomes inactive, to look active it redraws it's caption
  303.            in the active style.
  304.     */
  305.        if( (HWND)LOWORD(lParam)==hwndTBar )
  306.           return TRUE;
  307.  
  308.     case WM_ACTIVATEAPP:
  309.     /* This is necessary to draw a title bar of TB in inactive style
  310.        when the application becomes inactive. TB is never active and cannot
  311.        process this message by itself. If you remove this part TB will
  312.        remain looking 'active' even if another application is activated */
  313.        if( !wParam )
  314.           SendMessage( hwndTBar, WM_NCACTIVATE, FALSE, 0L );
  315.        else SendMessage( hwndTBar, WM_NCACTIVATE, TRUE, 0L );
  316.        //break;  -- DO NOT PUT IT HERE. Documentation states incorrectly that
  317.        // you can return 0 if you process this message. You cannot, if you
  318.        // return FALSE you prevent another application from activation.
  319.        // If you want you may try to return wParam for default processing.
  320.     default:
  321.        return (DefWindowProc(hWnd, message, wParam, lParam));
  322.    }
  323.    return (NULL);
  324. }
  325.  
  326. #pragma argsused
  327. /**************************************************************/
  328. int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  329.              LPSTR lpCmdLine, int nCmdShow)
  330. {
  331.     MSG msg;        
  332.     if( !hPrevInstance )    
  333.        if (!InitApplication(hInstance))
  334.         return (FALSE);    
  335.  
  336.     if (!InitInstance(hInstance, nCmdShow))
  337.        return (FALSE);
  338.  
  339.     while( GetMessage( &msg, NULL, NULL, NULL ) )    
  340.     {
  341.         TranslateMessage(&msg);    
  342.         DispatchMessage(&msg);    
  343.     }
  344.     return (msg.wParam);
  345. }
  346.  
  347.  
  348.