[AccessD] Form Positioning

Gustav Brock Gustav at cactus.dk
Fri Apr 8 12:25:41 CDT 2005


Hi Joe

We use the module below for basic screen metrics.
You should be able to use the sub CenterForm to model your PositionForm
sub by using the handle of the other form and not GetParent().

/gustav

<code>

Option Compare Database
Option Explicit

' Windows handle of desktop.
Private Const HWND_DESKTOP    As Long = 0
' Number of Twips per inch.
Private Const TWIPS_PER_INCH  As Long = 1440
' Pixel width.
Private Const LOGPIXELSX      As Long = 88
' Pixel height.
Private Const LOGPIXELSY      As Long = 90
' Screen width.
Private Const SM_CXSCREEN     As Long = 0
' Screen height.
Private Const SM_CYSCREEN     As Long = 1

Public Type RECT
  Left   As Long
  Top    As Long
  Right  As Long
  Bottom As Long
End Type
  
Private Declare Function GetSystemMetrics Lib "user32" ( _
  ByVal nIndex As Long) _
  As Long

Private Declare Function GetDC Lib "user32" ( _
  ByVal hwnd As Long) _
  As Long
  
Private Declare Function ReleaseDC Lib "user32" ( _
  ByVal hwnd As Long, _
  ByVal hdc As Long) _
  As Long
  
Private Declare Function GetDeviceCaps Lib "gdi32" ( _
  ByVal hdc As Long, _
  ByVal nIndex As Long) _
  As Long

Private Declare Function GetParent Lib "user32" ( _
  ByVal hwnd As Long) _
  As Long

Private Declare Function GetWindowRect Lib "user32" ( _
  ByVal hwnd As Long, _
  lpRect As RECT) _
  As Long

Public Declare Function MoveWindow Lib "user32" ( _
  ByVal hwnd As Long, _
  ByVal X As Long, _
  ByVal Y As Long, _
  ByVal nWidth As Long, _
  ByVal nHeight As Long, _
  ByVal bRepaint As Long) _
  As Long

Public Function ScreenWidth() As Long

' Returns the width of the screen in pixels.

  ScreenWidth = GetSystemMetrics(SM_CXSCREEN)
  
End Function

Public Function ScreenHeight() As Long

' Returns the height of the screen in pixels.

  ScreenHeight = GetSystemMetrics(SM_CYSCREEN)
  
End Function

Public Function TwipsPerPixelX() As Single

' Returns the width of a pixel, in twips.
  
  Dim lngDC             As Long
  Dim sngTwipsPerPixel  As Single
  
  lngDC = GetDC(HWND_DESKTOP)
  sngTwipsPerPixel = TWIPS_PER_INCH / GetDeviceCaps(lngDC, LOGPIXELSX)
  ReleaseDC HWND_DESKTOP, lngDC
  
  TwipsPerPixelX = sngTwipsPerPixel

End Function

Public Function TwipsPerPixelY() As Single

' Returns the height of a pixel, in twips.
  
  Dim lngDC             As Long
  Dim sngTwipsPerPixel  As Single
  
  lngDC = GetDC(HWND_DESKTOP)
  sngTwipsPerPixel = TWIPS_PER_INCH / GetDeviceCaps(lngDC, LOGPIXELSY)
  ReleaseDC HWND_DESKTOP, lngDC

  TwipsPerPixelY = sngTwipsPerPixel

End Function

Public Sub CenterForm(frm As Form)

  Dim rctMDI    As RECT
  Dim lngRetVal As Long
  Dim lngWidth  As Long
  Dim lngHeight As Long
  Dim lngLeft   As Long
  Dim lngTop    As Long
  
  lngRetVal = GetWindowRect(GetParent(frm.hwnd), rctMDI)
  
  lngWidth = frm.WindowWidth \ CLng(TwipsPerPixelX())
  lngHeight = frm.WindowHeight \ CLng(TwipsPerPixelY())
  lngLeft = (rctMDI.Right - rctMDI.Left - lngWidth) \ 2
  If lngLeft < 0 Then
    lngLeft = 0
  End If
  lngTop = (rctMDI.Bottom - rctMDI.Top - lngHeight) \ 2
  If lngTop < 0 Then
    lngTop = 0
  End If
  
  lngRetVal = MoveWindow(frm.hwnd, lngLeft, lngTop, lngWidth,
lngHeight, True)

End Sub

</code>

>>> JRojas at tnco-inc.com 04/06 10:33 pm >>>
Hi All,

I want to open a form in a specific location with relation to another
form.
How can I do this?

Thanks!
JR




More information about the AccessD mailing list