PDA

View Full Version : How to min/max user form



sekoa
12-13-2007, 06:08 AM
hi,
Ii'm a structural eng and i'm working on structural aplication in excel but i have problem how to minimize ora maximize user form. Plese look at attached file - tehere's example of my form. the form works like a simple toolbar - chose the icon (comand button in user form) and automaticly go to the right sheet in excell (there will be structural calculations - at the moment sheets are empty). It runs in 2 ways:
1. click comand button "uruchom aplikacje" in "element walcowany" sheet
2. press ctrl+u in other sheets

does anybody know how to write a code which will minimize form after pressing any button ? i mean not to unload form but minimize it and be able to to work in excell sheet


thanks

PaSha
12-13-2007, 06:21 AM
There is an option of userforms it says UserForm1.Hide

user form also have a hide method. when you invoke zhis method, the userform dissapepears, but it remains loaded in memory, so your code can still access the various properties of the controls.

Bob Phillips
12-13-2007, 07:25 AM
This code will add minimize and maximize icon to the form. Put it in the form code module



'Windows API calls
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 ShowWindow Lib "user32" ( _
ByVal hWnd As Long, _
ByVal nCmdShow As Long) As Long
Private Declare Function EnableWindow Lib "user32" ( _
ByVal hWnd As Long, _
ByVal fEnable As Long) As Long
Private Declare Function DrawMenuBar Lib "user32" ( _
ByVal hWnd As Long) As Long
Private Declare Function SetFocus Lib "user32" ( _
ByVal hWnd As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
ByVal hWnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Integer, _
ByVal lParam As Long) As Long
Private Declare Function LockWindowUpdate Lib "user32" ( _
ByVal hWndLock As Long) As Long

'Window styles
Private Const GWL_STYLE As Long = (-16) 'The offset of a window's style
Private Const GWL_EXSTYLE As Long = (-20) 'The offset of a window's extended style
Private Const WS_CAPTION As Long = &HC00000 'Style to add a titlebar
Private Const WS_SYSMENU As Long = &H80000 'Style to add a system menu
Private Const WS_MINIMIZEBOX As Long = &H20000 'Style to add a Minimize box on the title bar
Private Const WS_MAXIMIZEBOX As Long = &H10000 'Style to add a Maximize box to the title bar
Private Const WS_EX_APPWINDOW As Long = &H40000 'Application Window: shown on taskbar
Private Const WS_EX_TOOLWINDOW As Long = &H80 'Tool Window: small titlebar

'Hide or show a window
Private Const SW_HIDE As Long = 0
Private Const SW_SHOW As Long = 5

'Windows messages
Private Const WM_SETICON = &H80

Private hWnd As Long

Private Sub Userform_Initialize()
Dim lStyle As Long


'Get the userform's window handle
If Val(Application.Version) < 9 Then

hWnd = FindWindow("ThunderXFrame", Me.Caption) 'XL97
Else

hWnd = FindWindow("ThunderDFrame", Me.Caption) 'XL2000+
End If

SendMessage hWnd, WM_SETICON, True, 0
SendMessage hWnd, WM_SETICON, False, 0

LockWindowUpdate hWnd
EnableWindow FindWindow("XLMAIN", Application.Caption), True
ShowWindow hWnd, SW_HIDE 'Hide the form

lStyle = GetWindowLong(hWnd, GWL_STYLE) Or WS_CAPTION Or WS_SYSMENU Or WS_MINIMIZEBOX Or WS_MAXIMIZEBOX
SetWindowLong hWnd, GWL_STYLE, lStyle

lStyle = GetWindowLong(hWnd, GWL_EXSTYLE) Or WS_EX_APPWINDOW And Not WS_EX_TOOLWINDOW
SetWindowLong hWnd, GWL_EXSTYLE, lStyle

DrawMenuBar hWnd
SetFocus hWnd

ShowWindow hWnd, SW_SHOW 'Reshow the userform

LockWindowUpdate 0& 'Unfreeze the form

'Set the Excel window's enablement to the correct choice
EnableWindow FindWindow("XLMAIN", Application.Caption), 1

End Sub

blackie42
12-13-2007, 09:18 AM
I used the following code in a userform module to minimize

Private Sub ToggleButton11_Click()
If ToggleButton11.Value = True Then
AddEntry.Height = AddEntry.Height * 0.13 ' adjust to
Else
AddEntry.Height = dHeight
End If
End Sub

I put the togglebutton at the top of the form and altho it doesn't minimize to tray it does allow you to see most of the spreadsheet. The form did cover most of the screen.

Might help?

blackie42
12-13-2007, 09:20 AM
AddEntry being the name of the form of course

regards

blackie42
12-13-2007, 09:45 AM
Sorry - would need this bit too

Private Sub UserForm_Initialize()
dHeight = Me.Height
end sub