PlanetSquires Forums

Support Forums => Other Software and Code => Topic started by: Richard Kelly on January 13, 2011, 12:23:33 PM

Title: Centering Forms
Post by: Richard Kelly on January 13, 2011, 12:23:33 PM
I'd like the option, when a form is created to center it within another form.

i.e.


Function FormCenter (ByVal hWndParent As Dword, hWndChild As Dword) As Long

Local pPLACEMENT     As WINDOWPLACEMENT
Local rcParent       As Rect
Local nParentHeight  As Long
Local nParentWidth   As Long
Local nChildHeight   As Long
Local nChildWidth    As Long

   pPLACEMENT.Length = SizeOf (pPLACEMENT)
   
   Select Case GetWindowPlacement (hWndParent, ByVal VarPtr (pPLACEMENT))
   
      Case %TRUE
     
         Select Case pPLACEMENT.showCmd
         
            Case %SW_SHOWMAXIMIZED, %SW_SHOWMINIMIZED
           
            Case Else
           
               GetWindowRect (hWndParent, rcParent)
               
               nParentHeight = rcParent.nBottom - rcParent.nTop
               
               nParentWidth = rcParent.nRight - rcParent.nLeft
               
               FF_Control_GetSize (hWndChild, nChildWidth, nChildHeight)
               
               If nChildWidth < nParentWidth And nChildHeight < nParentHeight Then
   
                  SetWindowPos (hWndChild, _
                                0, _
                                rcParent.nLeft + ((nParentWidth - nChildWidth) / 2), _
                                rcParent.nTop + ((nParentHeight - nChildHeight) / 2), _
                                nChildWidth, _
                                nChildHeight, _
                                0)
                               
               End If
       
         End Select
   
   End Select

End Function
Title: Re: Centering Forms
Post by: Rolf Brandt on January 13, 2011, 02:19:29 PM
I use this code to center a child on a parent dialog (found it somewhere on a forum.):
'--------------------------------------------------------------------------------
' CenterOnParent
' Center Child Dialog on Parent Dialog
'--------------------------------------------------------------------------------
Sub CenterOnParent(ParentFrm As Dword, ChildFrm As Dword)
   Local pWin As Rect   'parent window 
   Local cWin As Rect   'child window   
   Local posLeft As Long
   Local posTop As Long
   
   GetWindowRect ParentFrm, pWin     
   GetWindowRect ChildFrm, cWin
   posLeft = (pWin.nLeft + pWin.nRight)/2 - (cWin.nRight - cWin.nLeft)/2
   posTop = (pWin.nTop + pWin.nBottom)/2 - (cWin.nBottom - cWin.nTop)/2
   
   MoveWindow ChildFrm, posLeft, posTop, (cWin.nRight -cWin.nLeft), (cWin.nBottom - cWin.nTop), %TRUE
End Sub