home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / winbase / debug / deb / toolbar.c < prev    next >
C/C++ Source or Header  |  1997-10-05  |  7KB  |  201 lines

  1.  
  2. /******************************************************************************\
  3. *       This is a part of the Microsoft Source Code Samples. 
  4. *       Copyright (C) 1993-1997 Microsoft Corporation.
  5. *       All rights reserved. 
  6. *       This source code is only intended as a supplement to 
  7. *       Microsoft Development Tools and/or WinHelp documentation.
  8. *       See these sources for detailed information regarding the 
  9. *       Microsoft samples programs.
  10. \******************************************************************************/
  11.  
  12. // ************************************************************************
  13. // MODULE    : ToolBar.C
  14. // PURPOSE   : A Win32 DLL containing a simple text ToolBar
  15. // FUNCTIONS :
  16. //   DllMain()         - Dll entry point (via _DllMainCRTStartup)
  17. //   TextButtonBar()   - generate a text button ToolBar
  18. //   TextButtonBarProc - processes messages for the TextButtonBarWClass
  19. // COMMENTS  :
  20. //
  21. // ************************************************************************
  22. #define   STRICT               // enable strict typing
  23. #include <Windows.H>           // required for all Windows applications
  24.  
  25. #include "ToolBar.H"           // specific to this program
  26.  
  27. //-- internal data
  28. HWND hWndToolBarOwner;         // window handle of Toolbar owner
  29.                                // where all WM_COMMAND messages are sent
  30.  
  31. //-- internal prototypes
  32. LRESULT CALLBACK TextButtonBarProc( HWND, UINT, WPARAM, LPARAM );
  33.  
  34.  
  35. // ************************************************************************
  36. // FUNCTION : DllMain( HINSTANCE, DWORD, LPVOID )
  37. // PURPOSE  : DllMain is called by the C run-time library from the
  38. //            _DllMainCRTStartup entry point.  The DLL entry point gets
  39. //            called (entered) on the following events: "Process Attach",
  40. //            "Thread Attach", "Thread Detach" or "Process Detach".
  41. // COMMENTS : No initialization is needed here so this entry point simply
  42. //            returns TRUE.
  43. // ************************************************************************
  44. BOOL WINAPI
  45. DllMain( HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpvReserved )
  46. {
  47.   UNREFERENCED_PARAMETER( hInstDLL );
  48.   UNREFERENCED_PARAMETER( fdwReason );
  49.   UNREFERENCED_PARAMETER( lpvReserved );
  50.  
  51.   return( TRUE );
  52. }
  53.  
  54.  
  55. // ************************************************************************
  56. // FUNCTION : TextButtonBar( HWND, LPTEXTBUTTON, LPINT )
  57. // PURPOSE  : Generate a simple text button ToolBar
  58. // COMMENTS : lpTextButton points to an array of TEXTBUTTON
  59. //            structures.  The last structure must set the
  60. //            member lpButtonText to NULL.  lpHeight contains the
  61. //            height of the ToolBar window.
  62. // ************************************************************************
  63. HWND
  64. TextButtonBar( HWND hWndParent, LPTEXTBUTTON lpTextButton, LPINT lpHeight )
  65. {
  66.   #define BUTTON_BORDER    4
  67.   #define BUTTONBAR_BORDER 4
  68.  
  69.   static HWND         hWndTextButtonBar;
  70.  
  71.   HDC          hDC;
  72.   WNDCLASS     WndClass;
  73.   LONG         ButtonWidth;
  74.   LONG         ButtonHeight;
  75.   INT          xPosButton;
  76.   INT          ButtonSpacing;
  77.   INT          ButtonSpace;
  78.   LPTSTR       lpButtonString;
  79.   LPTEXTBUTTON lpTempTextButton;
  80.   TEXTMETRIC   tm;
  81.   SIZE         ButtonTextSize;
  82.   RECT         rect;
  83.  
  84.   //-- store owner of Toolbar
  85.   hWndToolBarOwner = hWndParent;
  86.  
  87.   //-- register the TestButtonBar window class
  88.   WndClass.style         = (UINT) NULL;
  89.   WndClass.lpfnWndProc   = (WNDPROC) TextButtonBarProc;
  90.   WndClass.cbClsExtra    = 0;
  91.   WndClass.cbWndExtra    = 0;
  92.   WndClass.hInstance     = NULL;
  93.   WndClass.hIcon         = NULL;
  94.   WndClass.hCursor       = NULL;
  95.   WndClass.hbrBackground = (HBRUSH) GetStockObject(LTGRAY_BRUSH);
  96.   WndClass.lpszMenuName  = NULL;
  97.   WndClass.lpszClassName = TEXT( "TextButtonBarWClass" );
  98.  
  99.   if( !RegisterClass( &WndClass ) )
  100.     return( FALSE );
  101.  
  102.   hDC = GetDC( hWndParent );
  103.   GetTextMetrics( hDC, &tm );
  104.   ButtonHeight = 2 * BUTTON_BORDER + tm.tmHeight;
  105.   *lpHeight = 2 * BUTTONBAR_BORDER + ButtonHeight;
  106.  
  107.   GetWindowRect( GetDesktopWindow(), &rect );
  108.   hWndTextButtonBar = CreateWindow(
  109.                         TEXT( "TextButtonBarWClass" ), TEXT( "Toolbar" ),
  110.                         WS_CHILD | WS_CLIPSIBLINGS,
  111.                         0, 0, rect.right - rect.left, *lpHeight,
  112.                         hWndToolBarOwner,
  113.                         (HMENU) NULL, (HANDLE) NULL, NULL );
  114.  
  115.   SetWindowPos( hWndTextButtonBar, (HWND) 1, 0, 0, 0, 0,
  116.     SWP_NOMOVE | SWP_NOSIZE );
  117.  
  118.   //-- button spacing information
  119.   ButtonSpacing = BUTTON_BORDER / 2;
  120.   ButtonSpace   = BUTTON_BORDER * 4;
  121.   xPosButton    = BUTTON_BORDER * 2;
  122.  
  123.   //-- Create all the button windows
  124.   for( lpTempTextButton = lpTextButton;
  125.          !( (lpTempTextButton->lpButtonText) == '\0'
  126.            && (lpTempTextButton->idButton) == (UINT) NULL);
  127.          lpTempTextButton++ ) {
  128.  
  129.     //-- if TB_SPACE then adjust new xPos and continue
  130.     if( lpTempTextButton->idButton == TB_SPACE ) {
  131.       xPosButton += ButtonSpace;
  132.       continue;
  133.     }
  134.  
  135.     lpButtonString = lpTempTextButton->lpButtonText;
  136.     GetTextExtentPoint( hDC, lpButtonString, lstrlen(lpButtonString), &ButtonTextSize );
  137.     ButtonWidth = (4 * BUTTON_BORDER) + (LONG) ButtonTextSize.cx;
  138.  
  139.     (lpTempTextButton->hWndButton) = CreateWindow(
  140.          TEXT( "BUTTON" ), lpButtonString,
  141.          WS_VISIBLE | BS_PUSHBUTTON | WS_CHILD,
  142.          xPosButton, BUTTONBAR_BORDER, ButtonWidth, ButtonHeight,
  143.          hWndTextButtonBar,
  144.          (HMENU) lpTempTextButton->idButton,
  145.          (HANDLE) NULL, NULL );
  146.  
  147.     xPosButton += ButtonWidth + ButtonSpacing;
  148.   }
  149.   ReleaseDC( hWndParent, hDC );
  150.  
  151.   return( hWndTextButtonBar );
  152. }
  153.  
  154.  
  155. // ************************************************************************
  156. // FUNCTION : TextButtonBarProc( HWND, UINT, WPARAM, LPARAM )
  157. // PURPOSE  : Processes messages for the TextButtonBarWClass
  158. // MESSAGES :
  159. //  WM_COMMAND - forwarded to the owner window
  160. //  WM_PAINT   - draws a 3D background
  161. // COMMENTS :
  162. // ************************************************************************
  163. LRESULT CALLBACK
  164. TextButtonBarProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  165. {
  166.   static HDC         hDC;
  167.   static RECT        rect;
  168.   static PAINTSTRUCT ps;
  169.  
  170.   switch( uMsg ) {
  171.  
  172.     case WM_COMMAND:
  173.       SendMessage( hWndToolBarOwner, uMsg, wParam, lParam );
  174.       return( FALSE );
  175.  
  176.     case WM_PAINT:
  177.       hDC = BeginPaint( hWnd, &ps );
  178.       GetClientRect( hWnd, &rect );
  179.  
  180.       SelectObject( hDC, GetStockObject(WHITE_PEN) );
  181.       MoveToEx( hDC, rect.left, rect.top, NULL );
  182.       LineTo( hDC, rect.right+1, rect.top );
  183.  
  184.       #define DKGRAY_PEN RGB(128, 128, 128)
  185.       SelectObject( hDC, (HANDLE) CreatePen( PS_SOLID, 1, DKGRAY_PEN ) );
  186.       MoveToEx( hDC, rect.left, rect.bottom-2, NULL );
  187.       LineTo( hDC, rect.right+1, rect.bottom-2 );
  188.  
  189.       SelectObject( hDC, (HANDLE) CreatePen( PS_SOLID, 1, BLACK_PEN ) );
  190.       MoveToEx( hDC, rect.left, rect.bottom-1, NULL );
  191.       LineTo( hDC, rect.right+1, rect.bottom-1 );
  192.  
  193.       DeleteObject( SelectObject( hDC, GetStockObject(BLACK_PEN) ) );
  194.       EndPaint( hWnd, &ps );
  195.       return( FALSE );
  196.  
  197.     default:
  198.       return( DefWindowProc(hWnd, uMsg, wParam, lParam) );
  199.   }
  200. }
  201.