Re: Maximize or minimize form with button

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hi Mandeno!
Thanks again for your helpful notes. Please guide me if we can also maximize
or restore the main window of the MS Access with some sort of code or
settings apart from clicking at the top of the window, and also whether we
can change the heading coming at the top of the window, which I think, is
called as Forms MDI window. Can we give a title of ours own at the top? and
further can we remove the windows icons and buttons of all sorts also from
the main window. I being a novice would appreciate much your kind help.

"Graham Mandeno" wrote:

Hi Imran (or if that is your family name, then "Hi Ghani" :-)

The controls on a form are always positioned from the top left-hand corner.
This does not change if you maximize the form, so you would expect that
everything will appear to crowd up near the corner when you maximize the
form. This is unavoidable.

If you want to position the contents of the form in the centre then every
control (textbox/label/combo box/etc) will need to be repositioned
individually - not an easy task.

--
Good Luck :-)

Graham Mandeno [Access MVP]
Auckland, New Zealand

"Imran Ghani" <ImranGhani@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:3C730D6C-9F1F-4F66-9CA9-0A6B1CD23436@xxxxxxxxxxxxxxxx
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.

"Graham Mandeno" wrote:

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 ===============

"Imran Ghani" <ImranGhani@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:4A024875-63C9-420F-B51C-D6AE3DDC54A1@xxxxxxxxxxxxxxxx
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.

"Graham Mandeno" wrote:

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


"Imran Ghani" <ImranGhani@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:3B680859-85EB-41A5-823A-B4541DDB94D9@xxxxxxxxxxxxxxxx
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.

"Graham Mandeno" wrote:

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

"Imran Ghani" <ImranGhani@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in
message
news:D44C8898-B40E-4BDC-A4E1-9E446FFD4DEF@xxxxxxxxxxxxxxxx
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?

"Graham Mandeno" wrote:

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.

--
Good Luck :-)
.



Relevant Pages

  • Re: Creating new appointment & setting its properties
    ... The window and process subclassing I've seen with WordMail is horrendous, it's miles of code just to get a true hWnd for a WordMail window usually. ... Private Declare Function GetForegroundWindow Lib "User32.dll" As Long ... Private Declare Function FindWindowEx Lib "User32.dll" Alias "FindWindowExA" (ByVal hwndParent As Long, ByVal hwndChildAfter As Long, ByVal lpszClass As String, ByVal lpszWindow As String) As Long ...
    (microsoft.public.outlook.program_vba)
  • Re: Maximize or minimize form with button
    ... Private Declare Function apiGetClassName _ ... (ByVal hWnd As Long, _ ... ' If the form is maximized, restore it. ... However, if you drag it to another location, then maximize/restore it, it ...
    (microsoft.public.access.modulesdaovba)
  • Re: Maximize or minimize form with button
    ... Private Declare Function apiGetClassName _ ... (ByVal hWnd As Long, _ ... ' If the form is maximized, restore it. ... However, if you drag it to another location, then maximize/restore it, ...
    (microsoft.public.access.modulesdaovba)
  • Re: Maximize or minimize form with button
    ... Private Declare Function apiGetClassName _ ... (ByVal hWnd As Long, _ ... ' If the form is maximized, restore it. ... However, if you drag it to another location, then maximize/restore it, it ...
    (microsoft.public.access.modulesdaovba)
  • Re: Are VBA userforms always child of Windows desktop?
    ... Private Declare Function GetDesktopWindow Lib "user32" As Long ... Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long ... The system restores the window to its original state. ... Dim hWndDesktop As Long ...
    (microsoft.public.excel.programming)