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

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