Tiling Windows (Forms) with GetWindowRect and SetWindowPos api functions

J

Jon Lewis

I'm trying to tile a series of instances of the same Form by reteiving the a
form's position using GetWindowsRect, adding a fixed amount (actually the
title bar height) to the Left & Top elements and then using SetWindowsPos
with the new Left & Top values on the subsequent form.

However, extra X (a tiny bit) and Y (quite alot) shift happens.

So I tested the procedure by doing a GetWindowsRect and SetWindowsPos on the
same Form, leaving the Left & Top values the same. I would expect the Form
to remain in the same position but it doesn't. The MoveWindow api produces
exactly the same results.

The code that follows illustrates this. (Drop into a form - cmdMove is
obviously a command button).

I'm working within MSAccess so I don't know if this is an api issue or to do
with a peculiarity of Access forms.

Can anyone explain or suggest a resolution please?

BTW I can't use code from within the forms themselves so DoCmd.MoveSize
won't work for me.

TIA


Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

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.dll" (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 GetWindowRect Lib "user32.dll" _
(ByVal hWnd As Long, lpRect As RECT) As Long
Private Const SWP_NOSIZE = &H1 'don't resise the window
Private Const HWND_TOP = 0 'Put the window at the top of the Z-order.

Private Sub cmdMove_Click()
Dim R As RECT, s As RECT, t As RECT
Call GetWindowRect(Me.hWnd, R)
MsgBox R.Left & ", " & R.Top
MsgBox "Set"
Call SetWindowPos(Me.hWnd, HWND_TOP, R.Left, R.Top, 1, 1, SWP_NOSIZE)
Call GetWindowRect(Me.hWnd, s)
MsgBox s.Left & ", " & s.Top
MsgBox "Move"
Call MoveWindow(Me.hWnd, R.Left, R.Top, R.Right - R.Left, R.Bottom - R.Top,
True)
Call GetWindowRect(Me.hWnd, t)
MsgBox t.Left & ", " & t.Top
End Sub
 
D

Dave

Jon

In VB6 your code does not move the window by a single pixel.

Why are you adding an offset for the title bar, that is normally included in
what is returned by GetWindowRect?

Best regards
Dave O.
 
J

Jon Lewis

Dave,

I guess the make up of an Access form is causing the behaviour then. With
regards to the title bar, I was just using the bar height as an amount to
offset each subsequent instance of this form both left and top to achieve
the amount of tiling I wanted.

Thanks for your reply.

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top