home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / win_lrn / window / findwind.c < prev    next >
C/C++ Source or Header  |  1988-08-10  |  5KB  |  168 lines

  1. /*
  2.  *
  3.  * Function (s) demonstrated in this program: FindWindow
  4.  * Compiler version:  C 5.10
  5.  *
  6.  * Description:  This function returns a handle to the Window whose caption
  7.  *    matches the caption parameter.  If no match is found a NULL handle is
  8.  *         returned.  The program will tell the MS-DOS executive to minimize
  9.  *         itself.
  10.  */
  11.  
  12. #define NOMINMAX
  13. #include <windows.h>
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include "FindWind.h"
  17.  
  18. void ProcessMessage (HWND, int);
  19.  
  20. HWND     hWndParent1;
  21. HANDLE   hInstMain;
  22.  
  23. char    szOutputBuffer1 [70];
  24. char    szOutputBuffer2 [500];
  25.  
  26. struct   {
  27.   char    *szMessage; 
  28.   } Messages [] =   {
  29.   "About\0",
  30.   "     This is a sample application to demonstrate the\n\
  31. use of the FindWindow Windows function.",
  32.  
  33.   "Help Message",
  34.   "     This program uses the FindWindow Windows function\n\
  35. to get a handle to the MS-DOS Executive.  This handle\n\
  36. is then displayed in a message box.  The MS-DOS\n\
  37. Executive is then Iconized.  Use the menu to invoke\n\
  38. this function.",
  39.  
  40.   };
  41.  
  42.  
  43. /****************************************************************************/
  44.  
  45. void ProcessMessage (hWnd, MessageNumber)
  46. HWND     hWnd;
  47. int    MessageNumber;
  48.   {
  49.   sprintf (szOutputBuffer1, "%s", Messages [MessageNumber]);
  50.   sprintf (szOutputBuffer2, "%s", Messages [MessageNumber + 1]);
  51.   MessageBox (hWnd, szOutputBuffer2, szOutputBuffer1, MB_OK);
  52.   }
  53.  
  54.  
  55. /****************************************************************************/
  56.  
  57. int     PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, nCmdShow)
  58. HANDLE   hInstance, hPrevInstance;
  59. LPSTR    lpszCmdLine;
  60. int      nCmdShow;
  61.   {
  62.   static char   szAppName [] = "FindWind";
  63.   HWND        hWnd;
  64.   WNDCLASS    wndclass;
  65.   MSG msg;
  66.   short xScreen, yScreen;
  67.  
  68.   if (!hPrevInstance)
  69.     {
  70.     wndclass.style         = CS_HREDRAW | CS_VREDRAW;
  71.     wndclass.lpfnWndProc   = WndProc;
  72.     wndclass.cbClsExtra    = 0;
  73.     wndclass.cbWndExtra    = 0;
  74.     wndclass.hInstance     = hInstance;
  75.     wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION);
  76.     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW);
  77.     wndclass.hbrBackground = GetStockObject (WHITE_BRUSH);
  78.     wndclass.lpszMenuName  = szAppName;
  79.     wndclass.lpszClassName = szAppName;
  80.  
  81.     if (!RegisterClass (&wndclass))
  82.       return FALSE;
  83.     }
  84.  
  85.   xScreen = GetSystemMetrics (SM_CXSCREEN);
  86.   yScreen = GetSystemMetrics (SM_CYSCREEN);
  87.  
  88.   hWndParent1 = CreateWindow (szAppName,          /* window class name       */
  89.                              "FindWindow",        /* window caption          */
  90.                              WS_OVERLAPPEDWINDOW, /* window style            */
  91.                              CW_USEDEFAULT,       /* initial x position      */
  92.                              0,                   /* initial y position      */
  93.                              CW_USEDEFAULT,       /* initial x size          */
  94.                              0,                   /* initial y size          */
  95.                              NULL,                /* parent window handle    */
  96.                              NULL,                /* window menu handle      */
  97.                              hInstance,           /* program instance handle */
  98.                              NULL);               /* create parameters       */
  99.  
  100.   ShowWindow (hWndParent1, nCmdShow);
  101.   UpdateWindow (hWndParent1);
  102.  
  103.   hInstMain = hInstance;
  104.  
  105.   while (GetMessage (&msg, NULL, 0, 0))
  106.     {
  107.     TranslateMessage (&msg);
  108.     DispatchMessage (&msg);
  109.     }
  110.   return (msg.wParam);
  111.   }
  112.  
  113. /****************************************************************************/
  114. long    FAR PASCAL WndProc (hWnd, iMessage, wParam, lParam)
  115. HWND      hWnd;
  116. unsigned  iMessage;
  117. WORD      wParam;
  118. LONG      lParam;
  119.   {
  120.   HMENU       hMenu;
  121.   HWND        hExecutive;
  122.   PAINTSTRUCT ps;
  123.   static int    xClient, yClient;
  124.  
  125.   switch (iMessage)
  126.     {
  127.     case WM_CREATE:
  128.       hMenu = GetSystemMenu (hWnd, FALSE);
  129.  
  130.       ChangeMenu (hMenu, NULL, "&About", IDM_ABOUT, MF_APPEND | MF_STRING);
  131.       break;
  132.  
  133.     case WM_SYSCOMMAND:
  134.       switch (wParam)
  135.         {
  136.         case IDM_ABOUT:
  137.           ProcessMessage (hWnd, 0);
  138.           break;
  139.         default:
  140.           return DefWindowProc (hWnd, iMessage, wParam, lParam);
  141.         }
  142.       break;
  143.  
  144.     case WM_COMMAND:
  145.       switch (wParam)
  146.         {
  147.         case IDM_FINDWINDOW:
  148.           hExecutive = FindWindow (NULL, "MS-DOS Executive");
  149.           if (!IsIconic (hExecutive))
  150.             ShowWindow (hExecutive, SW_SHOWMINIMIZED);
  151.           break;
  152.  
  153.         case IDM_HELP:
  154.           ProcessMessage (hWnd, 2);
  155.           break;
  156.         }
  157.       break;
  158.  
  159.     case WM_DESTROY:
  160.       PostQuitMessage (0);
  161.       break;
  162.  
  163.     default:
  164.       return DefWindowProc (hWnd, iMessage, wParam, lParam);
  165.     }
  166.   return (0L);
  167.   }
  168.