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

  1. /*
  2.  *
  3.  * Function (s) demonstrated in this program: CallMsgFilter, SetWindowsHook,
  4.  *    UnhookWindowsHook
  5.  * Compiler version:  C 5.10
  6.  * Description:  This function calls the Message Filter with the given message.
  7.  *    The message filter is inserted in the Message Filter chain by use of
  8.  *    the SetWindowsHook, and removed using the UnhookWindowsHook.
  9.  *
  10.  */
  11.  
  12. #define NOMINMAX
  13. #include <windows.h>
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include "CallMsgF.h"
  17.  
  18. extern HWND   hWndMain;
  19.  
  20. HWND     hWndMain;
  21. HANDLE   hInstMain;
  22.  
  23. char    szOutputBuffer1 [70];
  24. char    szOutputBuffer2 [500];
  25.  
  26. /****************************************************************************/
  27. /************************    Message Structure      *************************/
  28. /****************************************************************************/
  29.  
  30. struct   {
  31.   char    *szMessage; 
  32.   } Messages [] =   {
  33. "About",
  34. "     This is a sample  application to  demonstrate\n\
  35. the use of the CallMsgFilter, SetWindowsHook, and\n\
  36. UnhookWindowsHook Windows functions.",
  37.  
  38. "Help Message",
  39. "     This program uses the CallMsgFilter Windows\n\
  40. function to send a special message to the main\n\
  41. window.  To test this function use the menu to\n\
  42. select the option to invoke the function.",
  43.  
  44. "CallMsgFilter",
  45. "     This message was passed to the main window\n\
  46. by the message filter."
  47.  
  48. };
  49.  
  50.  
  51. /****************************************************************************/
  52.  
  53. void ProcessMessage (HWND, int);
  54.  
  55. void ProcessMessage (hWnd, MessageNumber)
  56. HWND     hWnd;
  57. int    MessageNumber;
  58.   {
  59.   sprintf (szOutputBuffer1, "%s", Messages [MessageNumber]);
  60.   sprintf (szOutputBuffer2, "%s", Messages [MessageNumber + 1]);
  61.   MessageBox (hWnd, szOutputBuffer2, szOutputBuffer1, MB_OK);
  62.   }
  63.  
  64.  
  65. /****************************************************************************/
  66.  
  67. int     PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, nCmdShow)
  68. HANDLE      hInstance, hPrevInstance;
  69. LPSTR       lpszCmdLine;
  70. int    nCmdShow;
  71.   {
  72.   static char    szAppName [] = "CallMsgF";
  73.   HWND        hWnd;
  74.   WNDCLASS    wndclass;
  75.   MSG msg;
  76.   short xScreen, yScreen;
  77.  
  78.   if (!hPrevInstance)
  79.     {
  80.     wndclass.style         = CS_HREDRAW | CS_VREDRAW;
  81.     wndclass.lpfnWndProc   = WndProc;
  82.     wndclass.cbClsExtra    = 0;
  83.     wndclass.cbWndExtra    = 0;
  84.     wndclass.hInstance     = hInstance;
  85.     wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION);
  86.     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW);
  87.     wndclass.hbrBackground = GetStockObject (WHITE_BRUSH);
  88.     wndclass.lpszMenuName  = szAppName;
  89.     wndclass.lpszClassName = szAppName;
  90.  
  91.     if (!RegisterClass (&wndclass))
  92.       return FALSE;
  93.     }
  94.  
  95.     xScreen = GetSystemMetrics (SM_CXSCREEN);
  96.     yScreen = GetSystemMetrics (SM_CYSCREEN);
  97.  
  98.     hWndMain = CreateWindow (szAppName,       /* window class name       */
  99.                "CallMsgFilter",               /* window caption          */
  100.                 WS_OVERLAPPEDWINDOW,          /* window style            */
  101.                 CW_USEDEFAULT,                /* initial x position      */
  102.                 0,                            /* initial y position      */
  103.                 CW_USEDEFAULT,                /* initial x size          */
  104.                 0,                            /* initial y size          */
  105.                 NULL,                         /* parent window handle    */
  106.                 NULL,                         /* window menu handle      */
  107.                 hInstance,                    /* program instance handle */
  108.                 NULL);                        /* create parameters       */
  109.  
  110.     ShowWindow (hWndMain, nCmdShow);
  111.     UpdateWindow (hWndMain);
  112.  
  113.     hInstMain = hInstance;
  114.   
  115.     InitHook (hInstMain);
  116.  
  117.     while (GetMessage (&msg, NULL, 0, 0))
  118.     {
  119.     TranslateMessage (&msg);
  120.     DispatchMessage (&msg);
  121.     }
  122.  
  123.     KillHook ();
  124.  
  125.     return (msg.wParam);
  126. }
  127.  
  128.  
  129. /****************************************************************************/
  130.  
  131. long    FAR PASCAL WndProc (hWnd, iMessage, wParam, lParam)
  132. HWND     hWnd;
  133. unsigned    iMessage;
  134. WORD     wParam;
  135. LONG     lParam;
  136.   {
  137.   HMENU       hMenu;
  138.   MSG         msg;
  139.  
  140.   switch (iMessage)
  141.     {
  142.     case WM_CREATE:
  143.       hMenu = GetSystemMenu (hWnd, FALSE);
  144.       ChangeMenu (hMenu, NULL, "&About", IDM_ABOUT, MF_APPEND | MF_STRING);
  145.       break;
  146.  
  147.     case WM_SYSCOMMAND:
  148.       switch (wParam)
  149.         {
  150.         case IDM_ABOUT:
  151.           ProcessMessage (hWnd, 0);
  152.           break;
  153.         default:
  154.           return DefWindowProc (hWnd, iMessage, wParam, lParam);
  155.         }
  156.       break;
  157.  
  158.     case WM_COMMAND:
  159.       switch (wParam)
  160.         {
  161.         case IDM_CALLMSGFILTER:
  162.           MessageBox (hWnd, "Calling the Message Filter.",
  163.                       "CallMsgFilter", MB_OK);
  164.           msg.hwnd    = hWnd;
  165.           msg.message = WM_COMMAND;
  166.           msg.wParam  = IDM_FILTERMSG;
  167.           msg.lParam  = 0L;
  168.  
  169.           CallMsgFilter (&msg, HC_ACTION);
  170.           break;
  171.  
  172.         case IDM_FROMMSGFILTER:
  173.           ProcessMessage (hWnd, 4);
  174.           break;
  175.  
  176.         case IDM_HELP:
  177.           ProcessMessage (hWnd, 2);
  178.           break;
  179.         }
  180.       break;
  181.  
  182.     case WM_DESTROY:
  183.       PostQuitMessage (0);
  184.       break;
  185.  
  186.     default:
  187.       return DefWindowProc (hWnd, iMessage, wParam, lParam);
  188.     }
  189.   return (0L);
  190.   }
  191.