home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / win_lrn / dialog / gnextitm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-11  |  7.6 KB  |  216 lines

  1. /*
  2.  * This application demonstrates the use of GetNextDlgGroupItem() and
  3.  * GetNextDlgTabItem(). In the dialog box, choosing the radiobutton
  4.  * and selecting the direction and the "NextTab" or "NextGroup" push
  5.  * button will change the focus to the next repsective item -- depending
  6.  * upon whether the previous flag and the "Tab" or "Group" selection.
  7.  *
  8.  */
  9.  
  10. #include "windows.h"
  11. #include "gnextitm.h"
  12.  
  13. /* Global Variables */
  14. static HANDLE hInst;       /* Instance handle for dialog box.          */
  15. FARPROC       lpprocNext;  /* Long pointer to dialog procedure.        */
  16. HWND          hGrpCurrent; /* Handle to the current group push button. */
  17. HWND          hTabCurrent; /* Handle to the current tab push button.   */
  18. BOOL          bDirCurrent; /* Current direction to get the next item.  */
  19.  
  20. /* Forward References */
  21. long FAR PASCAL NextWndProc(HWND, unsigned, WORD, LONG);
  22. BOOL FAR PASCAL NextItem(HWND, unsigned, WORD, LONG);
  23.  
  24. /* Dialog Procedure */
  25. BOOL FAR PASCAL NextItem(hDlg, message, wParam, lParam)
  26. HWND hDlg;
  27. unsigned message;
  28. WORD wParam;
  29. LONG lParam;
  30. {
  31.    HWND hGNextItem; /* Next Group Item */
  32.    HWND hTNextItem; /* Next Tab Item   */
  33.    int  i;          /* Counter         */
  34.  
  35.    switch (message) {
  36.       case WM_COMMAND:
  37.          switch (wParam) {
  38.             case ID_DONE: /* If all done, quit */
  39.                EndDialog(hDlg, TRUE);
  40.                return TRUE;
  41.  
  42.             case ID_DIRECTION: /* Change the "Previous" flag for direction */
  43.                if (bDirCurrent == TRUE)
  44.                   bDirCurrent = FALSE;
  45.                else
  46.                   bDirCurrent = TRUE;
  47.                return TRUE;
  48.  
  49.             case ID_TABFRST: /* Manually set the tab button focus */
  50.             case ID_TABSEC:
  51.             case ID_TABTHRD:
  52.                CheckRadioButton(hDlg, ID_TABFRST,
  53.                                 ID_TABTHRD, wParam);
  54.                hTabCurrent = GetDlgItem(hDlg, wParam);
  55.                return TRUE;
  56.  
  57.             case ID_GRPFRST: /* Manually set the group button focus */
  58.             case ID_GRPSEC:
  59.             case ID_GRPTHRD:
  60.                CheckRadioButton(hDlg, ID_GRPFRST,
  61.                                 ID_GRPTHRD, wParam);
  62.                hGrpCurrent = GetDlgItem(hDlg, wParam);
  63.                return TRUE;
  64.  
  65.             case ID_NEXTTAB: /* Get the next tab item */
  66.                /* The first two cases are needed because the dialog
  67.                   manager does neat things with tabs. */
  68.                if ((hTabCurrent == GetDlgItem(hDlg, ID_TABFRST)) &&
  69.                    (bDirCurrent == TRUE)) {
  70.                    CheckRadioButton(hDlg, ID_TABFRST,
  71.                                     ID_TABTHRD, ID_TABTHRD);
  72.                    hTabCurrent = GetDlgItem(hDlg, ID_TABTHRD);
  73.                    }
  74.                else if ((hTabCurrent == GetDlgItem(hDlg, ID_TABTHRD)) &&
  75.                         (bDirCurrent == FALSE)) {
  76.                    CheckRadioButton(hDlg, ID_TABFRST,
  77.                                     ID_TABTHRD, ID_TABFRST);
  78.                    hTabCurrent = GetDlgItem(hDlg, ID_TABFRST);
  79.                    }
  80.                else { /* Finally, you can do things properly */
  81.                   hTNextItem = GetNextDlgGroupItem(hDlg,
  82.                                                   hTabCurrent,
  83.                                                   bDirCurrent);
  84.                   hTabCurrent = hTNextItem;
  85.                   for (i = ID_TABFRST; i <= ID_TABTHRD; i = i + 25)
  86.                      if (GetDlgItem(hDlg, i) == hTabCurrent)
  87.                         CheckRadioButton(hDlg, ID_TABFRST,
  88.                                          ID_TABTHRD, i);
  89.                   }
  90.                return TRUE;
  91.  
  92.             case ID_NEXTGROUP: /* Groups are much neater to implement */
  93.                hGNextItem = GetNextDlgGroupItem(hDlg,
  94.                                                hGrpCurrent,
  95.                                                bDirCurrent);
  96.                hGrpCurrent = hGNextItem;
  97.                for (i = ID_GRPFRST; i <= ID_GRPTHRD; i = i + 25)
  98.                   if (GetDlgItem(hDlg, i) == hGrpCurrent)
  99.                      CheckRadioButton(hDlg, ID_GRPFRST,
  100.                                       ID_GRPTHRD, i);
  101.                return TRUE;
  102.  
  103.             default:
  104.                return FALSE;
  105.             }
  106.  
  107.       case WM_INITDIALOG: /* Initalize everything */
  108.          hTabCurrent = GetDlgItem(hDlg, ID_TABFRST);
  109.          CheckRadioButton(hDlg, ID_TABFRST, ID_TABTHRD, ID_TABFRST);
  110.          hGrpCurrent = GetDlgItem(hDlg, ID_GRPFRST);
  111.          CheckRadioButton(hDlg, ID_GRPFRST, ID_GRPTHRD, ID_GRPFRST);
  112.          bDirCurrent = FALSE;
  113.          CheckDlgButton(hDlg, ID_DIRECTION, bDirCurrent);
  114.          SetFocus(GetDlgItem(hDlg, ID_DONE));
  115.          return FALSE;
  116.  
  117.       default:
  118.          return FALSE;
  119.       }
  120. }
  121.  
  122. int PASCAL WinMain(hInstance, hPrevInstance, lpszCmdLine, cmdShow)
  123. HANDLE hInstance, hPrevInstance;
  124. LPSTR lpszCmdLine;
  125. int cmdShow;
  126. {
  127.     MSG   msg;
  128.     HWND  hWnd;
  129.  
  130.     if (!hPrevInstance) {
  131.        PWNDCLASS   pNextClass;
  132.  
  133.        pNextClass = (PWNDCLASS)LocalAlloc(LPTR, sizeof(WNDCLASS));
  134.  
  135.        pNextClass->hCursor        = LoadCursor(NULL, IDC_ARROW);
  136.        pNextClass->hIcon          = (HANDLE)NULL;
  137.        pNextClass->lpszMenuName   = (LPSTR)"NextMenu";
  138.        pNextClass->lpszClassName  = (LPSTR)"gnextitm";
  139.        pNextClass->hbrBackground  = (HBRUSH)GetStockObject(WHITE_BRUSH);
  140.        pNextClass->hInstance      = hInstance;
  141.        pNextClass->style          = CS_HREDRAW | CS_VREDRAW;
  142.        pNextClass->lpfnWndProc    = NextWndProc;
  143.  
  144.        if (!RegisterClass((LPWNDCLASS)pNextClass))
  145.            /* Initialization failed.
  146.             * Windows will automatically deallocate all allocated memory.
  147.             */
  148.            return FALSE;
  149.  
  150.        LocalFree((HANDLE)pNextClass);
  151.        }
  152.  
  153.     hWnd = CreateWindow((LPSTR)"gnextitm",
  154.                         (LPSTR)"GetNextDlgTabItem() & GetNextDlgGroupItem() Demo",
  155.                         WS_TILEDWINDOW,
  156.                         CW_USEDEFAULT,
  157.                         CW_USEDEFAULT,
  158.                         CW_USEDEFAULT,
  159.                         CW_USEDEFAULT,
  160.                         (HWND)NULL,        /* no parent                 */
  161.                         (HMENU)NULL,       /* use class menu            */
  162.                         (HANDLE)hInstance, /* handle to window instance */
  163.                         (LPSTR)NULL);      /* no params to pass on      */
  164.  
  165.     /* Save instance handle for DialogBox */
  166.     hInst = hInstance;
  167.  
  168.     /* Bind callback function with module instance */
  169.     lpprocNext = MakeProcInstance((FARPROC)NextItem, hInstance);
  170.  
  171.     /* Make window visible according to the way the app is activated */
  172.     ShowWindow(hWnd, cmdShow);
  173.     UpdateWindow(hWnd);
  174.  
  175.     /* Polling messages from event queue */
  176.     while (GetMessage((LPMSG)&msg, NULL, 0, 0)) {
  177.         TranslateMessage((LPMSG)&msg);
  178.         DispatchMessage((LPMSG)&msg);
  179.         }
  180.  
  181.     return (int)msg.wParam;
  182. }
  183.  
  184.  
  185. /* Procedures which make up the window class. */
  186. long FAR PASCAL NextWndProc(hWnd, message, wParam, lParam)
  187. HWND hWnd;
  188. unsigned message;
  189. WORD wParam;
  190. LONG lParam;
  191. {
  192.     switch (message)
  193.     {
  194.     case WM_COMMAND:
  195.         switch (wParam)
  196.         {
  197.         case ID_NEXTBOX: /* Go for it! */
  198.             DialogBox(hInst, MAKEINTRESOURCE(NEXTBOX), hWnd, lpprocNext);
  199.             break;
  200.  
  201.         default:
  202.             return DefWindowProc(hWnd, message, wParam, lParam);
  203.         }
  204.         break;
  205.  
  206.     case WM_DESTROY:
  207.         PostQuitMessage(0);
  208.         break;
  209.  
  210.     default:
  211.         return DefWindowProc(hWnd, message, wParam, lParam);
  212.         break;
  213.     }
  214.     return(0L);
  215. }
  216.