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

  1. /*
  2.  
  3. Function(s) demonstrated in this program: SetParent
  4.  
  5. Description:  This function changes the parent of the respective child 
  6.     window.
  7.  
  8. */
  9.  
  10. #define NOMINMAX
  11. #include <windows.h>
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include "SetPar.h"
  15.  
  16.  
  17. HWND     hWndParent1, hWndParent2, hWndChild1, hWndChild2;
  18. HANDLE   hInstMain;
  19.  
  20. char     szOutputBuffer1 [70];
  21. char     szOutputBuffer2 [500];
  22.  
  23.  
  24. /****************************************************************************/
  25. /************************    Message Structure      *************************/
  26. /****************************************************************************/
  27.  
  28. struct { char *szMessage; }
  29.        Messages [] = {
  30. "About\0",
  31. "     This is a sample application to demonstrate the\n\
  32. use  of  the  SetParent Windows function.",
  33.  
  34. "Help Message",
  35. "     This program uses the SetParent Windows\n\
  36. function to change the parent of the child windows.\n\
  37. Use the menu to choose the parent."
  38.  
  39. };    
  40.  
  41. /****************************************************************************/
  42.  
  43. void ProcessMessage (HWND, int); 
  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. int PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, nCmdShow)
  57.      HANDLE      hInstance, hPrevInstance ;
  58.      LPSTR       lpszCmdLine ;
  59.      int         nCmdShow ;
  60.      {
  61.      static char szAppName [] = "SetPar" ;
  62.      static char szChildClass [] = "SetParChild" ;
  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.           wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  85.           wndclass.lpfnWndProc   = ChildWndProc ;
  86.           wndclass.cbClsExtra    = 0 ;
  87.           wndclass.cbWndExtra    = 0 ;
  88.           wndclass.hInstance     = hInstance ;
  89.           wndclass.hIcon         = NULL ;
  90.           wndclass.hCursor       = LoadCursor (NULL, IDC_CROSS) ;
  91.           wndclass.hbrBackground = GetStockObject (LTGRAY_BRUSH) ;
  92.           wndclass.lpszMenuName  = NULL ;
  93.           wndclass.lpszClassName = szChildClass ;
  94.  
  95.           if (!RegisterClass (&wndclass))
  96.                return FALSE ;
  97.  
  98.           }
  99.  
  100.      xScreen = GetSystemMetrics (SM_CXSCREEN) ;
  101.      yScreen = GetSystemMetrics (SM_CYSCREEN) ;
  102.  
  103.      hWndParent1 = CreateWindow (szAppName,     /* window class name       */
  104.                     "Parent 1",                 /* window caption          */
  105.                     WS_OVERLAPPEDWINDOW,        /* window style            */
  106.                     xScreen / 9,                /* initial x position      */
  107.                     yScreen / 7,                /* initial y position      */
  108.                     xScreen * 3 / 9,            /* initial x size          */
  109.                     yScreen * 6 / 9,            /* initial y size          */
  110.                     NULL,                       /* parent window handle    */
  111.                     NULL,                       /* window menu handle      */
  112.                     hInstance,                  /* program instance handle */
  113.                     NULL) ;                     /* create parameters       */
  114.  
  115.      ShowWindow (hWndParent1, nCmdShow) ;
  116.      UpdateWindow (hWndParent1) ;
  117.  
  118.      hWndParent2 = CreateWindow (szAppName,     /* window class name       */
  119.                     "Parent 2",                 /* window caption          */
  120.                     WS_OVERLAPPEDWINDOW,        /* window style            */
  121.                     xScreen * 5 / 9,            /* initial x position      */
  122.                     yScreen / 7,                /* initial y position      */
  123.                     xScreen * 3 / 9,            /* initial x size          */
  124.                     yScreen * 6 / 9,            /* initial y size          */
  125.                     NULL,                       /* parent window handle    */
  126.                     NULL,                       /* window menu handle      */
  127.                     hInstance,                  /* program instance handle */
  128.                     NULL) ;                     /* create parameters       */
  129.  
  130.      ShowWindow (hWndParent2, nCmdShow) ;
  131.      UpdateWindow (hWndParent2) ;
  132.  
  133.      hInstMain = hInstance;
  134.  
  135.      hWndChild1 = CreateWindow (szChildClass, "Child 1", WS_CHILD | 
  136.                     WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MAXIMIZEBOX |
  137.                     WS_CLIPSIBLINGS,    
  138.                     0, 0, xScreen / 8, yScreen / 6,
  139.                     hWndParent1, 1, hInstance, NULL) ;     
  140.  
  141.      ShowWindow (hWndChild1, SW_SHOWNORMAL) ;
  142.      UpdateWindow (hWndChild1) ;
  143.  
  144.      hWndChild2 = CreateWindow (szChildClass, "Child 2", WS_CHILD | 
  145.                     WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MAXIMIZEBOX |
  146.                     WS_CLIPSIBLINGS,    
  147.                     0, yScreen / 5, xScreen / 8, yScreen / 6,    
  148.                     hWndParent1, 1, hInstance, NULL) ;         
  149.  
  150.      ShowWindow (hWndChild2, SW_SHOWNORMAL) ;
  151.      UpdateWindow (hWndChild2) ;
  152.  
  153.      while (GetMessage(&msg, NULL, 0, 0))
  154.      {
  155.       TranslateMessage(&msg);
  156.       DispatchMessage(&msg);
  157.      } 
  158.      return (msg.wParam) ;     
  159.      }
  160.  
  161. /****************************************************************************/
  162.  
  163. long FAR PASCAL WndProc (hWnd, iMessage, wParam, lParam)
  164. HWND     hWnd ;
  165. unsigned iMessage ;
  166. WORD     wParam ;
  167. LONG     lParam ;
  168. {
  169.  HMENU       hMenu;
  170.  PAINTSTRUCT ps;
  171.  static int  xClient, yClient;
  172.  static int  xDevisor, yDevisor, randNum, Index;
  173.  char        szOutputBuffer [40];
  174.  
  175.  switch(iMessage)
  176.  {
  177.   case WM_CREATE:
  178.        hMenu = GetSystemMenu (hWnd, FALSE);
  179.  
  180.        ChangeMenu (hMenu, NULL, "&About", IDM_ABOUT, 
  181.                    MF_APPEND | MF_STRING);
  182.        break;
  183.  
  184.   case WM_SYSCOMMAND:
  185.        switch (wParam) {
  186.           case IDM_ABOUT:
  187.                ProcessMessage (hWnd, 0);
  188.                break;
  189.           default:
  190.                return DefWindowProc (hWnd, iMessage, wParam, lParam) ;
  191.        }
  192.        break;
  193.  
  194.   case WM_COMMAND:
  195.        switch (wParam) {
  196.           case IDM_SETPARENT1:
  197.                SetParent (hWndChild1, hWndParent1);
  198.                SetParent (hWndChild2, hWndParent1);
  199.                break;
  200.  
  201.           case IDM_SETPARENT2:
  202.                SetParent (hWndChild1, hWndParent2);
  203.                SetParent (hWndChild2, hWndParent2);
  204.                break;
  205.  
  206.           case IDM_HELP:
  207.                ProcessMessage (hWnd, 2);
  208.                break;
  209.        }
  210.        break;
  211.  
  212.   case WM_PAINT:
  213.        BeginPaint(hWnd, (LPPAINTSTRUCT)&ps);
  214.        EndPaint(hWnd, (LPPAINTSTRUCT)&ps);
  215.        break;
  216.  
  217.   case WM_DESTROY:
  218.        PostQuitMessage(0);
  219.        break;
  220.  
  221.   default:
  222.   {
  223.    return DefWindowProc (hWnd, iMessage, wParam, lParam) ;
  224.   }
  225.  }
  226.  return (0L); 
  227. }
  228.  
  229. /****************************************************************************/
  230.  
  231. long FAR PASCAL ChildWndProc (hWnd, iMessage, wParam, lParam)
  232.      HWND     hWnd ;
  233.      unsigned iMessage ;
  234.      WORD     wParam ;
  235.      LONG     lParam ;
  236.      {
  237.      return DefWindowProc (hWnd, iMessage, wParam, lParam) ;
  238.      }
  239.  
  240. 
  241.