home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / winui / commctrl / rebar / rebar.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-12-11  |  16.5 KB  |  637 lines

  1. /**************************************************************************
  2.    THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  3.    ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  4.    THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  5.    PARTICULAR PURPOSE.
  6.  
  7.    Copyright 1997 - 1998 Microsoft Corporation.  All Rights Reserved.
  8. **************************************************************************/
  9.  
  10. /**************************************************************************
  11.  
  12.    File:          ReBar.c
  13.    
  14.    Description:   ReBar sample implementation.
  15.  
  16. **************************************************************************/
  17.  
  18. #define STRICT
  19.  
  20. /**************************************************************************
  21.    Include Files
  22. **************************************************************************/
  23.  
  24. #include <windows.h>
  25. #include <windowsx.h>
  26. #include <commctrl.h>
  27. #include "resource.h"
  28.  
  29. /**************************************************************************
  30.    Local Function Prototypes
  31. **************************************************************************/
  32.  
  33. int PASCAL WinMain(HINSTANCE, HINSTANCE, LPSTR, int);
  34. BOOL InitApplication(HINSTANCE);
  35. BOOL InitInstance(HINSTANCE, int);
  36. LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);
  37. BOOL CALLBACK About(HWND, UINT, WPARAM, LPARAM);
  38. HWND BuildRebar(HWND);
  39. LRESULT DoNotify(HWND, WPARAM, LPARAM);
  40. void MoveRebar(HWND);
  41. LRESULT HandleMenuPopup(HMENU);
  42. LRESULT HandleCommand(HWND, WPARAM, LPARAM);
  43.  
  44. /**************************************************************************
  45.    Global Variables
  46. **************************************************************************/
  47.  
  48. #define ID_REBAR     1000
  49. #define ID_BUTTON    2000
  50. #define ID_COMBOBOX  2001
  51.  
  52. #define TOP    0x00
  53. #define LEFT   0x01
  54. #define BOTTOM 0x02
  55. #define RIGHT  0x03
  56.  
  57. HINSTANCE   g_hInst;
  58. WORD        g_wSide;
  59.  
  60. /******************************************************************************
  61.  
  62.    WinMain
  63.  
  64.    Parameters:
  65.  
  66.    Description:
  67.  
  68.    Returns:
  69.  
  70. ******************************************************************************/
  71.  
  72. int PASCAL WinMain(  HINSTANCE hInstance,
  73.                      HINSTANCE hPrevInstance,
  74.                      LPSTR lpCmdLine,
  75.                      int nCmdShow)
  76. {
  77. MSG      msg;
  78. INITCOMMONCONTROLSEX iccex;
  79.  
  80. iccex.dwSize = sizeof(INITCOMMONCONTROLSEX);
  81. iccex.dwICC = ICC_COOL_CLASSES;
  82. InitCommonControlsEx(&iccex);
  83.  
  84. if(!hPrevInstance)
  85.    if(!InitApplication(hInstance))
  86.       return FALSE;
  87.  
  88. if (!InitInstance(hInstance, nCmdShow))
  89.    return FALSE;
  90.  
  91. while(GetMessage( &msg, NULL, 0x00, 0x00))
  92.    {
  93.    TranslateMessage(&msg);
  94.    DispatchMessage(&msg);
  95.    }
  96.  
  97. return msg.wParam;
  98. }
  99.  
  100. /*****************************************************************************
  101.  
  102.    InitApplication
  103.  
  104.    Parameters:
  105.  
  106.    Description:
  107.    
  108.    Returns:
  109.  
  110. *****************************************************************************/
  111.  
  112. BOOL InitApplication(HINSTANCE hInstance)
  113. {
  114. WNDCLASS  wc;
  115.  
  116. wc.style          = 0;
  117. wc.lpfnWndProc    = (WNDPROC)MainWndProc;
  118. wc.cbClsExtra     = 0;
  119. wc.cbWndExtra     = 0;
  120. wc.hInstance      = hInstance;
  121. wc.hIcon          = LoadIcon(NULL, IDI_APPLICATION);
  122. wc.hCursor        = LoadCursor(NULL, IDC_ARROW);
  123. wc.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
  124. wc.lpszMenuName   = MAKEINTRESOURCE(IDM_GENERICMENU);
  125. wc.lpszClassName  = "GenericClass";
  126.  
  127. return RegisterClass(&wc);
  128. }
  129.  
  130. /*****************************************************************************
  131.  
  132.    InitInstance
  133.  
  134.    Parameters:
  135.  
  136.    Description:
  137.    
  138.    Returns:
  139.  
  140. *****************************************************************************/
  141.  
  142. BOOL InitInstance(   HINSTANCE hInstance,
  143.                      int nCmdShow)
  144. {
  145. HWND  hWnd;
  146.  
  147. g_hInst = hInstance;
  148.  
  149. hWnd = CreateWindowEx(  0,
  150.                         "GenericClass",
  151.                         "Generic Application",
  152.                         WS_OVERLAPPEDWINDOW,
  153.                         CW_USEDEFAULT,
  154.                         CW_USEDEFAULT,
  155.                         CW_USEDEFAULT,
  156.                         CW_USEDEFAULT,
  157.                         NULL,
  158.                         NULL,
  159.                         hInstance,
  160.                         NULL);
  161.  
  162. if (!hWnd)
  163.    return FALSE;
  164.  
  165. ShowWindow(hWnd, nCmdShow);
  166. UpdateWindow(hWnd);
  167.  
  168. return TRUE;
  169.  
  170. }
  171.  
  172. /*****************************************************************************
  173.  
  174.    MainWndProc
  175.  
  176.    Parameters:
  177.  
  178.    Description:
  179.    
  180.    Returns:
  181.  
  182. *****************************************************************************/
  183.  
  184. LRESULT CALLBACK MainWndProc( HWND hWnd,
  185.                               UINT uMessage,
  186.                               WPARAM wParam,
  187.                               LPARAM lParam)
  188. {
  189. switch (uMessage)
  190.    {
  191.    case WM_CREATE:
  192.       g_wSide = TOP;
  193.  
  194.       BuildRebar(hWnd);
  195.       break;
  196.  
  197.    case WM_NOTIFY:
  198.       return DoNotify(hWnd, wParam, lParam);
  199.    
  200.    case WM_SIZE:
  201.       MoveRebar(hWnd);
  202.       break;
  203.  
  204.    case WM_DESTROY:
  205.       PostQuitMessage(0);
  206.       break;
  207.  
  208.    case WM_INITMENUPOPUP:
  209.       return HandleMenuPopup((HMENU)wParam);
  210.    
  211.    case WM_COMMAND:
  212.       return HandleCommand(hWnd, wParam, lParam);
  213.       
  214.    default:
  215.       break;
  216.    }
  217. return DefWindowProc(hWnd, uMessage, wParam, lParam);
  218. }
  219.  
  220. /*****************************************************************************
  221.  
  222.    About
  223.  
  224.    Parameters:
  225.  
  226.    Description:
  227.    
  228.    Returns:
  229.  
  230. *****************************************************************************/
  231.  
  232. BOOL CALLBACK About( HWND hDlg, 
  233.                      UINT uMessage, 
  234.                      WPARAM wParam, 
  235.                      LPARAM lParam)
  236. {
  237. switch (uMessage)
  238.    {
  239.    case WM_INITDIALOG:
  240.       return TRUE;
  241.       
  242.    case WM_COMMAND:
  243.       switch(wParam)
  244.          {
  245.          case IDOK:
  246.             EndDialog(hDlg, IDOK);
  247.             return TRUE;
  248.  
  249.          case IDCANCEL:
  250.             EndDialog(hDlg, IDCANCEL);
  251.             return TRUE;
  252.          }
  253.       break;
  254.     } 
  255.     
  256. return FALSE;
  257. }
  258.  
  259. /*****************************************************************************
  260.  
  261.    BuildRebar
  262.  
  263. *****************************************************************************/
  264.  
  265. HWND BuildRebar(HWND hwndParent)
  266. {
  267. HWND     hwndRebar = NULL;
  268. LRESULT  lResult;
  269.  
  270. hwndRebar = CreateWindowEx(   WS_EX_TOOLWINDOW, 
  271.                               REBARCLASSNAME, 
  272.                               NULL,
  273.                               WS_VISIBLE |
  274.                                  WS_BORDER | 
  275.                                  WS_CHILD | 
  276.                                  WS_CLIPCHILDREN | 
  277.                                  WS_CLIPSIBLINGS | 
  278.                                  RBS_VARHEIGHT | 
  279.                                  RBS_BANDBORDERS | 
  280.                                  CCS_NODIVIDER | 
  281.                                  CCS_NOPARENTALIGN |
  282.                                  ((g_wSide & 0x01) ? CCS_VERT : 0) | //g_wSide is odd if this is a vertical bar
  283.                                  0,
  284.                               0, 
  285.                               0, 
  286.                               200, 
  287.                               100, 
  288.                               hwndParent, 
  289.                               (HMENU)ID_REBAR, 
  290.                               g_hInst, 
  291.                               NULL);
  292.  
  293. if(hwndRebar)
  294.    {
  295.    REBARINFO      rbi;
  296.    HIMAGELIST     himlRebar;
  297.    HICON          hIcon;
  298.    REBARBANDINFO  rbbi;
  299.    HWND           hwndChild;
  300.    RECT           rc;
  301.    TCHAR          szString[64];
  302.  
  303.    //set up the ReBar
  304.    himlRebar = ImageList_Create(32, 32, ILC_COLORDDB | ILC_MASK, 1, 0);
  305.    hIcon = LoadIcon(g_hInst, MAKEINTRESOURCE(IDI_ICON1));
  306.  
  307.    ImageList_AddIcon(himlRebar, hIcon);
  308.  
  309.    rbi.cbSize  = sizeof(rbi);
  310.    rbi.fMask   = RBIM_IMAGELIST;
  311.    rbi.himl    = himlRebar;
  312.    lResult = SendMessage(hwndRebar, RB_SETBARINFO, 0, (LPARAM)&rbi);
  313.  
  314.    //add a band that contains a combobox
  315.    hwndChild = CreateWindowEx(   0, 
  316.                                  TEXT("combobox"), 
  317.                                  NULL,
  318.                                  WS_VISIBLE |
  319.                                     WS_CHILD | 
  320.                                     WS_TABSTOP |
  321.                                     WS_VSCROLL |
  322.                                     WS_CLIPCHILDREN | 
  323.                                     WS_CLIPSIBLINGS | 
  324.                                     CBS_AUTOHSCROLL | 
  325.                                     CBS_DROPDOWN | 
  326.                                     0,
  327.                                  0, 
  328.                                  0, 
  329.                                  100, 
  330.                                  200, 
  331.                                  hwndRebar, 
  332.                                  (HMENU)ID_COMBOBOX, 
  333.                                  g_hInst, 
  334.                                  NULL);
  335.  
  336.    //add some stuff to the combobox
  337.    {
  338.    int   i;
  339.  
  340.    SendMessage(hwndChild, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), MAKELPARAM(TRUE, 0));
  341.    
  342.    for(i = 0; i < 25; i++)
  343.       {
  344.       wsprintf(szString, TEXT("Item %d"), i + 1);
  345.       SendMessage(hwndChild, CB_ADDSTRING, 0, (LPARAM)szString);
  346.       }
  347.    }
  348.    
  349.    GetWindowRect(hwndChild, &rc);
  350.    
  351.    ZeroMemory(&rbbi, sizeof(rbbi));
  352.    rbbi.cbSize       = sizeof(REBARBANDINFO);
  353.    rbbi.fMask        = RBBIM_SIZE | 
  354.                         RBBIM_CHILD | 
  355.                         RBBIM_CHILDSIZE | 
  356.                         RBBIM_ID | 
  357.                         RBBIM_STYLE | 
  358.                         RBBIM_TEXT |
  359.                         RBBIM_BACKGROUND |
  360.                         RBBIM_IMAGE |
  361.                         0;
  362.    
  363.    rbbi.cxMinChild   = rc.right - rc.left;
  364.    rbbi.cyMinChild   = rc.bottom - rc.top;
  365.    rbbi.cx           = 100;
  366.    rbbi.fStyle       = RBBS_CHILDEDGE | 
  367.                         RBBS_FIXEDBMP |
  368.                         RBBS_GRIPPERALWAYS |
  369.                         0;
  370.    rbbi.wID          = ID_COMBOBOX;
  371.    rbbi.hwndChild    = hwndChild;
  372.    rbbi.lpText       = TEXT("ComboBox");
  373.    rbbi.cch          = 2;
  374.    rbbi.hbmBack      = LoadBitmap(g_hInst, MAKEINTRESOURCE(IDB_BACKGROUND));
  375.    rbbi.iImage       = 0;
  376.  
  377.    lResult = SendMessage(hwndRebar, RB_INSERTBAND, (WPARAM)-1, (LPARAM)(LPREBARBANDINFO)&rbbi);
  378.  
  379.    //add a band that contains a button
  380.    hwndChild = CreateWindowEx(   0, 
  381.                                  TEXT("button"), 
  382.                                  TEXT("Button"),
  383.                                  WS_CHILD | 
  384.                                     BS_PUSHBUTTON | 
  385.                                     0,
  386.                                  0, 
  387.                                  0, 
  388.                                  100, 
  389.                                  50, 
  390.                                  hwndRebar, 
  391.                                  (HMENU)ID_BUTTON, 
  392.                                  g_hInst, 
  393.                                  NULL);
  394.    
  395.    GetWindowRect(hwndChild, &rc);
  396.    
  397.    ZeroMemory(&rbbi, sizeof(rbbi));
  398.    rbbi.cbSize       = sizeof(REBARBANDINFO);
  399.    rbbi.fMask        = RBBIM_SIZE | 
  400.                         RBBIM_CHILD | 
  401.                         RBBIM_CHILDSIZE | 
  402.                         RBBIM_ID | 
  403.                         RBBIM_STYLE | 
  404.                         RBBIM_TEXT |
  405.                         RBBIM_BACKGROUND |
  406.                         0;
  407.    rbbi.cxMinChild   = rc.right - rc.left;
  408.    rbbi.cyMinChild   = rc.bottom - rc.top;
  409.    rbbi.cx           = 100;
  410.    rbbi.fStyle       = RBBS_CHILDEDGE | 
  411.                         RBBS_FIXEDBMP |
  412.                         RBBS_GRIPPERALWAYS |
  413.                         0;
  414.    rbbi.wID          = ID_BUTTON;
  415.    rbbi.hwndChild    = hwndChild;
  416.    rbbi.lpText       = TEXT("Button");
  417.    rbbi.hbmBack      = LoadBitmap(g_hInst, MAKEINTRESOURCE(IDB_BACKGROUND));
  418.  
  419.  
  420.    lResult = SendMessage(hwndRebar, RB_INSERTBAND, (WPARAM)-1, (LPARAM)(LPREBARBANDINFO)&rbbi);
  421.    }
  422.  
  423. MoveRebar(hwndParent);
  424.  
  425. return hwndRebar;
  426. }
  427.  
  428. /*****************************************************************************
  429.  
  430.    DoNotify
  431.  
  432. *****************************************************************************/
  433.  
  434. LRESULT DoNotify(HWND hWnd, WPARAM wParam, LPARAM lParam)
  435. {
  436. LPNMHDR           lpNM = (LPNMHDR)lParam;
  437.  
  438. switch(lpNM->code)
  439.    {
  440.    default:
  441.       break;
  442.    }
  443.  
  444. return FALSE;
  445. }
  446.  
  447. /**************************************************************************
  448.  
  449.    MoveRebar()
  450.    
  451. **************************************************************************/
  452.  
  453. void MoveRebar(HWND hWnd)
  454. {
  455. RECT  rc,
  456.       rcRebar;
  457. int   x,
  458.       y,
  459.       cx,
  460.       cy;
  461.  
  462. GetClientRect(hWnd, &rc);
  463. GetWindowRect(GetDlgItem(hWnd, ID_REBAR), &rcRebar);
  464.  
  465. switch(g_wSide)
  466.    {
  467.    default:
  468.    case TOP:
  469.       //align the rebar along the top of the window
  470.       x = 0;
  471.       y = 0;
  472.       cx = rc.right - rc.left;
  473.       cy = rc.bottom - rc.top;
  474.       break;
  475.  
  476.    case LEFT:
  477.       //align the rebar along the left side of the window
  478.       x = 0;
  479.       y = 0;
  480.       cx = rcRebar.right - rcRebar.left;
  481.       cy = rc.bottom - rc.top;
  482.       break;
  483.  
  484.    case BOTTOM:
  485.       //align the rebar along the bottom of the window
  486.       x = 0;
  487.       y = rc.bottom - (rcRebar.bottom - rcRebar.top);
  488.       cx = rc.right - rc.left;
  489.       cy = rcRebar.bottom - rcRebar.top;
  490.       break;
  491.  
  492.    case RIGHT:
  493.       //align the coolbar along the right side of the window
  494.       x = rc.right - (rcRebar.right - rcRebar.left);
  495.       y = 0;
  496.       cx = rcRebar.right - rcRebar.left;
  497.       cy = rc.bottom - rc.top;
  498.       break;
  499.    }
  500.  
  501. MoveWindow( GetDlgItem(hWnd, ID_REBAR), 
  502.             x, 
  503.             y, 
  504.             cx, 
  505.             cy, 
  506.             TRUE);
  507. }
  508.  
  509. /**************************************************************************
  510.  
  511.    HandleMenuPopup()
  512.    
  513. **************************************************************************/
  514.  
  515. LRESULT HandleMenuPopup(HMENU hMenu)
  516. {
  517. UINT  uSelect;
  518.  
  519. switch(g_wSide)
  520.    {
  521.    default:
  522.    case TOP:
  523.       uSelect = IDM_TOP;
  524.       break;
  525.  
  526.    case LEFT:
  527.       uSelect = IDM_LEFT;
  528.       break;
  529.  
  530.    case BOTTOM:
  531.       uSelect = IDM_BOTTOM;
  532.       break;
  533.  
  534.    case RIGHT:
  535.       uSelect = IDM_RIGHT;
  536.       break;
  537.    }
  538.  
  539. CheckMenuRadioItem(hMenu, IDM_TOP, IDM_BOTTOM, uSelect, MF_BYCOMMAND);
  540.  
  541. return 0;
  542. }
  543.  
  544. /**************************************************************************
  545.  
  546.    HandleCommand()
  547.    
  548. **************************************************************************/
  549.  
  550. LRESULT HandleCommand(HWND hWnd, WPARAM wParam, LPARAM lParam)
  551. {
  552. switch(GET_WM_COMMAND_ID(wParam, lParam))
  553.    {
  554.    case ID_BUTTON:
  555.       break;
  556.  
  557.    case ID_COMBOBOX:
  558.       break;
  559.  
  560.    case IDM_TOP:
  561.       if(g_wSide != TOP)
  562.          {
  563.          //destroy the existing Rebar
  564.          DestroyWindow(GetDlgItem(hWnd, ID_REBAR));
  565.          
  566.          //change to the new side
  567.          g_wSide = TOP;
  568.  
  569.          //create the new Rebar
  570.          BuildRebar(hWnd);
  571.  
  572.          //we have to do this because the rebar will recalculate it's rectangle after the first time it is sized
  573.          MoveRebar(hWnd);
  574.          }
  575.       break;
  576.  
  577.    case IDM_BOTTOM:
  578.       if(g_wSide != BOTTOM)
  579.          {
  580.          //destroy the existing Rebar
  581.          DestroyWindow(GetDlgItem(hWnd, ID_REBAR));
  582.          
  583.          g_wSide = BOTTOM;
  584.  
  585.          //create the new Rebar
  586.          BuildRebar(hWnd);
  587.  
  588.          //we have to do this because the rebar will recalculate it's rectangle after the first time it is sized
  589.          MoveRebar(hWnd);
  590.          }
  591.       break;
  592.  
  593.    case IDM_LEFT:
  594.       if(g_wSide != LEFT)
  595.          {
  596.          //destroy the existing Rebar
  597.          DestroyWindow(GetDlgItem(hWnd, ID_REBAR));
  598.          
  599.          g_wSide = LEFT;
  600.  
  601.          //create the new Rebar
  602.          BuildRebar(hWnd);
  603.  
  604.          //we have to do this because the rebar will recalculate it's rectangle after the first time it is sized
  605.          MoveRebar(hWnd);
  606.          }
  607.       break;
  608.  
  609.    case IDM_RIGHT:
  610.       if(g_wSide != RIGHT)
  611.          {
  612.          //destroy the existing Rebar
  613.          DestroyWindow(GetDlgItem(hWnd, ID_REBAR));
  614.          
  615.          g_wSide = RIGHT;
  616.  
  617.          //create the new Rebar
  618.          BuildRebar(hWnd);
  619.  
  620.          //we have to do this because the rebar will recalculate it's rectangle after the first time it is sized
  621.          MoveRebar(hWnd);
  622.          }
  623.       break;
  624.    
  625.    case IDM_EXIT:
  626.       DestroyWindow(hWnd);
  627.       break;
  628.    
  629.    case IDM_ABOUT:
  630.       DialogBox(g_hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
  631.       break;   
  632.       
  633.    }
  634.  
  635. return TRUE;
  636. }
  637.