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

  1. /*
  2.  *
  3.  *  FindWindow
  4.  *
  5.  *  findwin.c, charlesl, v2.00, 30-Dec-1987
  6.  *  
  7.  *  This program demonstrates the use of FindWindow function.
  8.  *  This function the handle of the window whose class is given
  9.  *  by it's frist parameter and whose window name (caption) is 
  10.  *  given by is second parameter.
  11.  *  
  12.  *  Microsoft Product Support Services
  13.  *  Windows Version 2.0 function demonstration application
  14.  *  Copyright (c) Microsoft 1987
  15.  *
  16.  */
  17.  
  18. /*************************************************************************/
  19. /*                           INCLUDE FILES                               */
  20. /*************************************************************************/
  21.  
  22. #include <windows.h>
  23.  
  24. /*************************************************************************/
  25. /*                        STRUCTURE DEFINTIONS                           */
  26. /*************************************************************************/
  27.  
  28. typedef struct
  29. {
  30.    int nDummy;
  31. }
  32. SETUPDATA;
  33.  
  34. /*************************************************************************/
  35. /*                         GLOBAL VARIABLES                              */
  36. /*************************************************************************/
  37.  
  38. static SETUPDATA strSetUpData;
  39. static HANDLE hInst;
  40. static char szFileName[] = "findwin";
  41. static char szFuncName[] = "FindWindow";
  42.  
  43. /*************************************************************************/
  44. /*                       FORWARD REFERENCES                              */
  45. /*************************************************************************/
  46.  
  47. long FAR PASCAL WindowProc ( HANDLE , unsigned , WORD , LONG );
  48.  
  49. /*************************************************************************/
  50. /*                         MAIN PROCEDURE                                */
  51. /*************************************************************************/
  52.  
  53. int PASCAL WinMain( hInstance, hPrevInstance, lpszCmdLine, cmdShow )
  54. HANDLE hInstance, hPrevInstance;
  55. LPSTR  lpszCmdLine;
  56. int    cmdShow;
  57. {
  58.   MSG  msg;
  59.  
  60.   WindowInit (hInstance, hPrevInstance, cmdShow );
  61.  
  62.   while ( GetMessage((LPMSG)&msg, NULL, 0 , 0 ))
  63.     {
  64.     TranslateMessage((LPMSG)&msg);
  65.     DispatchMessage((LPMSG)&msg);
  66.     }
  67.   exit(msg.wParam);
  68. }
  69.  
  70. /*************************************************************************/
  71. /*                      INITIALIZATION                                   */
  72. /*************************************************************************/
  73.  
  74. BOOL WindowInit (hInstance , hPrevInstance , cmdShow)
  75. HANDLE hInstance, hPrevInstance;
  76. int cmdShow;
  77. {
  78.   HWND  hWnd;
  79.  
  80.   if ( !hPrevInstance )
  81.      {
  82.      WNDCLASS rClass;
  83.  
  84.      rClass.style         = CS_HREDRAW | CS_VREDRAW;
  85.      rClass.lpfnWndProc   = WindowProc;
  86.      rClass.cbClsExtra    = 0;
  87.      rClass.cbWndExtra    = 0;
  88.      rClass.hInstance     = hInstance;
  89.      rClass.hCursor       = LoadCursor ( NULL , IDC_ARROW );
  90.      rClass.hIcon         = LoadIcon ( hInstance, IDI_APPLICATION );
  91.      rClass.hbrBackground = GetStockObject ( WHITE_BRUSH );
  92.      rClass.lpszMenuName  = (LPSTR) NULL;
  93.      rClass.lpszClassName = (LPSTR) szFileName;
  94.    
  95.      RegisterClass ( ( LPWNDCLASS ) &rClass );
  96.      }
  97.   else
  98.      GetInstanceData ( hPrevInstance, (PSTR) &strSetUpData,
  99.         sizeof ( SETUPDATA ) );
  100.  
  101.   hInst = hInstance;
  102.  
  103.   hWnd = CreateWindow ( (LPSTR) szFileName, (LPSTR) szFuncName,
  104.                       WS_OVERLAPPEDWINDOW,
  105.                       CW_USEDEFAULT, CW_USEDEFAULT,
  106.                       CW_USEDEFAULT, CW_USEDEFAULT,
  107.                       (HWND) NULL, (HMENU) NULL,
  108.                       (HANDLE) hInstance, (LPSTR) NULL );
  109.  
  110.   ShowWindow ( hWnd , cmdShow );
  111.   UpdateWindow (hWnd);
  112.  
  113.   return TRUE;
  114. }
  115.  
  116. /*************************************************************************/
  117. /*                 WINDOW PROCEDURE - PROCESS MESSAGES                   */
  118. /*************************************************************************/
  119.  
  120. long FAR PASCAL WindowProc (hWnd , message , wParam , lParam)
  121. HWND        hWnd;
  122. unsigned    message;
  123. WORD        wParam;
  124. LONG        lParam;
  125. {
  126.     PAINTSTRUCT ps;
  127.  
  128.     switch (message)
  129.     {
  130.  
  131.     case WM_PAINT:
  132.         BeginPaint ( hWnd, (LPPAINTSTRUCT)&ps );
  133.         FunctionDemonstrated ( hWnd, (PAINTSTRUCT *)&ps );
  134.         EndPaint ( hWnd, (LPPAINTSTRUCT)&ps );
  135.         break;
  136.  
  137.     case WM_DESTROY:
  138.         PostQuitMessage ( 0 );
  139.         break;
  140.  
  141.     default:
  142.         return ( DefWindowProc ( hWnd , message , wParam , lParam ) );
  143.         break;
  144.     }
  145.   return ( 0L );
  146. }
  147.  
  148. /*************************************************************************/
  149. /*              FUNCTION DEMONSTRATED HERE - LOOK HERE                   */
  150. /*************************************************************************/
  151.  
  152. FunctionDemonstrated ( hWnd )
  153. HWND hWnd;
  154. {
  155.     HWND hFindWnd;
  156.  
  157.     MessageBox (NULL, (LPSTR)"Searching for Window", (LPSTR)szFuncName,
  158.        MB_OK);
  159.  
  160.     hFindWnd = FindWindow ( ( LPSTR ) "findwin", ( LPSTR ) "FindWindow" );
  161.   
  162.     if ( hFindWnd != NULL )
  163.        MessageBox (NULL, (LPSTR)"Window Found", (LPSTR)szFuncName, MB_OK);
  164.     else
  165.        MessageBox (NULL, (LPSTR)"Window not Found", (LPSTR)szFuncName, MB_OK);
  166.  
  167.     return TRUE;
  168. }
  169.