home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / win_lrn / class / cwinproc.c next >
Encoding:
C/C++ Source or Header  |  1988-08-11  |  5.1 KB  |  205 lines

  1. /*
  2.  *   This program demonstrates the use of the CallWindowProc () function.
  3.  *   CallWindowProc () passes message information contained in the 3rd
  4.  *   parameter to the window identified by the 2nd parameter. CallWindow-
  5.  *   Proc is used in subclassing. This is how it is used in this program.
  6.  *   No real subclass is performed in this program; it is just set up.
  7.  *   Messages destined for the listbox in the dialog box are intercepted
  8.  *   by SubClassProc () and then passed on to the default procedure for
  9.  *   listboxes. CallWindowProc () is called in SubClassProc () after the
  10.  *   subclass is performed.
  11.  */
  12.  
  13. #include "windows.h"
  14. #include "cwinproc.h"
  15.  
  16. BOOL FAR PASCAL DialogBoxProc (HWND, unsigned, WORD, LONG);
  17. long    FAR PASCAL HelloWndProc (HWND, unsigned, WORD, LONG);
  18. long    FAR PASCAL SubClassProc (HWND, unsigned, WORD, LONG);
  19.  
  20. HANDLE hInst;
  21.  
  22. FARPROC lpprocDlgProc;
  23. FARPROC lpprocSubClassProc;
  24. FARPROC lpprocListBox;
  25.  
  26. char    szLBOutput[50];
  27.  
  28. BOOL FAR PASCAL DialogBoxProc (hDlg, message, wParam, lParam)
  29. HWND hDlg;
  30. unsigned    message;
  31. WORD wParam;
  32. LONG lParam;
  33. {
  34.   HWND hCtl;
  35.   LONG lListBoxSel;
  36.   int    i;
  37.   char    buffer[15];
  38.  
  39.   switch (message)
  40.   {
  41.   case WM_INITDIALOG:
  42.     lpprocSubClassProc = MakeProcInstance ( (FARPROC) SubClassProc, hInst);
  43.  
  44.     lpprocListBox = (FARPROC) SetWindowLong (GetDlgItem (hDlg, IDLISTBOX),
  45.         GWL_WNDPROC,
  46.         (long)lpprocSubClassProc);
  47.  
  48.     hCtl = GetDlgItem (hDlg, IDLISTBOX);
  49.     i =  ID_FEET;
  50.     while (LoadString (hInst, i, (LPSTR)buffer, MAXSTRLEN) != 0)
  51.     {
  52.       SendMessage (hCtl, LB_ADDSTRING, 0, (LONG) (LPSTR)buffer);
  53.       i++;
  54.     }
  55.  
  56.     SetFocus (hCtl);
  57.     return (TRUE);
  58.  
  59.   case WM_COMMAND:
  60.     switch (wParam)
  61.     {
  62.     case IDLISTBOX:
  63.       if ( (HIWORD (lParam)) == 2)
  64.       {
  65.     hCtl = GetDlgItem (hDlg, IDLISTBOX);
  66.  
  67.     lListBoxSel = SendMessage (hCtl, LB_GETCURSEL, 0,
  68.         (LONG) (LPSTR)"");
  69.     SendMessage (hCtl, LB_GETTEXT, (WORD) lListBoxSel,
  70.         (LONG) (LPSTR)szLBOutput);
  71.     MessageBox (NULL, (LPSTR)szLBOutput, (LPSTR)"Chose from listbox:",
  72.         MB_OK);
  73.     return (TRUE);
  74.       }
  75.       else
  76.     return (FALSE);
  77.       break;
  78.  
  79.     case IDOK:
  80.       hCtl = GetDlgItem (hDlg, IDLISTBOX);
  81.       lListBoxSel = SendMessage (hCtl, LB_GETCURSEL, 0, (LONG) (LPSTR)"");
  82.       if (lListBoxSel == -1L)
  83.     i = 1;
  84.       else
  85.     SendMessage (hCtl, LB_GETTEXT, (WORD)lListBoxSel,
  86.         (LONG) (LPSTR)szLBOutput);
  87.       EndDialog (hDlg, TRUE);
  88.       return (TRUE);
  89.  
  90.     case IDCANCEL:
  91.       EndDialog (hDlg, TRUE);
  92.       return (TRUE);
  93.  
  94.     default:
  95.       return (FALSE);
  96.     }
  97.     break;
  98.  
  99.   default:
  100.     return (FALSE);
  101.     break;
  102.   }
  103. }
  104.  
  105.  
  106. long    FAR PASCAL SubClassProc (hWnd, message, wParam, lParam)
  107. HWND      hWnd;
  108. unsigned    message;
  109. WORD      wParam;
  110. LONG      lParam;
  111. {
  112.   CallWindowProc (lpprocListBox, hWnd, message, wParam, lParam);
  113. /*  Pass Info onto the default control procedure  */
  114.   return 0L;
  115. }
  116.  
  117.  
  118. /* Procedure called when the application is loaded for the first time */
  119. BOOL HelloInit (hInstance)
  120. HANDLE hInstance;
  121. {
  122.   PWNDCLASS   pHelloClass;
  123.  
  124.   pHelloClass = (PWNDCLASS)LocalAlloc (LPTR, sizeof (WNDCLASS));
  125.  
  126.   pHelloClass->hCursor        = LoadCursor (NULL, IDC_ARROW);
  127.   pHelloClass->hIcon          = LoadIcon (hInstance, NULL);
  128.   pHelloClass->lpszMenuName   = (LPSTR)"fonttest";
  129.   pHelloClass->lpszClassName  = (LPSTR)"CallWindowProc";
  130.   pHelloClass->hbrBackground  = (HBRUSH)GetStockObject (WHITE_BRUSH);
  131.   pHelloClass->hInstance      = hInstance;
  132.   pHelloClass->style          = CS_HREDRAW | CS_VREDRAW;
  133.   pHelloClass->lpfnWndProc    = HelloWndProc;
  134.  
  135.   if (!RegisterClass ( (LPWNDCLASS)pHelloClass))
  136.     return FALSE;
  137.  
  138.   LocalFree ( (HANDLE)pHelloClass);
  139.   return TRUE;        /* Initialization succeeded */
  140. }
  141.  
  142.  
  143. int    PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, cmdShow)
  144. HANDLE hInstance, hPrevInstance;
  145. LPSTR lpszCmdLine;
  146. int    cmdShow;
  147. {
  148.   MSG   msg;
  149.   HWND  hWnd;
  150.  
  151.   HelloInit (hInstance);
  152.   hWnd = CreateWindow ( (LPSTR)"CallWindowProc",
  153.       (LPSTR)"CallWindowProc ()",
  154.       WS_OVERLAPPEDWINDOW,
  155.       CW_USEDEFAULT,
  156.       CW_USEDEFAULT,
  157.       CW_USEDEFAULT,
  158.       CW_USEDEFAULT,
  159.       (HWND)NULL,        /* no parent */
  160.   (HMENU)NULL,       /* use class menu */
  161.   (HANDLE)hInstance, /* handle to window instance */
  162.   (LPSTR)NULL);      /* no params to pass on */
  163.  
  164.   hInst = hInstance;
  165.  
  166.   ShowWindow (hWnd, cmdShow);
  167.   UpdateWindow (hWnd);
  168.  
  169.   while (GetMessage ( (LPMSG) & msg, NULL, 0, 0))
  170.   {
  171.     TranslateMessage ( (LPMSG) & msg);
  172.     DispatchMessage ( (LPMSG) & msg);
  173.   }
  174.   return (int)msg.wParam;
  175. }
  176.  
  177.  
  178. /* Procedures which make up the window class. */
  179. long    FAR PASCAL HelloWndProc (hWnd, message, wParam, lParam)
  180. HWND hWnd;
  181. unsigned    message;
  182. WORD wParam;
  183. LONG lParam;
  184. {
  185.   switch (message)
  186.   {
  187.   case WM_COMMAND:
  188.     lpprocDlgProc = MakeProcInstance ( (FARPROC)DialogBoxProc, hInst);
  189.     DialogBox (hInst, MAKEINTRESOURCE (5), hWnd, lpprocDlgProc);
  190.     FreeProcInstance ( (FARPROC)lpprocDlgProc);
  191.     break;
  192.  
  193.   case WM_DESTROY:
  194.     PostQuitMessage (0);
  195.     break;
  196.  
  197.   default:
  198.     return DefWindowProc (hWnd, message, wParam, lParam);
  199.     break;
  200.   }
  201.   return (0L);
  202. }
  203.  
  204.  
  205.