home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / graphics / gdi / fonts / ttfonts / toolbar.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-10-05  |  5.5 KB  |  172 lines

  1.  
  2. /******************************************************************************\
  3. *       This is a part of the Microsoft Source Code Samples. 
  4. *       Copyright (C) 1992-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. *  toolbar.c -- module for the "toolbar" on top of the main window.
  14. *   Includes the window procedure and an initialization routine.
  15. \**************************************************************************/
  16.  
  17. #include <windows.h>
  18. #include "ttfonts.h"
  19.  
  20.  
  21. /* for the initial positioning of the buttons within the toolbar. */
  22. #define SPACEBUTTON 8
  23. #define CXBUTTON ((GetSystemMetrics (SM_CXFULLSCREEN)) /5 -2*SPACEBUTTON)
  24. #define BUTTONTOP    TOOLBARHEIGHT/8
  25. #define BUTTONHEIGHT TOOLBARHEIGHT*3/4
  26. #define BUTTONLEFT(x) ((2*x+1)*SPACEBUTTON + x*CXBUTTON)
  27.  
  28. #define BORDER     2
  29.  
  30.  
  31.  
  32. int initTB (HWND hwndParent)
  33. {
  34. WNDCLASS  wc;
  35.  
  36.   wc.style = 0;
  37.   wc.lpfnWndProc = (WNDPROC)ToolBarWndProc;
  38.   wc.cbClsExtra = 0;
  39.   wc.cbWndExtra = 0;
  40.   wc.hInstance = hInst;
  41.   wc.hIcon = NULL;
  42.   wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  43.   wc.hbrBackground = GetStockObject(LTGRAY_BRUSH);
  44.   wc.lpszMenuName = NULL;
  45.   wc.lpszClassName = TEXT("ToolBar");
  46.  
  47.   if (!RegisterClass(&wc)) return (FALSE);
  48.  
  49.   hwndTB = CreateWindow(
  50.       TEXT("ToolBar"),
  51.       NULL,
  52.       WS_CHILD | WS_CLIPSIBLINGS | WS_VISIBLE,
  53.       0,0,
  54.       GetSystemMetrics (SM_CXFULLSCREEN),
  55.       TOOLBARHEIGHT,
  56.       hwndParent, NULL, hInst, NULL);
  57.  
  58.   if (!hwndTB) return (FALSE);
  59.  
  60.   return TRUE;
  61. }
  62.  
  63.  
  64. /**************************************************************************\
  65. *
  66. *  function:  ToolBarWndProc
  67. *
  68. *  input parameters:  normal window procedure parameters.
  69. *
  70. *  global variables:
  71. *   hwndMain - parent of the toolbar.
  72. *
  73. * When the window is created, create the various buttons.  When those
  74. *  buttons send WM_COMMAND messages later, send the messages back to hwndMain.
  75. *
  76. \**************************************************************************/
  77. LRESULT CALLBACK ToolBarWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  78. {
  79. static HWND  hwndButton, hwndEnumPrinter;
  80.   switch (message) {
  81.  
  82.     /**********************************************************************\
  83.     *  WM_CREATE
  84.     *
  85.     * Create the various buttons which are on the toolbar.  Once the buttons
  86.     *  are created, set the window ID so that the WM_COMMANDS may be
  87.     *  distinguished.
  88.     \**********************************************************************/
  89.     case WM_CREATE: {
  90.       hwndButton = CreateWindow(
  91.         TEXT("BUTTON"),TEXT("EnumFonts"),
  92.         WS_CHILD | WS_VISIBLE,
  93.         BUTTONLEFT(0),BUTTONTOP, CXBUTTON, BUTTONHEIGHT,
  94.         hwnd, NULL, hInst, NULL);
  95.       SetWindowLong (hwndButton, GWL_ID, TBID_ENUM);
  96.  
  97.       hwndButton = CreateWindow(
  98.         TEXT("BUTTON"),TEXT("CreateFont"),
  99.         WS_CHILD | WS_VISIBLE,
  100.         BUTTONLEFT(1),BUTTONTOP, CXBUTTON, BUTTONHEIGHT,
  101.         hwnd, NULL, hInst, NULL);
  102.       SetWindowLong (hwndButton, GWL_ID, TBID_CREATE);
  103.  
  104.       hwndButton = CreateWindow(
  105.         TEXT("BUTTON"),TEXT("GetMetrics"),
  106.         WS_CHILD | WS_VISIBLE ,
  107.         BUTTONLEFT(2),BUTTONTOP, CXBUTTON, BUTTONHEIGHT,
  108.         hwnd, NULL, hInst, NULL);
  109.       SetWindowLong (hwndButton, GWL_ID, TBID_GETTM);
  110.  
  111.       hwndButton = CreateWindow(
  112.         TEXT("BUTTON"),TEXT("GetFontData"),
  113.         WS_CHILD | WS_VISIBLE ,
  114.         BUTTONLEFT(3),BUTTONTOP, CXBUTTON, BUTTONHEIGHT,
  115.         hwnd, NULL, hInst, NULL);
  116.       SetWindowLong (hwndButton, GWL_ID, TBID_GETFONTDATA);
  117.  
  118.       hwndEnumPrinter = CreateWindow(
  119.         TEXT("BUTTON"),TEXT("Enum(Printer)"),
  120.         WS_CHILD | WS_VISIBLE,
  121.         BUTTONLEFT(4),BUTTONTOP, CXBUTTON, BUTTONHEIGHT,
  122.         hwnd, NULL, hInst, NULL);
  123.       SetWindowLong (hwndEnumPrinter, GWL_ID, TBID_PRINT);
  124.  
  125.     } break;
  126.  
  127.  
  128.     /**********************************************************************\
  129.     *  WM_COMMAND
  130.     *
  131.     * Send the command messages back to hwndMain.
  132.     *  except for the one to disable the printer button.
  133.     \**********************************************************************/
  134.     case WM_COMMAND:
  135.       if (wParam == IDU_NOPRINTER)
  136.         EnableWindow (hwndEnumPrinter, FALSE);
  137.       else
  138.         PostMessage (hwndMain,message, wParam, lParam);
  139.     break;
  140.  
  141.  
  142.     /**********************************************************************\
  143.     *  WM_PAINT
  144.     *
  145.     * Paint two rectangular strips, one on top, one on bottom.
  146.     \**********************************************************************/
  147.     case WM_PAINT : {
  148.       PAINTSTRUCT ps;
  149.       RECT rect;
  150.       HDC hdc;
  151.  
  152.       hdc = BeginPaint(hwnd, &ps);
  153.       GetClientRect (hwnd, &rect);
  154.       rect.right --;
  155.       rect.bottom --;
  156.  
  157.       SelectObject (hdc, GetStockObject (BLACK_PEN));
  158.       MoveToEx (hdc, rect.right, rect.top, NULL);
  159.       LineTo (hdc, rect.right, rect.bottom);
  160.       LineTo (hdc, rect.left, rect.bottom);
  161.       SelectObject (hdc, GetStockObject (WHITE_PEN));
  162.       LineTo (hdc, rect.left, rect.top);
  163.       LineTo (hdc, rect.right, rect.top);
  164.  
  165.       EndPaint (hwnd, &ps);
  166.     } break;
  167.  
  168.  
  169.   } /* end switch */
  170.   return (DefWindowProc(hwnd, message, wParam, lParam));
  171. }
  172.