center form?

B

B.Feldman

Hi,

I am trying to center form on the Access application window. For this task I
use a function, which I copy from one Access's book. But suddenly I found
that my form is not on the center of application window, it's closer to the
bottom that to the top of the window. As I see in the function body, there
is using system function to define application and form coordinates.

So what's wrong? How is it right to center form on the window.

With best regards,

Boris Feldman
 
R

Rob Oldfield

You seem to be asking "What's wrong with this code?" without actually saying
what the code is. You think you could provide a touch more detail? The
code perhaps?
 
B

B.Feldman

Sorry, there is my code:

Private Declare Function GetWindowRect Lib "user32" (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 Function centerFormOnAppWindow(frmIn As Object) As Long
Dim w As Long, h As Long
Dim frmLeft As Long, frmTop As Long
Dim frmWidth As Long, frmHeight As Long
Dim frmX As Long, frmY As Long

Dim leftNew As Long, topNew As Long

Dim AppW As Long, AppH As Long

Dim AppCenterX As Long, AppCenterY As Long
Dim dx As Long, dy As Long
Dim dblCfnt As Double

Dim lpRectApp As RECT, lpRectFrm As RECT
Dim res As Long
Dim frm As Form

centerFormOnAppWindow = 0
res = 0
Set frm = Nothing
On Error Resume Next
Set frm = frmIn
If frm Is Nothing Then Set frm = frmIn.Parent
On Error GoTo 0
If frm Is Nothing Then Exit Function
If Not frm.Moveable Then Exit Function
' Access Application window parametrs
res = GetWindowRect(Application.hWndAccessApp, lpRectApp)
If res <> 0 Then
With lpRectApp
AppW = .Right - .Left
AppH = .Bottom - .Top
End With
AppCenterX = AppW / 2
AppCenterY = AppH / 2
frmHeight = frm.WindowHeight
frmWidth = frm.WindowWidth
frmLeft = frm.WindowLeft
frmTop = frm.WindowTop
' Form window parametrs
res = GetWindowRect(frm.hwnd, lpRectFrm)
If res <> 0 Then
dblCfnt = frmWidth / (lpRectFrm.Right - lpRectFrm.Left) '
coefficient from system units (pixels?) to form units (twips?)
AppCenterX = AppCenterX * dblCfnt
AppCenterY = AppCenterY * dblCfnt
frmX = frmWidth / 2 + frmLeft
frmY = frmHeight / 2 + frmTop
dx = AppCenterX - frmX
dy = AppCenterY - frmY
leftNew = frmLeft + dx
topNew = frmTop + dy
If leftNew < 0 Then
leftNew = 0
End If
If topNew < 0 Then
topNew = 0
End If
If frmHeight > AppH * dblCfnt Then
frmHeight = AppH * dblCfnt
End If
If frmWidth > AppW * dblCfnt Then
frmWidth = AppW * dblCfnt
End If
frm.Move leftNew, topNew, frmWidth, frmHeight
End If
End If
centerFormOnAppWindow = res
exit_func:
Set frm = Nothing
Exit Function
err_Func:
errS = Err.Number & ": " & Err.Description & " (" & Err.Source & ")"
Debug.Print errS
centerFormOnAppWindow = 0
Resume exit_func
End Function
 
R

Rob Oldfield

Sorry. That doesn't work for me at all. Errors coping with the Moveable
and WindowLeft properties. This is for versions later than 2000 (which I
don't have)?
 
A

Andreas

Hmm,

What version of Access are you using?
XP and later (not sure on 2000) will allow you to do this without the
API call and in a far nicer manner (don't ask for the code as it is on
another PC and just to awkward to get on this one to e-mail it).

Anyway, at a guess, it looks as so this might not allow for
Menubar/Toolbars/etc which reside within the application window.

One way around this is to open a form (without header/footer), maximize
it and then get the width/height from that form to calculate the center.

Regards,
Andreas
 
M

Mark

Why mess with lots of code? Have you tried using the form's AutoCenter
property in the design view?
 
B

B.Feldman

Hi all,
1. I'm using XP and Access 2003. If you know how to center form in XP or
know XP or Access 2003 function that do it I'l use it in my function. My
e-mail: (e-mail address removed)
2. Realy, my code was got from the Access 2000/2002 books. As I know there
no books for Access 2003 programers in Russia yet.
3. The code I used at the beging was more less but use API call to center
form on the application window. So I found that after that any access forms
moving or resizing function were working with errors.
4. The code is so big because at the beging I think that it's my error, so,
I was tring to modify code.

With best regards,
Boris Feldman
 

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

Similar Threads


Top