View Single Post
  #27   Report Post  
Posted to microsoft.public.excel.programming
Tom Ogilvy Tom Ogilvy is offline
external usenet poster
 
Posts: 27,285
Default Minimize Maximize buttons

Bingo!

--
Regards,
Tom Ogilvy

"RB Smissaert" wrote in message
...
I suppose there is little point in minimizing a modal userform as you
can't do anything outside the form.

RBS

"RB Smissaert" wrote in message
...
Had a look at the file FormFun.xls, but even that doesn't handle this
(maximize and minimize with a modal form) properly, as you can see
when you tick modal and minimize the form.

RBS

"RB Smissaert" wrote in message
...
Would be interested if anybody could tell me how to make this work
properly with a normal modal userform.

RBS

"RB Smissaert" wrote in message
...
It looks it can actually work with a normal modal form as well, but as
you can
see it needs some adjustments as it doesn't work first time.

RBS

"Scott" wrote in message
...
RBS,

Great! It works perfect. Thanks a lot.

"RB Smissaert" wrote:

Figured out now how to let it minimize to the taskbar and this is all
the
code in my test Wb:

In the UserForm1 code:

Option Explicit
Private lFormHwnd As Long
Private Declare Function FindWindow _
Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, _
ByVal lpWindowName As String) _
As Long

Public Property Let propFormHwnd(lHwnd As Long)
lFormHwnd = lHwnd
End Property

Public Property Get propFormHwnd() As Long
propFormHwnd = lFormHwnd
End Property

Private Sub UserForm_Initialize()

Dim hwnd As Long

If Val(Application.Version) = 9 Then
hwnd = FindWindow("ThunderDFrame", Caption)
Else
hwnd = FindWindow("ThunderXFrame", Caption)
End If

Me.propFormHwnd = hwnd

End Sub

Private Sub UserForm_Resize()
SetMinimize 'leave out for floating minimize
End Sub

'=====================================

In the normal module:

Option Explicit
Public 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 Declare Function IsIconic Lib "user32" (ByVal hwnd As Long) _
As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As
Long)

Private Const WS_MAXIMIZEBOX As Long = &H10000
Private Const WS_MINIMIZEBOX As Long = &H20000
Private Const GWL_STYLE As Long = -16

Sub LoadForm()

Load UserForm1
UserForm1.Show 0
AddMinMax
SetMinimize True 'leave out for floating minimize

End Sub

Sub AddMinMax()

Dim hwnd As Long
Dim lngStyle As Long

hwnd = UserForm1.propFormHwnd

lngStyle = GetWindowLong(hwnd, GWL_STYLE)
lngStyle = lngStyle Or WS_MAXIMIZEBOX
lngStyle = lngStyle Or WS_MINIMIZEBOX

SetWindowLong hwnd, GWL_STYLE, lngStyle
DrawMenuBar hwnd

End Sub

Sub SetMinimize(Optional bNormal As Boolean)

Dim hwnd As Long

hwnd = UserForm1.propFormHwnd

If IsIconic(hwnd) Then
ShowWindow hwnd, 0
SetWindowLong hwnd, -20, &H40101
ShowWindow hwnd, 6
Else
If bNormal Then
ShowWindow hwnd, 0
SetWindowLong hwnd, -20, &H40101
ShowWindow hwnd, 1
End If
End If

End Sub


So, you now have the options to minimize to a small floating window
or to
the taskbar and this
depends on running SetMinimize in the 2 places as mentioned.
I think it now all should work.


RBS



"Scott" wrote in message
...
RBS,

I have a form called "UserForm1" under folder "Forms". I copied
your codes
to "Module2" in folder Modules. And I tried to run your codes,
which
brought
up UserForm1, but there were no Min/Max buttons. Here are the
codes:

Option Explicit
Public Declare Function GetWindowLong _
Lib "user32" Alias _
"GetWindowLongA" (ByVal hwnd As Long, _
ByVal nIndex As Long)
As
Long
Private Declare Function FindWindow _
Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, _
ByVal lpWindowName As String) 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 Declare Function IsIconic Lib "user32" (ByVal hwnd As Long)
As
Long
Private Const WS_MAXIMIZEBOX As Long = &H10000
Private Const WS_MINIMIZEBOX As Long = &H20000
Private Const GWL_STYLE As Long = (-16)
Private strFormType As String

Sub AddMinMax(strFormCaption As String)

Dim hwnd As Long
Dim lngStyle As Long

'this is not really necessary, vbNullString will do
If Val(Application.Version) = 9 Then
strFormType = "ThunderDFrame"
Else
strFormType = "ThunderXFrame"
End If

hwnd = FindWindow(strFormType, strFormCaption)
lngStyle = GetWindowLong(hwnd, GWL_STYLE)
lngStyle = lngStyle Or WS_MAXIMIZEBOX
lngStyle = lngStyle Or WS_MINIMIZEBOX

SetWindowLong hwnd, GWL_STYLE, lngStyle
DrawMenuBar hwnd

End Sub


Sub LoadForm()

Load UserForm1
UserForm1.Show 0

AddMinMax "UserForm1"

End Sub


"RB Smissaert" wrote:

Did you do:
UserForm1.Show 0

So did you load the form as a modeless form?
A modeless form means you can do things outside the form (like
doing
things
in the sheet) while the form is loaded.
It doesn't work with a normal modal form, not sure now if it can.

RBS

"Scott" wrote in message
...
Thanks for the help.

I copied your codes to my file, but I still did not get the
buttons on
my
UserForm1.

"RB Smissaert" wrote:

This is simplest code to add those buttons:

Option Explicit
Public Declare Function GetWindowLong _
Lib "user32" Alias _
"GetWindowLongA" (ByVal hwnd As
Long, _
ByVal nIndex As
Long)
As
Long
Private Declare Function FindWindow _
Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, _
ByVal lpWindowName As String) 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 Declare Function IsIconic Lib "user32" (ByVal hwnd As
Long) As
Long
Private Const WS_MAXIMIZEBOX As Long = &H10000
Private Const WS_MINIMIZEBOX As Long = &H20000
Private Const GWL_STYLE As Long = (-16)
Private strFormType As String

Sub AddMinMax(strFormCaption As String)

Dim hwnd As Long
Dim lngStyle As Long

'this is not really necessary, vbNullString will do
If Val(Application.Version) = 9 Then
strFormType = "ThunderDFrame"
Else
strFormType = "ThunderXFrame"
End If

hwnd = FindWindow(strFormType, strFormCaption)
lngStyle = GetWindowLong(hwnd, GWL_STYLE)
lngStyle = lngStyle Or WS_MAXIMIZEBOX
lngStyle = lngStyle Or WS_MINIMIZEBOX

SetWindowLong hwnd, GWL_STYLE, lngStyle
DrawMenuBar hwnd

End Sub


Sub LoadForm()

Load UserForm1
UserForm1.Show 0

AddMinMax "Userform1"

End Sub


RBS


"Scott" wrote in message
...
Tom,

Thanks very much. The FormFun is very interesting. But the
codes are
too
complicated for me to adopt. I just want to have the bottons
on the
form
once
the form is initialized.

Anyway, I apprecite your help.