Maximising a userform when it inializes

Steve Jukes

Board Regular
Joined
Apr 15, 2002
Messages
83
Can anyone tell me how to make a userform maximize to the screen size regardless of the screen settings.

Please help!!
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
On 2002-05-02 04:46, Steve Jukes wrote:
Can anyone tell me how to make a userform maximize to the screen size regardless of the screen settings.

Please help!!

Perhaps this will help
This option places the Min/Max buttons on the
Userform.....if you want it to Max all the time then look @ Option2

<pre/>
Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function DrawMenuBar Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long

Private Const GWL_STYLE As Long = (-16) 'Sets a new window style
Private Const WS_SYSMENU As Long = &H80000 'Windows style
Private Const WS_MINIMIZEBOX As Long = &H20000
Private Const WS_MAXIMIZEBOX As Long = &H10000
Private Const SW_SHOWMAXIMIZED = 3

Private Sub UserForm_Activate()
Dim lFormHandle As Long, lStyle As Long
'===========================================
'= Originally from Dax =
'= Modified with comments by Ivan F Moala =
'= 22/07/01 =
'===========================================

'Lets find the UserForm Handle the function below retrieves the handle
'to the top-level window whose class name ("ThunderDFrame" for Excel)
'and window name (me.caption or UserformName caption) match the specified strings.
lFormHandle = FindWindow("ThunderDFrame", Me.Caption)

'The GetWindowLong function retrieves information about the specified window.
'The function also retrieves the 32-bit (long) value at the specified offset
'into the extra window memory of a window.
lStyle = GetWindowLong(lFormHandle, GWL_STYLE)

'lStyle is the New window style so lets set it up with the following
lStyle = lStyle Or WS_SYSMENU 'SystemMenu
lStyle = lStyle Or WS_MINIMIZEBOX 'With MinimizeBox
lStyle = lStyle Or WS_MAXIMIZEBOX 'and MaximizeBox

'Now lets set up our New window the SetWindowLong function changes
'the attributes of the specified window , given as lFormHandle,
'GWL_STYLE = New windows style, and our Newly defined style = lStyle
SetWindowLong lFormHandle, GWL_STYLE, (lStyle)

'Remove >'< if you want to show form Maximised
'ShowWindow lFormHandle, SW_SHOWMAXIMIZED 'Shows Form Maximized

'The DrawMenuBar function redraws the menu bar of the specified window.
'We need this as we have changed the menu bar after Windows has created it.
'All we need is the Handle.
DrawMenuBar lFormHandle

End Sub
</pre>

OPTION2

<pre/>
Option Explicit

Private Const SM_CYSCREEN As Long = 1
Private Const SM_CXSCREEN As Long = 0
Const SM_CXFULLSCREEN = 16
Const SM_CYFULLSCREEN = 17
Const SM_CXMINTRACK = 34
Const SM_CYMINTRACK = 35
Const SM_CXMAXIMIZED = 61
Const SM_CYMAXIMIZED = 62
Private Declare Function GetSystemMetrics Lib "user32" _
(ByVal nIndex As Long) As Long


Private Sub UserForm_Activate()
Dim lScreenHeight As Long, lScreenWidth As Long
Dim lwinwidth, lWinHeight
lScreenHeight = GetSystemMetrics(SM_CYMAXIMIZED) '(SM_CYSCREEN)
lScreenWidth = GetSystemMetrics(SM_CXMAXIMIZED) '(SM_CXSCREEN)
'lwinwidth = ActiveWindow.PointsToScreenPixelsX(lScreenWidth)
'lWinHeight = ActiveWindow.PointsToScreenPixelsY(lScreenHeight)

Me.Left = 0
Me.Top = 0
Me.Height = lScreenHeight ' - 200
Me.Width = lScreenWidth 'lwinwidth 'lScreenWidth
End Sub



</pre>
 
Upvote 0
Ivan thank you the 1st option works fine if you remove the (') symbol were you say so, so the form maximses on being activated.
I found the 2nd option opened the form to big and therefore i needed to divide the scrrenwidth by 1.35 setting and screenheight setting by by 1.40 for it to reduce to screen size.

Other than that it worked fine.
 
Upvote 0
Ivan - Option 2 worked for me. Thanks! Is there a way to make the controls on the form re-position based on the users screen size?
 
Upvote 0
Try this in the Form module:

<pre>
Private Sub UserForm_Initialize()
With Application
Me.Top = .Top
Me.Left = .Left
Me.Height = .Height
Me.Width = .Width
End With
End Sub
</pre>

The code leaves the taskbar and menubar displayed (on my PC).

Regards,

Mike
 
Upvote 0
Ekim - Are you saying that code will re-position the controls on a UserForm based on the size of the users screen? It didn't work for me. ?
 
Upvote 0
Roger C
I was altered to your posts as when I set the original question I asked the board to notify me of any replies.
I have now, using Ivan cde set a form up with various controls and made it resize depending on the users screen setting it will also resize and reposition all controls depending on the new sise of the userform.
I hope this code helps, I must admit that I took some from other posts so this is not all my work!!!

This code is placed in the userform initialise section.
TextBox1 = Me.Width
TextBox2 = Me.Height

This code is placed in the userform activate section.

Dim ctl As Object
Dim i As Integer
FW = Val(Userform1.TextBox1)'This takes the width of the user before it maximises
FH = Val(userform1.TextBox2)''This takes the hieght of the user before it maximises
nfw = userform1.Width' This is the userforms new width
nfh = userform1.Height' This the userforms new height

On Error Resume Next
For Each ctl In userform1.Controls
If ctl.Left > 0 Then ctl.Left = (((ctl.Left)) * (nfw / FW))
If ctl.Top > 0 Then ctl.Top = (ctl.Top * (nfh / FH))

ctl.Width = (ctl.Width * (nfw / FW))


'ctl.Width = (ctl.Width * (nfw / FW))
ctl.Height = (ctl.Height * (nfh / FW))

ctl.Font.Name = "Arial"

If TypeName(ctl) = "ListBox" Then

ctl.Font.Size = ctl.Font.Size * ((nfh / FH)) - 2
Else

ctl.Font.Size = ctl.Font.Size * ((nfh / FH))
End If

Next ctl

I hope this helps.

Please let me know how you egt on!!


Steve
 
Upvote 0
Steve - I put the initialise code into a Private Sub UserForm_Initialize() and the other code in the UserForm_Activate Sub, but when I run it I get a 'Compile Error: Variable Not Defined on TextBox1'. Any idea what I may have done wrong?
 
Upvote 0
RogerC,

Sorry for not getting back to you ealier but I have been out of the office for a few days.

The reason for your error is that you have to have a textbox named textbox1 and another textbox named textbox2. The 2 textboxes are there to record the height and width of the form before it maximises, ie the height and width it is when your are working on it in vb editor.

Hope this helps,

Steve
 
Upvote 0

Forum statistics

Threads
1,214,388
Messages
6,119,229
Members
448,879
Latest member
VanGirl

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top