home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / mag&info / msjv7_6.zip / TOPTEN.ARJ / PASSMSG.ARJ / PASSMSG.C < prev    next >
Text File  |  1992-10-01  |  8KB  |  278 lines

  1. #include <windows.h>
  2.  
  3. #include "basedefs.h"
  4. #include "passmsg.h"
  5.  
  6. WINPROC WndProc     ( WINDOWS_PARAMS );
  7. WINPROC FormWndProc ( WINDOWS_PARAMS );
  8.  
  9. /**************************************************************
  10. *                                                             *
  11. *                   Global Variables                          *
  12. *                                                             *
  13. **************************************************************/
  14.  
  15. HANDLE   ghInst;
  16. HWND     ghWnd;
  17. HWND     ghWndChild;
  18. char     szNullString[] = "";
  19. char     szAppName[] = "BASE1";
  20. char     szChildName[] = "Ben";
  21. char     szMoveMe[]    = "Move Me!";
  22.  
  23. /**************************************************************
  24. *                                                             *
  25. *                      WinMain                                *
  26. *                                                             *
  27. **************************************************************/
  28.  
  29. int PASCAL WinMain (HANDLE hInstance,  HANDLE hPrevInstance, 
  30.                     LPSTR lpszCmdLine, int    nCmdShow       )
  31. {                        
  32.   MSG         msg        ;
  33.   WNDCLASS    wndclass   ;
  34.   int         dx         ;
  35.   HBRUSH      hFormBrush ;
  36.   HBITMAP     hFormBM    ;
  37.  
  38.   dx = 24;
  39.   while ((!SetMessageQueue(dx)) && (dx >= 8)) dx--;
  40.  
  41.   hFormBM = LoadBitmap ( hInstance, "FORM" );
  42.   hFormBrush = CreatePatternBrush ( hFormBM );
  43.   DeleteObject ( hFormBM );
  44.  
  45.   if (!hPrevInstance) 
  46.     {
  47.  
  48.     // Register the Parent Window
  49.  
  50.     wndclass.style         = CS_BYTEALIGNCLIENT;
  51.     wndclass.lpfnWndProc   = (WNDPROC)WndProc ;
  52.     wndclass.cbClsExtra    = 0 ;
  53.     wndclass.cbWndExtra    = 0 ;
  54.     wndclass.hInstance     = hInstance ;
  55.     wndclass.hIcon         = NULL;
  56.     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  57.     wndclass.hbrBackground = hFormBrush;
  58.     wndclass.lpszMenuName  = NULL;
  59.     wndclass.lpszClassName = szAppName ;
  60.  
  61.     if (!RegisterClass (&wndclass))
  62.          return FALSE;
  63.  
  64.     // Register the Child Window
  65.  
  66.     wndclass.style         = CS_BYTEALIGNCLIENT | CS_HREDRAW | CS_VREDRAW;
  67.     wndclass.lpfnWndProc   = (WNDPROC)FormWndProc ;
  68.     wndclass.cbClsExtra    = 0 ;
  69.     wndclass.cbWndExtra    = 0 ;
  70.     wndclass.hInstance     = hInstance ;
  71.     wndclass.hIcon         = NULL;
  72.     wndclass.hCursor       = LoadCursor (hInstance, "HAND" );
  73.     wndclass.hbrBackground = GetStockObject ( LTGRAY_BRUSH ) ;
  74.     wndclass.lpszMenuName  = (LPSTR)NULL;
  75.     wndclass.lpszClassName = szChildName ;
  76.  
  77.     if (!RegisterClass (&wndclass))
  78.          return FALSE;
  79.     }
  80.  
  81.  
  82.   ghInst   = hInstance;
  83.  
  84.   ghWnd =        CreateWindow (szAppName, "PASSMSG Sample",
  85.                                WS_OVERLAPPEDWINDOW,
  86.                                GetSystemMetrics ( SM_CXSCREEN ) /4,
  87.                                GetSystemMetrics ( SM_CYSCREEN ) /4,
  88.                                GetSystemMetrics ( SM_CXSCREEN ) /2,
  89.                                GetSystemMetrics ( SM_CYSCREEN ) /2,
  90.                                NULL, NULL, hInstance, NULL) ;
  91.  
  92.   ghWndChild =   CreateWindow (szChildName, szNullString,
  93.                                WS_CHILD | WS_BORDER | WS_THICKFRAME | WS_VISIBLE,
  94.                                10, 10,
  95.                                100, 100,
  96.                                ghWnd, NULL, hInstance, NULL) ;
  97.  
  98.  
  99.   ShowWindow ( ghWnd, nCmdShow );
  100.   UpdateWindow ( ghWnd );
  101.  
  102.   while (GetMessage((LPMSG)&msg, NULL, 0, 0))
  103.     {
  104.     TranslateMessage(&msg);
  105.     DispatchMessage(&msg);
  106.     }
  107.  
  108.   DeleteObject ( hFormBrush );
  109.  
  110.   return msg.wParam ;
  111. }
  112.  
  113. /*********************************************************************
  114. *                                                                    *
  115. *                       WndProc: Main Message Translator             *
  116. *                                                                    *
  117. *********************************************************************/
  118.  
  119. WINPROC WndProc ( WINDOWS_PARAMS )
  120. {
  121.   static BOOL   bDragging;
  122.   static POINT  ptLastPos;
  123.  
  124.   switch ( msg )
  125.     {
  126.     case WM_LBUTTONDOWN:
  127.  
  128.       ptLastPos.x = LOWORD ( lParam);
  129.       ptLastPos.y = HIWORD ( lParam);
  130.  
  131.       ptLastPos.x = LOWORD ( lParam ) - LOWORD ( lParam )%8;
  132.       ptLastPos.y = HIWORD ( lParam ) - HIWORD ( lParam )%8;
  133.  
  134.       if (ChildWindowFromPoint(hWnd, ptLastPos) != hWnd)  // must be the child!
  135.         {
  136.         SetCapture ( hWnd );
  137.         bDragging = TRUE;
  138.         }
  139.       break;
  140.  
  141.     case WM_LBUTTONUP:
  142.  
  143.       if (bDragging)
  144.         {
  145.         InvalidateRect ( ghWndChild, NULL, TRUE );
  146.         ReleaseCapture ( );
  147.         bDragging = FALSE;
  148.         }
  149.       break;
  150.  
  151.     case WM_MOUSEMOVE:
  152.  
  153.       if (bDragging)
  154.         {
  155.         POINT ptNew;
  156.  
  157.         ptNew.x = LOWORD ( lParam ) - LOWORD ( lParam )%8;
  158.         ptNew.y = HIWORD ( lParam ) - HIWORD ( lParam )%8;
  159.  
  160.         if (
  161.             ( ptNew.x != ptLastPos.x )
  162.             ||
  163.             ( ptNew.y != ptLastPos.y )
  164.            )
  165.           {
  166.           POINT ptTopLeft;
  167.  
  168.           // Figure out current position of Child window
  169.  
  170.           ptTopLeft.x = 0 - GetSystemMetrics ( SM_CXFRAME );
  171.           ptTopLeft.y = 0 - GetSystemMetrics ( SM_CYFRAME );
  172.  
  173.           ClientToScreen ( ghWndChild, &ptTopLeft );
  174.           ScreenToClient ( hWnd,       &ptTopLeft );
  175.  
  176.           // Move child Window to new spot
  177.  
  178.           SetWindowPos (ghWndChild, 
  179.                         NULL, 
  180.                         ptTopLeft.x + (ptNew.x-ptLastPos.x),
  181.                         ptTopLeft.y + (ptNew.y-ptLastPos.y),
  182.                         NULL,
  183.                         NULL,
  184.                         SWP_NOSIZE | SWP_NOZORDER 
  185.                        );
  186.  
  187.           InvalidateRect (ghWndChild, NULL, TRUE );
  188.           UpdateWindow (ghWndChild);
  189.  
  190.           ptLastPos = ptNew;
  191.           }
  192.         }
  193.  
  194.       break;
  195.  
  196.     case WM_CREATE  :
  197.  
  198.       break;
  199.  
  200.     case WM_DESTROY :
  201.  
  202.       PostQuitMessage (0) ;
  203.       break ;
  204.  
  205.     default :
  206.  
  207.       return DefWindowProc ( hWnd, msg, wParam, lParam );
  208.  
  209.     }
  210.   return 0L ;
  211. }
  212. /*********************************************************************
  213. *                                                                    *
  214. *                                                                    *
  215. *                                                                    *
  216. *********************************************************************/
  217.  
  218. WINPROC FormWndProc ( WINDOWS_PARAMS )
  219. {
  220.   POINT         pt;
  221.   HDC           hDC;
  222.   PAINTSTRUCT   ps;
  223.   RECT          rc;
  224.  
  225.   switch ( msg )
  226.     {
  227.     case WM_ERASEBKGND:  // Eat this message to avoid flicker
  228.  
  229.       break;
  230.  
  231.     case WM_CREATE  :
  232.  
  233.       break;
  234.  
  235.     case WM_LBUTTONDOWN:
  236.     case WM_LBUTTONUP:
  237.     case WM_MOUSEMOVE:
  238.  
  239.       // Pass all mouse messages on through to parent, in parent coordinates
  240.  
  241.       pt.x = LOWORD ( lParam );
  242.       pt.y = HIWORD ( lParam );
  243.  
  244.       ClientToScreen ( hWnd,            &pt );
  245.       ScreenToClient ( GetParent(hWnd), &pt );
  246.  
  247.       PostMessage ( ghWnd, msg, wParam, MAKELONG (pt.x, pt.y ));
  248.       break;
  249.  
  250.     case WM_PAINT:
  251.  
  252.       // Just draw a simple little something centered in the window with nice
  253.  
  254.       hDC = BeginPaint ( hWnd, &ps );
  255.       GetClientRect ( hWnd, &rc );
  256.  
  257.       SetBkColor   ( hDC, RGB (   0,   0, 128 )); 
  258.       SetTextColor ( hDC, RGB ( 255, 255,   0 )); 
  259.  
  260.       pt.x = (rc.right  - LOWORD (GetTextExtent ( hDC, szMoveMe, lstrlen(szMoveMe))))/2;
  261.       pt.y = (rc.bottom - HIWORD (GetTextExtent ( hDC, szMoveMe, lstrlen(szMoveMe))))/2;
  262.  
  263.       ExtTextOut ( hDC, pt.x, pt.y, ETO_OPAQUE, &rc, szMoveMe, lstrlen (szMoveMe), NULL );
  264.  
  265.       EndPaint ( hWnd, &ps );
  266.       break;
  267.  
  268.     default :
  269.  
  270.       return DefWindowProc ( hWnd, msg, wParam, lParam );
  271.  
  272.     }
  273.   return 0L ;
  274. }
  275.  
  276.  
  277. 
  278.