Download text file
#if 0

Hi,

  There are 2 routines here:

  CenterChildInWindow places the child dlg in the center of the parent window includeing the title bar and borders.

  fGetWindowLoc captures and returns the X,Y position of the window and the width and heigth of the window. The
                string that is returned is suitable for framing in an INI file so the size and postion can be
                restored or set when next the window is created.

  In both cases the variable names speak for themselves and neither routine is difficult to understand so I'll not
  go into a protracted explaination. If more info on the window is required or desired look into GetWindowPlacement
  API function for details.

C'ya,
Don
d83@DASoftVSS.com
#endif
'
'------------------------------------------------------------------------------
'------------ TEST CODE -------------------------------------------------------
'------------------------------------------------------------------------------
'
#if 1

  DECLARE      SUB  CenterChildInWindow ALIAS "CenterChildInWindow" (BYVAL hParent???,BYVAL hDlg???)
  DECLARE FUNCTION fGetWindowLoc        ALIAS "fGetWindowLoc"       (BYVAL hDlg???,OPT BYVAL InPixels&) AS STRING

#INCLUDE "WIN32API.INC"

%FLAG_DLGM1 =  %WS_POPUP Or %WS_BORDER Or %WS_DLGFRAME Or %WS_THICKFRAME Or %WS_SYSMENU Or %WS_MINIMIZEBOX Or %WS_MAXIMIZEBOX Or %WS_CLIPSIBLINGS Or %WS_VISIBLE Or %DS_MODALFRAME Or %DS_3DLOOK Or %DS_NOFAILCREATE Or %DS_SETFONT
%FLAG_DLGM2 =  %WS_EX_WINDOWEDGE Or %WS_EX_CONTROLPARENT Or %WS_EX_LEFT Or %WS_EX_LTRREADING Or %WS_EX_RIGHTSCROLLBAR
'
'------------------------------------------------------------------------------
'
CALLBACK FUNCTION TestCenterWindow_CB ()

  DIM Txt AS LOCAL ASCIIz PTR * 128
  SELECT CASE CBMSG
    CASE %WM_DESTROY    : DIALOG GET USER CBHNDL, 1 TO Txt
                          @Txt = fGetWindowLoc(CBHNDL)
                          DIALOG END CBHNDL, 0
    CASE %WM_COMMAND    : SELECT CASE AS LONG CBCTL
                            CASE 2 : DIALOG END CBHNDL, 0
                          END SELECT
  END SELECT

END FUNCTION
'
'------------------------------------------------------------------------------
'
SUB TestCenterWindow (BYVAL hParent AS DWORD)

  DIM hDlg AS LOCAL DWORD
  DIM  Txt AS LOCAL ASCIIz * 128

  DIALOG NEW hParent, "Test Window",,, 150, 150, %FLAG_DLGM1, %FLAG_DLGM2, TO hDlg
  CONTROL ADD BUTTON, hDlg, 2, "Exit",  95, 130, 50, 13

  DIALOG SET USER hDlg, 1, VARPTR(Txt)

  DIALOG SHOW MODAL hDlg, CALL TestCenterWindow_CB

  Txt = "The child window postion & size:" & $CRLF & Txt
  MSGBOX Txt

END SUB
'
'------------------------------------------------------------------------------
'
CALLBACK FUNCTION PBmain_CB ()

  SELECT CASE CBMSG
    CASE %WM_DESTROY    : DIALOG END CBHNDL, 0
    CASE %WM_COMMAND    : SELECT CASE AS LONG CBCTL
                            CASE    1 : TestCenterWindow CBHNDL
                            CASE    2 : DIALOG END CBHNDL, 0
                          END SELECT
  END SELECT

END FUNCTION
'
'------------------------------------------------------------------------------
'
FUNCTION PBmain ()

  DIM hDlg AS LOCAL DWORD

  DIALOG NEW %HWND_DESKTOP, "Main Window",,, 300, 300, %FLAG_DLGM1, %FLAG_DLGM2, TO hDlg
  CONTROL ADD BUTTON, hDlg, 1, "Go"  ,   5, 280, 50, 13
  CONTROL ADD BUTTON, hDlg, 2, "Exit", 245, 280, 50, 13

  DIALOG SHOW MODAL hDlg, CALL PBmain_CB


END FUNCTION
#endif
'
'------------------------------------------------------------------------------
'------------ MAIN CODE -------------------------------------------------------
'------------------------------------------------------------------------------
'
FUNCTION fGetWindowLoc ALIAS "fGetWindowLoc" (     BYVAL hDlg     AS DWORD, _
                                               OPT BYVAL InPixels AS LONG   ) EXPORT AS STRING

  DIM tP AS LOCAL WindowPlacement

  GetWindowPlacement hDlg, tP
  tP.rcNormalPosition.nBottom = tP.rcNormalPosition.nBottom - tP.rcNormalPosition.nTop  + 1
  tP.rcNormalPosition.nRight  = tP.rcNormalPosition.nRight  - tP.rcNormalPosition.nLeft + 1

  IF InPixels = 0 THEN
    DIALOG PIXELS hDlg, tP.rcNormalPosition.nTop , tP.rcNormalPosition.nBottom TO UNITS tP.rcNormalPosition.nTop , tP.rcNormalPosition.nBottom
    DIALOG PIXELS hDlg, tP.rcNormalPosition.nLeft, tP.rcNormalPosition.nRight  TO UNITS tP.rcNormalPosition.nLeft, tP.rcNormalPosition.nRight
  END IF

  FUNCTION = FORMAT$(tP.rcNormalPosition.nLeft  ) & "," & _
             FORMAT$(tP.rcNormalPosition.nTop   ) & "," & _
             FORMAT$(tP.rcNormalPosition.nRight ) & "," & _
             FORMAT$(tP.rcNormalPosition.nBottom)

END FUNCTION
'
'------------------------------------------------------------------------------
'
SUB CenterChildInWindow ALIAS "CenterChildInWindow" ( BYVAL hParent AS DWORD, _
                                                      BYVAL hDlg    AS DWORD  )

  DIM tC  AS LOCAL WindowPlacement
  DIM tP  AS LOCAL WindowPlacement
  DIM tPR AS LOCAL Rect
  DIM tCR AS LOCAL Rect

  tP.Length = SIZEOF(tP)
  tC.Length = tP.Length

  GetWindowPlacement hParent, tP
  GetWindowPlacement hDlg   , tC

  TYPE SET tPR = tP.rcNormalPosition
  TYPE SET tCR = tC.rcNormalPosition

  tPR.nBottom = tPR.nBottom - tPR.nTop  + 1   ' set sizes
  tPR.nRight  = tPR.nRight  - tPR.nLeft + 1
  tCR.nBottom = tCR.nBottom - tCR.nTop  + 1
  tCR.nRight  = tCR.nRight  - tCR.nLeft + 1

  tPR.nBottom = (tPR.nBottom - tCR.nBottom + 1) \ 2  ' tPR now holds offsets
  tPR.nRight  = (tPR.nRight  - tCR.nRight  + 1) \ 2

  tCR.nTop  = tPR.nTop  + tPR.nBottom
  tCR.nLeft = tPR.nLeft + tPR.nRight

  IF tCR.nTop  < 0 THEN tCR.nTop  = MAX(0,tPR.nTop  + tCR.nTop )
  IF tCR.nLeft < 0 THEN tCR.nLeft = MAX(0,tPR.nLeft + tCR.nLeft)

  tC.rcNormalPosition.nTop    = tCR.nTop
  tC.rcNormalPosition.nLeft   = tCR.nLeft
  tC.rcNormalPosition.nBottom = tCR.nTop  + tCR.nBottom - 1
  tC.rcNormalPosition.nRight  = tCR.nLeft + tCR.nRight  - 1

  SetWindowPlacement hDlg, tC

END SUB