Hi Mandeno!
Thanks a lot for your very helpful code. The code is indeed working
great
when the form is in the Restored state, but when it is maximized again
the
contents do not come in the center. Please do guide me for what type of
settings I have to look for. Are there any specific settings I have to
have,
apart from the function call. I am having my call function code in the
form
current code. I'd appreciate your kind help in this regards.
:
Hi Imran
Well, I warned you the code was serious!
Copy the code below and paste it into a new, empty module.
Then you can call the function winCenterInMDI from your form code,
passing
the handle of the form's window like this:
Call winCenterInMDI(Me.hWnd)
If the form is already maximized or minimized, then it will be
restored,
then it will be centered in the Access window.
--
Good Luck
Graham Mandeno [Access MVP]
Auckland, New Zealand
========== start code ===============
Option Compare Text
Option Explicit
Private Const cMaxBuffer = 255
Private Declare Function apiGetClassName _
Lib "user32" _
Alias "GetClassNameA" _
(ByVal hWnd As Long, _
ByVal lpClassName As String, _
ByVal nMaxCount As Long) _
As Long
Private Declare Function apiGetWindow _
Lib "user32" _
Alias "GetWindow" _
(ByVal hWnd As Long, _
ByVal wCmd As Long) _
As Long
Private Const GW_HWNDNEXT = 2
Private Const GW_CHILD = 5
Private Declare Function apiShowWindow _
Lib "user32" _
Alias "ShowWindow" _
(ByVal hWnd As Long, _
ByVal nCmdShow As Long) _
As Long
Private Const SW_RESTORE = 9
Private Declare Function apiIsZoomed _
Lib "user32" _
Alias "IsZoomed" _
(ByVal hWnd As Long) _
As Long
Private Declare Function apiIsIconic _
Lib "user32" _
Alias "IsIconic" _
(ByVal hWnd As Long) _
As Long
Private Declare Function apiMoveWindow _
Lib "user32" _
Alias "MoveWindow" _
(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 Type winRECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Declare Function apiGetWindowRect _
Lib "user32" _
Alias "GetWindowRect" _
(ByVal hWnd As Long, _
lpRect As winRECT) _
As Long
Public Function winGetHWndMDI(Optional hWndApp As Long) As Long
Dim hWnd As Long
winGetHWndMDI = 0
If hWndApp = 0 Then hWndApp = Application.hWndAccessApp
hWnd = apiGetWindow(hWndApp, GW_CHILD)
Do Until hWnd = 0
If winGetClassName(hWnd) = "MDIClient" Then
winGetHWndMDI = hWnd
Exit Do
End If
hWnd = apiGetWindow(hWnd, GW_HWNDNEXT)
Loop
End Function
Public Function winGetClassName(hWnd As Long) As String
Dim sBuffer As String, iLen As Integer
sBuffer = String$(cMaxBuffer - 1, 0)
iLen = apiGetClassName(hWnd, sBuffer, cMaxBuffer)
If iLen > 0 Then
winGetClassName = Left$(sBuffer, iLen)
End If
End Function
Public Function winCenterInMDI(hWnd As Long)
Dim MDIRect As winRECT, WndRect As winRECT
Dim lNewTop As Long, lNewLeft As Long
Dim lHeight As Long, lWidth As Long
' If the form is maximized, restore it.
If apiIsZoomed(hWnd) <> 0 Or apiIsIconic(hWnd) <> 0 Then
apiShowWindow hWnd, SW_RESTORE
End If
' Get the window size of the MDIClient window.
apiGetWindowRect winGetHWndMDI, MDIRect
' Get the window size of the MDIClient window.
apiGetWindowRect hWnd, WndRect
' Calculate Height and Width of window we are moving
With WndRect
lHeight = .Bottom - .Top
lWidth = .Right - .Left
End With
' Calculate new Top and Left coordinates
With MDIRect
lNewTop = (.Bottom - .Top - lHeight) / 2
lNewLeft = (.Right - .Left - lWidth) / 2
End With
' Move the window to the new position
apiMoveWindow hWnd, lNewLeft, lNewTop, lWidth, lHeight, True
End Function
========== end code ===============
Thanks indeed for your helpful advise. I do want to make my form to
re-center
itself when it is restored. I'd appreciate much your help in this
regards.
Yes, I'd like to code the form for the same purpose. Please go ahead
with
your helpful code for this purpose.
:
Hi Imran
I'm glad you got the Maximize/Restore working
If you set the form's AutoCenter property to "Yes" then the form
should
center itself when it is opened. If you then maximize it and
subsequently
restore it, it should revert to its previous position.
However, if you drag it to another location, then maximize/restore
it,
it
will stay in that new location.
If you want it to re-center itself when it is restored, then you
will
require some much more serious code. Do you really want to go
there?
--
Good Luck
Graham Mandeno [Access MVP]
Auckland, New Zealand
message
Thanks for your kind help. Your code worked perfectly with the
form's
maximized and restored positions. Actually the properties set in
my
form
were
not allowing the code to work. I have changed the properties and
hence
it
is
all working now. Can you please also guide regarding if I want to
bring
all
the forms' contents in centre of the form when the form is in
restored
position. For the time being the contents of the form are not in
the
centre
of the screen when its in restored position.I also want to bring
all
the
other forms in my application to the same position, either
restored
or
maximized, as the main form. What sort of coding or properties do
I
need
to
set to accomplish my goal.
:
Hi Imran
I have not used pictures on command buttons as they are a new
feature
of
Access 2007. However, I believe you should be able to change
them
at
run-time simply by assigning another image file name to the
Picture
property:
Me.cmdMyButton.Picture = "C:\Icons\NewIcon.ico"
--
Good Luck
Graham Mandeno [Access MVP]
Auckland, New Zealand
message
Thanks for your guidance. Please guide me if we can also
change
the
picture
of the command button also indicating the maximize and
minimize
position
along with changing the form position. Is there any specific
property
of
the
form which should also be enabled for the form to be maximized
or
restored?
:
Hi Imran
I'm sorry, I can't think of a reason for DoCmd.Maximize not
to
be
working.
Can you try creating a new form with nothing on it except the
cmdToggleMaximize command button, and the event procedure,
and
see
if
it
works then?
For moving and resizing the form, check out DoCmd.MoveSize
method,
and
also
the form's InsideHeight and InsideWidth properties.
You should be aware that the units for all these methods and
properties
is
the "TWIP". Are TWIP is a "twentieth of a point", which
means
there
are
1440 per inch.