The trouble with the maximise event
The first in an occasional series of rants by Chris
Shepherd
When chatting to other developers and users I often find myself starting
sentences with "The trouble with Access is.....". The purpose of this
column is to get some of these problems out in to the open and to offer some
possible solutions.
For my first rant I would like to look at the maximise event (or lack of it).
A few weeks back I was watching some customers use my system. This is always a
sobering thing to do and a very educational way to learn what does and doesn’t
work on user interfaces. As an aside - next time you are designing parts of the
user interface don’t explain what each part of the screen does, ask them to
explain what they think each part does. This is a great way to discover that
your nice clear screens don’t make any sense to the users at all.
Anyway I was watching my users and noticed that with a particular screen
every user ran it maximised. What’s wrong with that I hear you cry. Well the
trouble with Access is that you cannot detect when a user hits the maximise
form button. You can detect if they drag the edges and make it larger then you
can but lots of fun code in the resize event to resize all the controls. But
there is no Maximise event and what is worse maximising a form doesn’t fire
off the resize event. I confidently expect a mail at this point from some one
telling me that I am labouring under a misapprehension, but until that time here
are some thoughts about how to get round it.
The easiest way is not to bother with any resize code, if you are going to do
this the first thing you must do is change the resolution of your screen to
match that of the user with the lowest setting. There is something slightly
embarrassing about demonstrating your nice shiny new data entry form and the
users can only see half of it.
The second option involves writing all the resize code. For some unknown
reason I always find this easier to do after three cans of lager, where as sober
the code never seems to work properly/ Once you have the resize code working
just switch of the maximise button on the form and let the users resize the form
as necessary.
My preferred choice in this instance was to have the form always opened up in
a maximised state (no mater what the users screen resolution) and to resize the
controls as necessary. How do you get a form to maximise? Well you could try
The problem with this is that you are back to square one because you don’t
know how big the screen is and the resize event doesn’t fire.
What you need is a way to maximise any given screen, rummaging around MSDN I
found the following code for a MaximizeRestoredForm subroutine:
Option Compare Database
Option Explicit
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'Declarations used by MaximizeRestoredForm
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Type Rect
x1 As Long
y1 As Long
x2 As Long
y2 As Long
End Type
Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long,
_
lpRect As Rect) As Long
Declare Function IsZoomed Lib
"user32" (ByVal hwnd As Long) As Long
Declare Function ShowWindow Lib
"user32" (ByVal hwnd As Long, _
ByVal nCmdShow As Long) As Long
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
Declare Function GetParent Lib
"user32" (ByVal hwnd As Long) As Long
Public Const SW_MAXIMIZE = 3
Public Const SW_SHOWNORMAL = 1
Sub MaximizeRestoredForm(F As Form)
Dim MDIRect As Rect
Const ScrlBar As Integer = 5
' If the form is maximized, restore it.
If IsZoomed(F.hwnd) <> 0 Then
ShowWindow F.hwnd, SW_SHOWNORMAL
End If
' Get the screen coordinates and window size of the
' MDIClient window.
GetWindowRect GetParent(F.hwnd), MDIRect
' Move the form to the upper left corner of the MDIClient
' window (0,0) and size it to the same size as the
' MDIClient window.
MoveWindow F.hwnd, 0, 0, MDIRect.x2 - MDIRect.x1 - ScrlBar, _
MDIRect.y2 -
MDIRect.y1 - ScrlBar, True
End Sub |
This can be called when opening a form or from a maximise button using the
syntax:
Personally I keep this code as part of a utilities module (usefull97.mdb
or usefull2000.mdb )available from this web site) which I add to each and every project I do. This
dramatically reduces the time it takes to develop systems.
If you have any comments about this article or would like to suggest other
niggles with Access please drop me a line at Mail Chris
Chris Shepherd is a Technical Consultant working for SCC in the UK. He is an
MCP in Access, VB, Excel and will go anywhere for a beer.
|