Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Determine the Access application Window height 1

Status
Not open for further replies.
Feb 19, 2006
28
GB
Hey Guys,

I would like to code something that will give me the height of the Access application window as I wish to place a 'ticker tape' style device at the bottom of the window.

I have determined my 'ticker tape' device is 1185 twips high, but the actual inner height of the access window could vary from workstation to workstation, so I would like it to reposition itself everytime the main application window is resized.

I've done some hunting about for properties that this could pertain to, but to no avail.

As always, any help would be greatly appreciated

Thanks :)
 
If you're into APIs
Code:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindowRect Lib "user32" Alias "GetWindowRect" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
End Type



Public Sub WindowSize(ByRef Height As Long, ByRef Width As Long)

    Dim hwnd                        As Long
    Dim rct                         As RECT

    hwnd = FindWindow(vbNullString, "Microsoft Access")
    If hwnd <> 0 And GetWindowRect(hwnd, rct) <> 0 Then
        Height = (rct.Bottom - rct.Top) * Screen.TwipsPerPixelY
        Width = (rct.Right - rct.Left) * Screen.TwipsPerPixelX
    End If
End Sub
 
now knowing as little as I do about API coding - where would I put the Private decalre function sections?
 
Okay, I have that lot figured out, but how do I now get the height value through to my form so i can tell it where to position itself?

The form is 1185 twips high, and I want it positioned at the bottom left of the screen. The status bar is present.

I'm sorry to sound like such a novice, but there are so few decent API references out there.
 
Just call the Sub I posted. Right now it returns a result for the main Access window. You may want to make it more general by passing the form caption as an argument and using that in place of "Microsoft Access" so that it will return results for any form. The calling arguments are returned ByRef so their values after the call contain the Height and Width of the form.

Grab a window handle using FindWindow and then use the MoveWindow API to reposition it. Movewindow is documented here
 
... where would I put the Private declare function sections?

I have a module that I call modAPIDef and I have most of my API definitions including Declares, Constants and Types there as [blue]Public[/blue]. I just include that module in each project and then I don't need to worry about specifically coding them within the project.
 
Maybe it's me being totally stupid, but how do you call the Sub from the form?

I've tried

Code:
intWindowHeight = WindowSize.Height

without much success, and unsure how else I would reference it...

But really - thanks for your help so far golom - I have spent all day trawling the net for a solution to this to no avail at all.
 
Code:
Dim intWindowHeight As Long
Dim intWindowWidth  As Long

WindowSize intWindowHeight, intWindowWidth

Debug.Print "Height = " & intWindowHeight 
Debug.Print "Width  = " & intWindowWidth
 
Now I'm getting an error from the Sub at this section:

If hwnd <> 0 And GetWindowRect(hwnd, rct) <> 0 Then
Height = (rct.Bottom - rct.Top) * Screen.TwipsPerPixelY
Width = (rct.Right - rct.Left) * Screen.TwipsPerPixelX
End If


it doesn't seem to like the TwipsPerPixelY and TwipsPerPixelX and says "Method or data member not found".

Am I missing a reference or another function?
 
got it:

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

Const HWND_DESKTOP As Long = 0
Const LOGPIXELSX As Long = 88
Const LOGPIXELSY As Long = 90

'--------------------------------------------------
Function TwipsPerPixelX() As Single
'--------------------------------------------------
'Returns the width of a pixel, in twips.
'--------------------------------------------------
  Dim lngDC As Long
  lngDC = GetDC(HWND_DESKTOP)
  TwipsPerPixelX = 1440& / GetDeviceCaps(lngDC, LOGPIXELSX)
  ReleaseDC HWND_DESKTOP, lngDC
End Function

'--------------------------------------------------
Function TwipsPerPixelY() As Single
'--------------------------------------------------
'Returns the height of a pixel, in twips.
'--------------------------------------------------
  Dim lngDC As Long
  lngDC = GetDC(HWND_DESKTOP)
  TwipsPerPixelY = 1440& / GetDeviceCaps(lngDC, LOGPIXELSY)
  ReleaseDC HWND_DESKTOP, lngDC
End Function

Fantastic, took me a whole day to crack that one :)

Thanks to golom!
 
The problem was that the Screen object that I used was the VB screen object. The Access Screen object doesn't support those properties. Here's my fix (which is essentially identical to yours.)

In a Module
Code:
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function GetWindowRect Lib "user32" Alias "GetWindowRect" (ByVal hwnd As Long, lpRect As RECT) As Long
Public Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
End Type

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

Public Const WU_LOGPIXELSX = 88
Public Const WU_LOGPIXELSY = 90

Public Function TwipsPerPixel(strDirection As String) As Long

    'Handle to device
    Dim lngDC                       As Long
    Dim lngPixelsPerInch            As Long
    Const nTwipsPerInch = 1440
    lngDC = GetDC(0)

    If (Left$(strDirection, 1) = "X") Then                       'Horizontal
        lngPixelsPerInch = GetDeviceCaps(lngDC, WU_LOGPIXELSX)
    Else                                              'Vertical
        lngPixelsPerInch = GetDeviceCaps(lngDC, WU_LOGPIXELSY)
    End If
    lngDC = ReleaseDC(0, lngDC)
    TwipsPerPixel = nTwipsPerInch / lngPixelsPerInch

End Function


Public Sub WindowSize(ByRef Height As Long, ByRef Width As Long)

    Dim hwnd                        As Long
    Dim rct                         As RECT

    hwnd = FindWindow(vbNullString, "Microsoft Access")
    If hwnd <> 0 And GetWindowRect(hwnd, rct) <> 0 Then
        Height = (rct.Bottom - rct.Top) * TwipsPerPixel("Y")
        Width = (rct.Right - rct.Left) * TwipsPerPixel("X")
    End If
End Sub

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top