home *** CD-ROM | disk | FTP | other *** search
/ BUG 4 / BUGCD1997_05.BIN / aplic / clip4win / clip4win.exe / C4W30E.HUF / SOURCE / CONTRIB / CENTER.ZIP / CENTER.PRG
Text File  |  1993-06-13  |  3KB  |  80 lines

  1. // Program...: Center.prg
  2. // Author....: Gerald Barber
  3. // Date......: June 5, 1993 04:14 pm; Last Updated June 13, 1993 01:13 pm
  4. // Notice....: Copyright 1993, Gerald Barber
  5. // Notes.....: Part of CalcFxn.Lib
  6. //             Centers a Child Window within its Parent
  7. //             May be used to center a dialog box within the parent window
  8. //             by calling Center(hWnd) in response to the WM_INITDIALOG
  9. //             windows message.
  10. //             Part of CalcFxn.Lib
  11. //             Requires Clip4Win v 1.2 or greater
  12. // Parameters: hWnd -> handle to the Child Window
  13.  
  14.  
  15. #define WIN_WANT_ALL
  16.  
  17. #include "windows.ch"
  18.  
  19. function Center(hWnd)
  20. Local hWndParent              // handle to the Parent Window
  21. Local aChild_[4]              // Screen Coordinates of Child Window
  22. Local iCWidth                 // Width of Child Window in Pixels
  23. Local iCHeight                // Height of Child Window in Pixels
  24. Local aParent_[4]             // Logical Coordinates of Parent Window
  25. Local aPoint_[2]              // Multiple Uses
  26.  
  27. // Obtain upper left and lower right coordinates of the child window
  28. // in screen coordinates and determine its height and width.
  29. aChild_  := GetWindowRect(hWnd)
  30. iCWidth  := aChild_[3] - aChild_[1]
  31. iCHeight := aChild_[4] - aChild_[2]
  32.  
  33. // Obtain handle for parent window
  34. hWndParent := GetWindow(hWnd,GW_OWNER)
  35.  
  36. // Obtain client rectangle of the parent window in logical coordinates
  37. aParent_   := GetClientRect(hWndParent)
  38.  
  39. // Set aPoint_ equal to the center of the Client Rectangle
  40. // of the parent window in logical coordinates
  41. aPoint_    := {(aParent_[3]/2),(aParent_[4]/2)}
  42.  
  43. // Convert aPoint_ from logical to screen coordinates
  44. ClienttoScreen(hWndParent,aPoint_)
  45.  
  46. // Offset aPoint_ by half the height and width of the child window
  47. // still in screen coordinates
  48. aPoint_[1] -= (iCWidth  / 2)
  49. aPoint_[2] -= (iCHeight / 2)
  50.  
  51. // 06/13/93 Modified GB - Not Pretty but it works
  52. // Insure that new upper left corner falls within client area of
  53. // Parent Window
  54.  
  55. // Convert Point to logical coordinates or parent window
  56. ScreentoClient(hWndParent,aPoint_)
  57. // Make sure logical coordinates >= 0
  58. aPoint_[1] := max(0, aPoint_[1])
  59. aPoint_[2] := max(0, aPoint_[2])
  60. // Convert Point back to screen coordinates
  61. ClienttoScreen(hWndParent,aPoint_)
  62.  
  63. // Move the child window to the new screen coordinates.
  64. // aPoint_ := the new upper left corner of the child window
  65. // Height and width of the child window remain unchanged.
  66. MoveWindow(hWnd,aPoint_[1],aPoint_[2],iCWidth,iCHeight,.F.)
  67. return nil
  68.  
  69. // GetWindowRect calls the appropriate windows function in the
  70. // default windows USER.EXE DLL Library to return the upper left and
  71. // lower right screen coordinates of the hWnd passed to it.
  72. function GetWindowRect(hWnd)
  73. local hLib := LoadLibrary("USER.EXE")
  74. local cGWR := GetProcAddress(hLib, "GetWindowRect", ;
  75.                              "Pascal", "void", "HWND, string")
  76. local cBuf := space(8)
  77. CallDLL(cGWR, hWnd, @cBuf)
  78. FreeLibrary(hLib)
  79. return bin2a(cBuf, "int[4]")
  80.