Stuart McCall's Microsoft Access Pages - Windows
Set the size of the main Access window
Various window manipulation routines
Convert Pixel values into Twips and vice versa
Determine network user name
Determine computer name
Home
Set the size of the main Access window
Private Type Rect
x1 As Long
y1 As Long
x2 As Long
y2 As Long
End Type
'
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function GetWindowRect Lib "user32" _
(ByVal hWnd As Long, r As Rect) As Long
Private Declare Function IsZoomed Lib "user32" _
(ByVal hWnd As Long) As Long
Private Declare Function MoveWindow Lib "user32" _
(ByVal hWnd As Long, ByVal X As Long, ByVal Y As Long, _
ByVal dx As Long, ByVal dy As Long, ByVal fRepaint As Long) As Long
Private Declare Function ShowWindow Lib "user32" _
(ByVal hWnd As Long, ByVal nCmdShow As Long) As Long
|
Sub SizeAccess(ByVal dx As Long, ByVal dy As Long)
'Set size of Access and center on Desktop
Const SW_RESTORE As Long = 9
Dim h As Long
Dim r As Rect
On Error Resume Next
h = Application.hWndAccessApp
'If maximised, restore
If (IsZoomed(h)) Then ShowWindow h, SW_RESTORE
'Get available Desktop size
GetWindowRect GetDesktopWindow(), r
If ((r.x2 - r.x1) - dx) < 0 Or ((r.y2 - r.y1) - dy) < 0 Then
'Desktop smaller than requested size
'so size to Desktop
MoveWindow h, r.x1, r.y1, r.x2, r.y2, True
Else
'Adjust to requested size and center
MoveWindow h, _
r.x1 + ((r.x2 - r.x1) - dx) \ 2, _
r.y1 + ((r.y2 - r.y1) - dy) \ 2, _
dx, dy, True
End If
End Sub
|
Usage:
SizeAccess 640, 480
Various window manipulation routines
Private Type Rect
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
'
Private Const SWP_NOSIZE = &H1
Private Const SWP_NOZORDER = &H4
'
Private Declare Function GetClientRect Lib "user32" (ByVal hWnd As Long, lpRect As Rect) As Long
Private Declare Function GetDesktopWindow Lib "user32" () 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
Private Declare Function IsZoomed Lib "user32" (ByVal hWnd As Long) As Long
Private 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
Private Declare Function SetWindowPos Lib "user32" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long
|
Public Function SizeFormToWindow(f As Object)
'Sizes the form or report to fit
'inside the main Access window
Dim r As Rect
If IsZoomed(f.hWnd) Then ShowWindow f.hWnd, 1
r = ParentClientRect(f)
MoveWindow f.hWnd, 0, 0, r.Right - r.Left, r.Bottom - r.Top, True
End Function
Public Function CenterChildForm(f As Object)
'Centers an MDI child window
Dim cr As Rect, fr As Rect
If IsZoomed(f.hWnd) Then ShowWindow f.hWnd, 1
cr = ParentClientRect(f)
fr = FormRect(f)
X& = (cr.Left + (cr.Right - cr.Left) / 2) - ((fr.Right - fr.Left) / 2)
Y& = (cr.Top + (cr.Bottom - cr.Top) / 2) - ((fr.Bottom - fr.Top) / 2)
SetWindowPos f.hWnd, 0&, X&, Y&, 0&, 0&, SWP_NOSIZE Or SWP_NOZORDER
End Function
Public Function CenterPopupForm(f As Object, Optional CenterOnScreen As Boolean)
'Centers a Popup window
Dim sr As Rect, fr As Rect
If IsZoomed(f.hWnd) Then ShowWindow f.hWnd, 1
hw& = IIf(CenterOnScreen, GetDesktopWindow(), Application.hWndAccessApp)
sr = WinRect(hw&)
fr = FormRect(f)
X& = (sr.Left + (sr.Right - sr.Left) / 2) - ((fr.Right - fr.Left) / 2)
Y& = (sr.Top + (sr.Bottom - sr.Top) / 2) - ((fr.Bottom - fr.Top) / 2)
SetWindowPos f.hWnd, 0&, X&, Y&, 0&, 0&, SWP_NOSIZE Or SWP_NOZORDER
End Function
Public Function FormRect(f As Object) As Rect
'Returns a rectangle of the form or report's coords
FormRect = WinRect(f.hWnd)
End Function
Private Function WinRect(hWnd As Long) As Rect
'Returns a rectangle of the window with the handle hWnd
Dim r As Rect
GetWindowRect hWnd, r
WinRect = r
End Function
Private Function ParentClientRect(f As Object) As Rect
'Returns a rectangle of the MDI child form's coords
Dim r As Rect
GetClientRect GetParent(f.hWnd), r
ParentClientRect = r
End Function
|
Convert Pixel values into Twips and vice versa
Private Const WU_LOGPIXELSX As Long = 88
Private Const WU_LOGPIXELSY As Long = 90
Private Const TwipsPerInch As Long = 1440
Private Declare Function GetDC Lib "user32" (ByVal hw As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hw As Long, ByVal hDC As Long) As Long
Private Declare Function GetDeviceCaps Lib "Gdi32" (ByVal hDC As Long, ByVal iCapability As Long) As Long
|
Public Function PixelsPerInch(strDirection As String) As Long
'strDirection can be either:
' "H" (Horizontal) or "V" (Vertical)
' eg: VerticalPixels = Pixelsperinch("V")
hDC& = GetDC(0)
PixelsPerInch = GetDeviceCaps(hDC&, _
Switch(strDirection = "H", WU_LOGPIXELSX, _
strDirection = "V", WU_LOGPIXELSY))
ReleaseDC 0, hDC&
End Function
Public Function PixelsToTwips(lngPixels As Long, strDirection As String) As Long
' eg: Forms!Form1.Width = PixelsToTwips(PixelWidth, "H")
PixelsToTwips = (lngPixels / PixelsPerInch(strDirection)) * TwipsPerInch
End Function
Public Function TwipsToPixels(lngTwips As Long, strDirection As String) As Long
' eg: PixelWidth = TwipsToPixels(Forms!Form1.Width, "H")
TwipsToPixels = (lngTwips / TwipsPerInch) * PixelsPerInch(strDirection)
End Function
|
Determine network user name
Private Declare Function GetUserNameA Lib "advapi32.dll" (ByVal lpBuffer As String, nSize As Long) As Long
|
Function NetUserName() As String
Dim rtn As Long
Dim Buffer As String * 255
rtn = GetUserNameA(Buffer, Len(Buffer))
If rtn Then
NetUserName = Left$(Buffer, InStr(Buffer, vbNullChar) - 1)
End If
End Function
|
Determine computer name
Private Declare Function GetComputerNameA Lib "kernel32" (ByVal lpBuffer As String, nSize As Long) As Long
|
Public Function ComputerName() As String
Dim rtn As Long
Dim Buffer As String * 255
rtn = GetComputerNameA(Buffer, Len(Buffer))
If rtn Then
ComputerName = Left$(Buffer, InStr(Buffer, vbNullChar) - 1)
End If
End Function
|