Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Forms Programming / March 2005

Tip: Looking for answers? Try searching our database.

center form?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
B.Feldman - 27 Mar 2005 23:10 GMT
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
Rob Oldfield - 28 Mar 2005 00:38 GMT
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?

> Hi,
>
[quoted text clipped - 9 lines]
>
> Boris Feldman
B.Feldman - 29 Mar 2005 00:45 GMT
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
Rob Oldfield - 29 Mar 2005 07:10 GMT
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)?

> Sorry, there is my code:
>
[quoted text clipped - 85 lines]
>     Resume exit_func
> End Function
Andreas - 29 Mar 2005 08:16 GMT
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

> 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
[quoted text clipped - 89 lines]
>>    Resume exit_func
>>End Function
Mark - 29 Mar 2005 15:37 GMT
Why mess with lots of code?  Have you tried using the form's AutoCenter
property in the design view?

> Hmm,
>
[quoted text clipped - 105 lines]
>>>    Resume exit_func
>>>End Function
B.Feldman - 31 Mar 2005 02:01 GMT
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: bsf_NOSPAM@NOSPAM.rambler.ru
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
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.