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

  1. /*
  2.  *   SetWindowWord
  3.  *   setwnwrd.c
  4.  *
  5.  *   This program demonstrates the use of the function SetWindowWord.
  6.  *   SetWindowWord changes an attribute of a window.  The attribute
  7.  *   can be one of the following options: the instance handle of the
  8.  *   module that owns the window, the window handle of the parent window
  9.  *   if one exists, the control ID of the child window.
  10.  *
  11.  */
  12.  
  13. #include "windows.h"
  14.  
  15. /* Forward references */
  16.  
  17. BOOL FAR PASCAL SampleInit( HANDLE );
  18.  
  19. long FAR PASCAL SampleWndProc(HWND, unsigned, WORD, LONG);
  20.  
  21. /* ---------------------------------------------------------------------- */
  22.  
  23. int PASCAL WinMain( hInstance, hPrevInstance, lpszCmdLine, CmdShow )
  24. HANDLE hInstance, hPrevInstance;
  25. LPSTR lpszCmdLine;
  26. int CmdShow;
  27. {
  28.     MSG   msg;
  29.     HWND  hParent1, hParent2, hPopup;
  30.     HMENU hMenu;
  31.  
  32.     SampleInit( hInstance );
  33.  
  34. /* Create the first parent window */
  35.  
  36.     hParent1 = CreateWindow((LPSTR)"SetWindowWord",
  37.             (LPSTR)"SetWindowWord - Parent1",
  38.             WS_OVERLAPPEDWINDOW,
  39.             CW_USEDEFAULT,
  40.             CW_USEDEFAULT,
  41.             400,
  42.            20,
  43.          (HWND)NULL,        /* no parent */
  44.          (HMENU)NULL,       /* use class menu */
  45.          (HANDLE)hInstance, /* handle to window instance */
  46.          (LPSTR)NULL        /* no params to pass on */
  47.          );
  48.  
  49.     /* Make window visible according to the way the app is activated */
  50.     ShowWindow( hParent1, CmdShow );
  51.     UpdateWindow( hParent1 );
  52.  
  53.     MessageBox (hParent1, (LPSTR)"create a popup window for parent1",
  54.                (LPSTR)"I'm about to ...", MB_OK);
  55.  
  56. /* Create the popup window */
  57.  
  58.     hPopup = CreateWindow((LPSTR)"SetWindowWord",
  59.                            (LPSTR)"Popup",
  60.                           WS_POPUP|WS_CAPTION|WS_VISIBLE,
  61.                             10,
  62.                             50,
  63.                             200,
  64.                             100,
  65.                          (HWND)hParent1,     /* 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.  
  72.     MessageBox (hParent1, (LPSTR)"create a second window called parent2",
  73.                (LPSTR)"I'm about to ...", MB_OK);
  74.  
  75. /* Create the second parent window */
  76.  
  77.     hParent2 = CreateWindow((LPSTR)"SetWindowWord",
  78.             (LPSTR)"SetWindowWord - Parent2",
  79.             WS_OVERLAPPEDWINDOW,
  80.             100,
  81.             100,
  82.             400,
  83.             20,
  84.          (HWND)NULL,        /* no parent */
  85.          (HMENU)NULL,       /* use class menu */
  86.          (HANDLE)hInstance, /* handle to window instance */
  87.          (LPSTR)NULL        /* no params to pass on */
  88.          );
  89.  
  90.     /* Make window visible according to the way the app is activated */
  91.     ShowWindow( hParent2, CmdShow );
  92.     UpdateWindow( hParent2 );
  93.  
  94. /****************************************************************************/
  95. /* Begin SetWindowWord */
  96. /* I have set up this function to change the parent of the popup window */
  97.  
  98.     MessageBox (hParent1, (LPSTR)"make the Parent2 the parent of Popup, and \
  99. iconize Parent1.  Notice that Parent2 is currently in front of Popup .",
  100.                (LPSTR)"I'm about to ...", MB_OK);
  101.  
  102.     SetWindowWord(hPopup, GWW_HWNDPARENT, hParent2);
  103.     ShowWindow( hParent1, SW_SHOWMINIMIZED );
  104.  
  105.     MessageBox (hParent1, (LPSTR)"move Parent2 in front of the Popup.  \
  106. You can't do it because Parent2 is now the parent of Popup!",
  107.                (LPSTR)"Now try to ...", MB_OK);
  108.  
  109. /* End of SetWindowWord section */
  110. /****************************************************************************/
  111.  
  112.     /* Polling messages from event queue */
  113.     while (GetMessage((LPMSG)&msg, NULL, 0, 0)) {
  114.         TranslateMessage((LPMSG)&msg);
  115.         DispatchMessage((LPMSG)&msg);
  116.         }
  117.  
  118.     return (int)msg.wParam;
  119. }
  120.  
  121. /* ---------------------------------------------------------------------- */
  122. /* Procedure called when the application is loaded for the first time */
  123.  
  124. BOOL FAR PASCAL SampleInit( hInstance )
  125. HANDLE hInstance;
  126. {
  127.     PWNDCLASS   pSampleClass;
  128.  
  129.     pSampleClass = (PWNDCLASS)LocalAlloc( LPTR, sizeof(WNDCLASS) );
  130.  
  131.     pSampleClass->hCursor        = LoadCursor( NULL, IDC_ARROW );
  132.     pSampleClass->hIcon              = LoadIcon( hInstance,NULL);
  133.     pSampleClass->lpszMenuName   = (LPSTR)NULL;
  134.     pSampleClass->lpszClassName  = (LPSTR)"SetWindowWord";
  135.     pSampleClass->hbrBackground  = (HBRUSH)GetStockObject( WHITE_BRUSH );
  136.     pSampleClass->hInstance      = hInstance;
  137.     pSampleClass->style          = CS_HREDRAW | CS_VREDRAW;
  138.     pSampleClass->lpfnWndProc    = SampleWndProc;
  139.  
  140.     if (!RegisterClass( (LPWNDCLASS)pSampleClass ) )
  141.         /* Initialization failed.
  142.          * Windows will automatically deallocate all allocated memory.
  143.          */
  144.         return FALSE;
  145.  
  146.     LocalFree( (HANDLE)pSampleClass );
  147.     return TRUE;        /* Initialization succeeded */
  148. }
  149.  
  150.  
  151. /* ---------------------------------------------------------------------- */
  152. /* Every message for this window will be delevered right here.         */
  153.  
  154. long FAR PASCAL SampleWndProc( hWnd, message, wParam, lParam )
  155. HWND hWnd;
  156. unsigned message;
  157. WORD wParam;
  158. LONG lParam;
  159. {
  160.     PAINTSTRUCT ps;
  161.  
  162.     switch (message)
  163.     {
  164.     case WM_DESTROY:
  165.         PostQuitMessage( 0 );
  166.         break;
  167.  
  168. /*    case WM_PAINT:
  169.         BeginPaint( hWnd, (LPPAINTSTRUCT)&ps );
  170.         EndPaint( hWnd, (LPPAINTSTRUCT)&ps );
  171.         break; */
  172.  
  173.     default:
  174.         return DefWindowProc( hWnd, message, wParam, lParam );
  175.         break;
  176.     }
  177.     return(0L);
  178. }
  179.