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

  1. /*
  2.  *   GetWindowWord
  3.  *
  4.  *   This program demonstrates the use of the GetWindowWord function.
  5.  *   GetWindowWord returns information about the window identified by
  6.  *   the first parameter. The information it returns is specified by the
  7.  *   second parameter. In this sample application, GetWindowWord was
  8.  *   called in WinMain and passed "hWnd" and "GWW_HINSTANCE," telling it
  9.  *   to return the instance handle of the module owning "hWnd."
  10.  *
  11.  *   Windows Version 2.0 function demonstration application
  12.  *
  13.  */
  14.  
  15. #include "windows.h"
  16.  
  17. long FAR PASCAL HelloWndProc(HWND, unsigned, WORD, LONG);
  18.  
  19. /* Procedure called when the application is loaded for the first time */
  20. BOOL HelloInit( hInstance )
  21. HANDLE hInstance;
  22. {
  23.     PWNDCLASS   pHelloClass;
  24.  
  25.     pHelloClass = (PWNDCLASS)LocalAlloc( LPTR, sizeof(WNDCLASS) );
  26.  
  27.     pHelloClass->hCursor        = LoadCursor( NULL, IDC_ARROW );
  28.     pHelloClass->hIcon        = LoadIcon( hInstance,NULL);
  29.     pHelloClass->lpszMenuName   = (LPSTR)NULL;
  30.     pHelloClass->lpszClassName    = (LPSTR)"Sample Application";
  31.     pHelloClass->hbrBackground  = (HBRUSH)GetStockObject( WHITE_BRUSH );
  32.     pHelloClass->hInstance      = hInstance;
  33.     pHelloClass->style          = CS_HREDRAW | CS_VREDRAW;
  34.     pHelloClass->lpfnWndProc    = HelloWndProc;
  35.  
  36.     if (!RegisterClass( (LPWNDCLASS)pHelloClass ) )
  37.         /* Initialization failed.
  38.          * Windows will automatically deallocate all allocated memory.
  39.          */
  40.         return FALSE;
  41.  
  42.     LocalFree( (HANDLE)pHelloClass );
  43.     return TRUE;        /* Initialization succeeded */
  44. }
  45.  
  46. int PASCAL WinMain( hInstance, hPrevInstance, lpszCmdLine, cmdShow )
  47. HANDLE hInstance, hPrevInstance;
  48. LPSTR lpszCmdLine;
  49. int cmdShow;
  50. {
  51.     MSG   msg;
  52.     HWND  hWnd;
  53.     HMENU hMenu;
  54.     HDC hDC;          /* handle to display context */
  55.     HANDLE ourhInstance;  /* return value from GetWindowWord */
  56.  
  57.     HelloInit( hInstance );
  58.     hWnd = CreateWindow((LPSTR)"Sample Application",
  59.             (LPSTR)"GetWindowWord",
  60.             WS_OVERLAPPEDWINDOW,
  61.             CW_USEDEFAULT,
  62.             CW_USEDEFAULT,
  63.             CW_USEDEFAULT,
  64.             CW_USEDEFAULT,
  65.                         (HWND)NULL,        /* no parent */
  66.                         (HMENU)NULL,       /* use class menu */
  67.                         (HANDLE)hInstance, /* handle to window instance */
  68.                         (LPSTR)NULL        /* no params to pass on */
  69.                         );
  70.  
  71.     /* Make window visible according to the way the app is activated */
  72.     ShowWindow( hWnd, cmdShow );
  73.     UpdateWindow( hWnd );
  74.  
  75.     hDC = GetDC(hWnd);      /** get a display context for hWnd **/
  76.  
  77.     TextOut(hDC,
  78.         20,
  79.         5,
  80.   (LPSTR)"I will now call GetWindowWord and pass it 'hWnd' and 'GWW_HINSTANCE,'",
  81.   (short)68);
  82.  
  83.     TextOut(hDC,
  84.         20,
  85.         15,
  86. (LPSTR)"telling it to retrieve the instance handle of the module owning the",
  87.   (short)67);
  88.  
  89.     TextOut(hDC,
  90.         20,
  91.         25,
  92.   (LPSTR)"window associated with hWnd. GetWindowWord should return 'hInstance.'",
  93.   (short)69);
  94.  
  95.   MessageBox(hWnd,(LPSTR)"  OKAY",(LPSTR)" ",MB_OK);
  96.  
  97. /**   call GetWindowWord, tell it to retrieve the instance handle  **/
  98. /**   of the module owning the window associated with hWnd       **/
  99.   ourhInstance = GetWindowWord(hWnd,GWW_HINSTANCE);
  100.  
  101. /**   clear screen **/
  102.   InvalidateRect(hWnd,(LPRECT)NULL,(BOOL)TRUE);
  103.   UpdateWindow(hWnd);
  104.  
  105. /**  if GetWindowWord correctly returned the instance handle **/
  106. /**  of the  module owning the window associated with hWnd.. **/
  107.   if (ourhInstance = hInstance)
  108.       MessageBox(hWnd,
  109.         (LPSTR)"instance handle of the module owning hWnd.",
  110.         (LPSTR)"GetWindowWord correctly retrieved the...",
  111.         MB_OK);
  112.     else
  113.       MessageBox(hWnd,
  114.         (LPSTR)"instance handle of the module owning hWnd.",
  115.         (LPSTR)"GetWindowWord DID NOT correctly retrieve the...",
  116.         MB_OK);
  117.  
  118.     /* Polling messages from event queue */
  119.     while (GetMessage((LPMSG)&msg, NULL, 0, 0)) {
  120.         TranslateMessage((LPMSG)&msg);
  121.         DispatchMessage((LPMSG)&msg);
  122.         }
  123.  
  124.     return (int)msg.wParam;
  125. }
  126.  
  127.  
  128. /* Procedures which make up the window class. */
  129. long FAR PASCAL HelloWndProc( hWnd, message, wParam, lParam )
  130. HWND hWnd;
  131. unsigned message;
  132. WORD wParam;
  133. LONG lParam;
  134. {
  135.     PAINTSTRUCT ps;
  136.  
  137.     switch (message)
  138.     {
  139.     case WM_SYSCOMMAND:
  140.     return DefWindowProc( hWnd, message, wParam, lParam );
  141.         break;
  142.  
  143.     case WM_DESTROY:
  144.         PostQuitMessage( 0 );
  145.         break;
  146.  
  147.     case WM_PAINT:
  148.         BeginPaint( hWnd, (LPPAINTSTRUCT)&ps );
  149.         EndPaint( hWnd, (LPPAINTSTRUCT)&ps );
  150.         break;
  151.  
  152.     default:
  153.         return DefWindowProc( hWnd, message, wParam, lParam );
  154.         break;
  155.     }
  156.     return(0L);
  157. }
  158.