User Form

Y

youu917

can anyone tell me how to make the user form in excel VBA more similar
to VB version?
As in: VB's user form has maximize and minimize button beside
close[X],However, excel user form hasn't, vb can add in pulldown menu
easily, but excel user form can't.

Another question is: How can i view the full picture by using
Loadpicture function if the picture size is greater than the
picturebox's size?

thank you.
 
M

Matthew Pfluger

To add the min or max button to the userform, put this code in a standard
code module:

'///////////////////////////////////////'
'/ /'
'/ API calls to add the Minimize and /'
'/ Restore buttons to the Title Bar /'
'/ of a VBA UserForm. /'
'/ /'
'///////////////////////////////////////'
'
'Written: Jan. 30, 2007
'Author: Leith Ross


'Constants for ShowWindow (nCmdShow)
Const SW_HIDDEN As Long = 0
Const SW_NORMAL As Long = 1
Const SW_MINIMIZED As Long = 2
Const SW_MAXIMIZED As Long = 3
Const SW_NOTACTIVE As Long = 4
Const SW_UNHIDDEN As Long = 5
Const SW_MINWITHFOCUS As Long = 6
Const SW_MINNOTACTIVE As Long = 7
Const SW_RESTORE As Long = 9

'Constants for GetWindow
Const GW_HWNDFIRST As Long = &H0
Const GW_HWNDLAST As Long = &H1
Const GW_HWNDNEXT As Long = &H2
Const GW_HWNDPREV As Long = &H3
Const GW_OWNER As Long = &H4
Const GW_CHILD As Long = &H5

'Window Style constants
Const WS_DISABLE As Long = 0
Const WS_MAXIMIZEBOX As Long = &H10000
Const WS_MINIMIZEBOX As Long = &H20000
Const WS_THICKFRAME As Long = &H40000 'Style to add a sizable Frame
Const WS_SYSMENU As Long = &H80000
Const WS_ENABLE As Long = &HFFFFFFFF

'Get Window Long constants
Const GWL_HINSTANCE As Long = (-6)
Const GWL_HWNDPARENT As Long = (-8)
Const GWL_ID As Long = (-12)
Const GWL_STYLE As Long = (-16)
Const GWL_EXSTYLE As Long = (-20)

Private Declare Function GetWindowLong _
Lib "user32.dll" _
Alias "GetWindowLongA" _
(ByVal hWnd As Long, ByVal nIndex As Long) As
Long

Private Declare Function SetWindowLong _
Lib "user32.dll" _
Alias "SetWindowLongA" _
(ByVal hWnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long

'Function to Change how Window is Displayed
Private Declare Function ShowWindow _
Lib "user32.dll" _
(ByVal hWnd As Long, _
ByVal nCmdShow As Long) As Long

'Returns the Window Handle of the Active Window
Public Declare Function GetActiveWindow _
Lib "user32.dll" () As Long

'Redraw the Icons on the Window's Title Bar
Private Declare Function DrawMenuBar _
Lib "user32.dll" _
(ByVal hWnd As Long) As Long

Public Sub MinimizeWindow(Optional ByVal Window_Handle As Long, _
Optional ByVal With_Focus As Boolean)

Dim RetVal

If With_Focus = True Then
RetVal = ShowWindow(Window_Handle, SW_MINWITHFOCUS)
Else
RetVal = ShowWindow(Window_Handle, SW_MINNOTACTIVE)
End If

End Sub

Public Sub RestoreWindow(Optional ByVal Window_Handle As Long)

Dim RetVal

RetVal = ShowWindow(Window_Handle, SW_NORMAL)

End Sub


Public Sub AddMinBox(Optional Window_Handle As Long)

Dim hWnd As Long
Dim BitMask As Long
Dim WindowStyle As Long

If Window_Handle = 0 Then
hWnd = GetActiveWindow()
Else
hWnd = Window_Handle
End If

WindowStyle = GetWindowLong(hWnd, GWL_STYLE)
BitMask = WindowStyle Or WS_MINIMIZEBOX

Call SetWindowLong(hWnd, GWL_STYLE, BitMask)
Call DrawMenuBar(hWnd)

End Sub

Public Sub AddMaxBox(Optional Window_Handle As Long)

Dim hWnd As Long
Dim BitMask As Long
Dim WindowStyle As Long

If Window_Handle = 0 Then
hWnd = GetActiveWindow()
Else
hWnd = Window_Handle
End If

WindowStyle = GetWindowLong(hWnd, GWL_STYLE)
BitMask = WindowStyle Or WS_MAXIMIZEBOX

Call SetWindowLong(hWnd, GWL_STYLE, BitMask)
Call DrawMenuBar(hWnd)

End Sub

You will also need to add these lines to the Userform code:

Private Sub UserForm_Activate()
' Add min and max buttons to userform
Call AddMinBox
Call AddMaxBox
End Sub

Credit for this code goes to Leigh Ross, whose code I copied from this
forum.

As for a pull down menu, I'm not sure what you mean. You can add a combobox
control to the form, and that can be configured as a drop-down control.

Regarding the picture, set the image control's Autosize property to TRUE
before loading the picture:

Me.Controls(yourControlHere).Autosize = True

Hope this helps,
Matthew Pfluger

can anyone tell me how to make the user form in excel VBA more similar
to VB version?
As in: VB's user form has maximize and minimize button beside
close[X],However, excel user form hasn't, vb can add in pulldown menu
easily, but excel user form can't.

Another question is: How can i view the full picture by using
Loadpicture function if the picture size is greater than the
picturebox's size?

thank you.
 
Y

youu917

To add the min or max button to the userform, put this code in a standard
code module:

'///////////////////////////////////////'
'/ /'
'/ API calls to add the Minimize and /'
'/ Restore buttons to the Title Bar /'
'/ of a VBA UserForm. /'
'/ /'
'///////////////////////////////////////'
'
'Written: Jan. 30, 2007
'Author: Leith Ross

'Constants for ShowWindow (nCmdShow)
Const SW_HIDDEN As Long = 0
Const SW_NORMAL As Long = 1
Const SW_MINIMIZED As Long = 2
Const SW_MAXIMIZED As Long = 3
Const SW_NOTACTIVE As Long = 4
Const SW_UNHIDDEN As Long = 5
Const SW_MINWITHFOCUS As Long = 6
Const SW_MINNOTACTIVE As Long = 7
Const SW_RESTORE As Long = 9

'Constants for GetWindow
Const GW_HWNDFIRST As Long = &H0
Const GW_HWNDLAST As Long = &H1
Const GW_HWNDNEXT As Long = &H2
Const GW_HWNDPREV As Long = &H3
Const GW_OWNER As Long = &H4
Const GW_CHILD As Long = &H5

'Window Style constants
Const WS_DISABLE As Long = 0
Const WS_MAXIMIZEBOX As Long = &H10000
Const WS_MINIMIZEBOX As Long = &H20000
Const WS_THICKFRAME As Long = &H40000 'Style to add a sizableFrame
Const WS_SYSMENU As Long = &H80000
Const WS_ENABLE As Long = &HFFFFFFFF

'Get Window Long constants
Const GWL_HINSTANCE As Long = (-6)
Const GWL_HWNDPARENT As Long = (-8)
Const GWL_ID As Long = (-12)
Const GWL_STYLE As Long = (-16)
Const GWL_EXSTYLE As Long = (-20)

Private Declare Function GetWindowLong _
Lib "user32.dll" _
Alias "GetWindowLongA" _
(ByVal hWnd As Long, ByVal nIndex As Long) As
Long

Private Declare Function SetWindowLong _
Lib "user32.dll" _
Alias "SetWindowLongA" _
(ByVal hWnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long

'Function to Change how Window is Displayed
Private Declare Function ShowWindow _
Lib "user32.dll" _
(ByVal hWnd As Long, _
ByVal nCmdShow As Long) As Long

'Returns the Window Handle of the Active Window
Public Declare Function GetActiveWindow _
Lib "user32.dll" () As Long

'Redraw the Icons on the Window's Title Bar
Private Declare Function DrawMenuBar _
Lib "user32.dll" _
(ByVal hWnd As Long) As Long

Public Sub MinimizeWindow(Optional ByVal Window_Handle As Long, _
Optional ByVal With_Focus As Boolean)

Dim RetVal

If With_Focus = True Then
RetVal = ShowWindow(Window_Handle, SW_MINWITHFOCUS)
Else
RetVal = ShowWindow(Window_Handle, SW_MINNOTACTIVE)
End If

End Sub

Public Sub RestoreWindow(Optional ByVal Window_Handle As Long)

Dim RetVal

RetVal = ShowWindow(Window_Handle, SW_NORMAL)

End Sub

Public Sub AddMinBox(Optional Window_Handle As Long)

Dim hWnd As Long
Dim BitMask As Long
Dim WindowStyle As Long

If Window_Handle = 0 Then
hWnd = GetActiveWindow()
Else
hWnd = Window_Handle
End If

WindowStyle = GetWindowLong(hWnd, GWL_STYLE)
BitMask = WindowStyle Or WS_MINIMIZEBOX

Call SetWindowLong(hWnd, GWL_STYLE, BitMask)
Call DrawMenuBar(hWnd)

End Sub

Public Sub AddMaxBox(Optional Window_Handle As Long)

Dim hWnd As Long
Dim BitMask As Long
Dim WindowStyle As Long

If Window_Handle = 0 Then
hWnd = GetActiveWindow()
Else
hWnd = Window_Handle
End If

WindowStyle = GetWindowLong(hWnd, GWL_STYLE)
BitMask = WindowStyle Or WS_MAXIMIZEBOX

Call SetWindowLong(hWnd, GWL_STYLE, BitMask)
Call DrawMenuBar(hWnd)

End Sub

You will also need to add these lines to the Userform code:

Private Sub UserForm_Activate()
' Add min and max buttons to userform
Call AddMinBox
Call AddMaxBox
End Sub

Credit for this code goes to Leigh Ross, whose code I copied from this
forum.

As for a pull down menu, I'm not sure what you mean. You can add a combobox
control to the form, and that can be configured as a drop-down control.

Regarding the picture, set the image control's Autosize property to TRUE
before loading the picture:

Me.Controls(yourControlHere).Autosize = True

Hope this helps,
Matthew Pfluger



can anyone tell me how to make the user form in excel VBA more similar
to VB version?
As in: VB's user form has maximize and minimize button beside
close[X],However, excel user form hasn't, vb can add in pulldown menu
easily, but excel user form can't.
Another question is: How can i view the full picture by using
Loadpicture function if the picture size is greater than the
picturebox's size?
thank you.- Òþ²Ø±»ÒýÓÃÎÄ×Ö -

- ÏÔʾÒýÓõÄÎÄ×Ö -

thank you matthew.
regarding the picturebox control, if i set Autosize=True, the picture
will be shown completely. However, what i want is a smaller picture
which is shown fully in the small picturebox. If Autosize=true, the
picture still the large picture regardless the size of picturebox.
 
S

SteveM

To add the min or max button to the userform, put this code in a standard
code module:
'///////////////////////////////////////'
'/ /'
'/ API calls to add the Minimize and /'
'/ Restore buttons to the Title Bar /'
'/ of a VBA UserForm. /'
'/ /'
'///////////////////////////////////////'
'
'Written: Jan. 30, 2007
'Author: Leith Ross
'Constants for ShowWindow (nCmdShow)
Const SW_HIDDEN As Long = 0
Const SW_NORMAL As Long = 1
Const SW_MINIMIZED As Long = 2
Const SW_MAXIMIZED As Long = 3
Const SW_NOTACTIVE As Long = 4
Const SW_UNHIDDEN As Long = 5
Const SW_MINWITHFOCUS As Long = 6
Const SW_MINNOTACTIVE As Long = 7
Const SW_RESTORE As Long = 9
'Constants for GetWindow
Const GW_HWNDFIRST As Long = &H0
Const GW_HWNDLAST As Long = &H1
Const GW_HWNDNEXT As Long = &H2
Const GW_HWNDPREV As Long = &H3
Const GW_OWNER As Long = &H4
Const GW_CHILD As Long = &H5
'Window Style constants
Const WS_DISABLE As Long = 0
Const WS_MAXIMIZEBOX As Long = &H10000
Const WS_MINIMIZEBOX As Long = &H20000
Const WS_THICKFRAME As Long = &H40000 'Style to add a sizable Frame
Const WS_SYSMENU As Long = &H80000
Const WS_ENABLE As Long = &HFFFFFFFF
'Get Window Long constants
Const GWL_HINSTANCE As Long = (-6)
Const GWL_HWNDPARENT As Long = (-8)
Const GWL_ID As Long = (-12)
Const GWL_STYLE As Long = (-16)
Const GWL_EXSTYLE As Long = (-20)
Private Declare Function GetWindowLong _
Lib "user32.dll" _
Alias "GetWindowLongA" _
(ByVal hWnd As Long, ByVal nIndex As Long)As
Long
Private Declare Function SetWindowLong _
Lib "user32.dll" _
Alias "SetWindowLongA" _
(ByVal hWnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long
'Function to Change how Window is Displayed
Private Declare Function ShowWindow _
Lib "user32.dll" _
(ByVal hWnd As Long, _
ByVal nCmdShow As Long) As Long
'Returns the Window Handle of the Active Window
Public Declare Function GetActiveWindow _
Lib "user32.dll" () As Long
'Redraw the Icons on the Window's Title Bar
Private Declare Function DrawMenuBar _
Lib "user32.dll" _
(ByVal hWnd As Long) As Long
Public Sub MinimizeWindow(Optional ByVal Window_Handle As Long, _
Optional ByVal With_Focus As Boolean)
Dim RetVal
If With_Focus = True Then
RetVal = ShowWindow(Window_Handle, SW_MINWITHFOCUS)
Else
RetVal = ShowWindow(Window_Handle, SW_MINNOTACTIVE)
End If
Public Sub RestoreWindow(Optional ByVal Window_Handle As Long)
Dim RetVal
RetVal = ShowWindow(Window_Handle, SW_NORMAL)
Public Sub AddMinBox(Optional Window_Handle As Long)
Dim hWnd As Long
Dim BitMask As Long
Dim WindowStyle As Long
If Window_Handle = 0 Then
hWnd = GetActiveWindow()
Else
hWnd = Window_Handle
End If
WindowStyle = GetWindowLong(hWnd, GWL_STYLE)
BitMask = WindowStyle Or WS_MINIMIZEBOX
Call SetWindowLong(hWnd, GWL_STYLE, BitMask)
Call DrawMenuBar(hWnd)
Public Sub AddMaxBox(Optional Window_Handle As Long)
Dim hWnd As Long
Dim BitMask As Long
Dim WindowStyle As Long
If Window_Handle = 0 Then
hWnd = GetActiveWindow()
Else
hWnd = Window_Handle
End If
WindowStyle = GetWindowLong(hWnd, GWL_STYLE)
BitMask = WindowStyle Or WS_MAXIMIZEBOX
Call SetWindowLong(hWnd, GWL_STYLE, BitMask)
Call DrawMenuBar(hWnd)
You will also need to add these lines to the Userform code:
Private Sub UserForm_Activate()
' Add min and max buttons to userform
Call AddMinBox
Call AddMaxBox
End Sub
Credit for this code goes to Leigh Ross, whose code I copied from this
forum.
As for a pull down menu, I'm not sure what you mean. You can add a combobox
control to the form, and that can be configured as a drop-down control.
Regarding the picture, set the image control's Autosize property to TRUE
before loading the picture:
Me.Controls(yourControlHere).Autosize = True
Hope this helps,
Matthew Pfluger
can anyone tell me how to make the user form in excel VBA more similar
to VB version?
As in: VB's user form has maximize and minimize button beside
close[X],However, excel user form hasn't, vb can add in pulldown menu
easily, but excel user form can't.
Another question is: How can i view the full picture by using
Loadpicture function if the picture size is greater than the
picturebox's size?
thank you.- Òþ²Ø±»ÒýÓÃÎÄ×Ö -
- ÏÔʾÒýÓõÄÎÄ×Ö -

thank you matthew.
regarding the picturebox control, if i set Autosize=True, the picture
will be shown completely. However, what i want is a smaller picture
which is shown fully in the small picturebox. If Autosize=true, the
picture still the large picture regardless the size of picturebox.

Set Autosize = False. It's PictureSizeMode = Stretch or Zoom that
fits the picture into the size of the box you defined. Pick the one
you want.

SteveM
 
Y

youu917

To add the min or max button to the userform, put this code in a standard
code module:
'///////////////////////////////////////'
'/ /'
'/ API calls to add the Minimize and /'
'/ Restore buttons to the Title Bar /'
'/ of a VBA UserForm. /'
'/ /'
'///////////////////////////////////////'
'
'Written: Jan. 30, 2007
'Author: Leith Ross
'Constants for ShowWindow (nCmdShow)
Const SW_HIDDEN As Long = 0
Const SW_NORMAL As Long = 1
Const SW_MINIMIZED As Long = 2
Const SW_MAXIMIZED As Long = 3
Const SW_NOTACTIVE As Long = 4
Const SW_UNHIDDEN As Long = 5
Const SW_MINWITHFOCUS As Long = 6
Const SW_MINNOTACTIVE As Long = 7
Const SW_RESTORE As Long = 9
'Constants for GetWindow
Const GW_HWNDFIRST As Long = &H0
Const GW_HWNDLAST As Long = &H1
Const GW_HWNDNEXT As Long = &H2
Const GW_HWNDPREV As Long = &H3
Const GW_OWNER As Long = &H4
Const GW_CHILD As Long = &H5
'Window Style constants
Const WS_DISABLE As Long = 0
Const WS_MAXIMIZEBOX As Long = &H10000
Const WS_MINIMIZEBOX As Long = &H20000
Const WS_THICKFRAME As Long = &H40000 'Style to add a sizable Frame
Const WS_SYSMENU As Long = &H80000
Const WS_ENABLE As Long = &HFFFFFFFF
'Get Window Long constants
Const GWL_HINSTANCE As Long = (-6)
Const GWL_HWNDPARENT As Long = (-8)
Const GWL_ID As Long = (-12)
Const GWL_STYLE As Long = (-16)
Const GWL_EXSTYLE As Long = (-20)
Private Declare Function GetWindowLong _
Lib "user32.dll" _
Alias "GetWindowLongA" _
(ByVal hWnd As Long, ByVal nIndex As Long) As
Long
Private Declare Function SetWindowLong _
Lib "user32.dll" _
Alias "SetWindowLongA" _
(ByVal hWnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long
'Function to Change how Window is Displayed
Private Declare Function ShowWindow _
Lib "user32.dll" _
(ByVal hWnd As Long, _
ByVal nCmdShow As Long) As Long
'Returns the Window Handle of the Active Window
Public Declare Function GetActiveWindow _
Lib "user32.dll" () As Long
'Redraw the Icons on the Window's Title Bar
Private Declare Function DrawMenuBar _
Lib "user32.dll" _
(ByVal hWnd As Long) As Long
Public Sub MinimizeWindow(Optional ByVal Window_Handle As Long, _
Optional ByVal With_Focus As Boolean)
Dim RetVal
If With_Focus = True Then
RetVal = ShowWindow(Window_Handle, SW_MINWITHFOCUS)
Else
RetVal = ShowWindow(Window_Handle, SW_MINNOTACTIVE)
End If
End Sub
Public Sub RestoreWindow(Optional ByVal Window_Handle As Long)
Dim RetVal
RetVal = ShowWindow(Window_Handle, SW_NORMAL)
End Sub
Public Sub AddMinBox(Optional Window_Handle As Long)
Dim hWnd As Long
Dim BitMask As Long
Dim WindowStyle As Long
If Window_Handle = 0 Then
hWnd = GetActiveWindow()
Else
hWnd = Window_Handle
End If
WindowStyle = GetWindowLong(hWnd, GWL_STYLE)
BitMask = WindowStyle Or WS_MINIMIZEBOX
Call SetWindowLong(hWnd, GWL_STYLE, BitMask)
Call DrawMenuBar(hWnd)
End Sub
Public Sub AddMaxBox(Optional Window_Handle As Long)
Dim hWnd As Long
Dim BitMask As Long
Dim WindowStyle As Long
If Window_Handle = 0 Then
hWnd = GetActiveWindow()
Else
hWnd = Window_Handle
End If
WindowStyle = GetWindowLong(hWnd, GWL_STYLE)
BitMask = WindowStyle Or WS_MAXIMIZEBOX
Call SetWindowLong(hWnd, GWL_STYLE, BitMask)
Call DrawMenuBar(hWnd)
End Sub
You will also need to add these lines to the Userform code:
Private Sub UserForm_Activate()
' Add min and max buttons to userform
Call AddMinBox
Call AddMaxBox
End Sub
Credit for this code goes to Leigh Ross, whose code I copied from this
forum.
As for a pull down menu, I'm not sure what you mean. You can add a combobox
control to the form, and that can be configured as a drop-down control..
Regarding the picture, set the image control's Autosize property to TRUE
before loading the picture:
Me.Controls(yourControlHere).Autosize = True
Hope this helps,
Matthew Pfluger
:
can anyone tell me how to make the user form in excel VBA more similar
to VB version?
As in: VB's user form has maximize and minimize button beside
close[X],However, excel user form hasn't, vb can add in pulldown menu
easily, but excel user form can't.
Another question is: How can i view the full picture by using
Loadpicture function if the picture size is greater than the
picturebox's size?
thank you.- Òþ²Ø±»ÒýÓÃÎÄ×Ö -
- ÏÔʾÒýÓõÄÎÄ×Ö -
thank you matthew.
regarding the picturebox control, if i set Autosize=True, the picture
will be shown completely. However, what i want is a smaller picture
which is shown fully in the small picturebox. If Autosize=true, the
picture still the large picture regardless the size of picturebox.

Set Autosize = False. It's PictureSizeMode = Stretch or Zoom that
fits the picture into the size of the box you defined. Pick the one
you want.

SteveM- Òþ²Ø±»ÒýÓÃÎÄ×Ö -

- ÏÔʾÒýÓõÄÎÄ×Ö -

sorry, i tried but still the same.
 
P

pswanie

i had the same problem and the zoom/stretch worked for me. i used stretch
and then size the picture box

On 1月20æ—¥, 上åˆ9æ—¶30分, Matthew Pfluger
To add the min or max button to the userform, put this code in a standard
code module:
'///////////////////////////////////////'
'/ /'
'/ API calls to add the Minimize and /'
'/ Restore buttons to the Title Bar /'
'/ of a VBA UserForm. /'
'/ /'
'///////////////////////////////////////'
'
'Written: Jan. 30, 2007
'Author: Leith Ross
'Constants for ShowWindow (nCmdShow)
Const SW_HIDDEN As Long = 0
Const SW_NORMAL As Long = 1
Const SW_MINIMIZED As Long = 2
Const SW_MAXIMIZED As Long = 3
Const SW_NOTACTIVE As Long = 4
Const SW_UNHIDDEN As Long = 5
Const SW_MINWITHFOCUS As Long = 6
Const SW_MINNOTACTIVE As Long = 7
Const SW_RESTORE As Long = 9
'Constants for GetWindow
Const GW_HWNDFIRST As Long = &H0
Const GW_HWNDLAST As Long = &H1
Const GW_HWNDNEXT As Long = &H2
Const GW_HWNDPREV As Long = &H3
Const GW_OWNER As Long = &H4
Const GW_CHILD As Long = &H5
'Window Style constants
Const WS_DISABLE As Long = 0
Const WS_MAXIMIZEBOX As Long = &H10000
Const WS_MINIMIZEBOX As Long = &H20000
Const WS_THICKFRAME As Long = &H40000 'Style to add a sizable Frame
Const WS_SYSMENU As Long = &H80000
Const WS_ENABLE As Long = &HFFFFFFFF
'Get Window Long constants
Const GWL_HINSTANCE As Long = (-6)
Const GWL_HWNDPARENT As Long = (-8)
Const GWL_ID As Long = (-12)
Const GWL_STYLE As Long = (-16)
Const GWL_EXSTYLE As Long = (-20)
Private Declare Function GetWindowLong _
Lib "user32.dll" _
Alias "GetWindowLongA" _
(ByVal hWnd As Long, ByVal nIndex As Long) As
Long
Private Declare Function SetWindowLong _
Lib "user32.dll" _
Alias "SetWindowLongA" _
(ByVal hWnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long
'Function to Change how Window is Displayed
Private Declare Function ShowWindow _
Lib "user32.dll" _
(ByVal hWnd As Long, _
ByVal nCmdShow As Long) As Long
'Returns the Window Handle of the Active Window
Public Declare Function GetActiveWindow _
Lib "user32.dll" () As Long
'Redraw the Icons on the Window's Title Bar
Private Declare Function DrawMenuBar _
Lib "user32.dll" _
(ByVal hWnd As Long) As Long
Public Sub MinimizeWindow(Optional ByVal Window_Handle As Long, _
Optional ByVal With_Focus As Boolean)
Dim RetVal
If With_Focus = True Then
RetVal = ShowWindow(Window_Handle, SW_MINWITHFOCUS)
Else
RetVal = ShowWindow(Window_Handle, SW_MINNOTACTIVE)
End If
Public Sub RestoreWindow(Optional ByVal Window_Handle As Long)
Dim RetVal
RetVal = ShowWindow(Window_Handle, SW_NORMAL)
Public Sub AddMinBox(Optional Window_Handle As Long)
Dim hWnd As Long
Dim BitMask As Long
Dim WindowStyle As Long
If Window_Handle = 0 Then
hWnd = GetActiveWindow()
Else
hWnd = Window_Handle
End If
WindowStyle = GetWindowLong(hWnd, GWL_STYLE)
BitMask = WindowStyle Or WS_MINIMIZEBOX
Call SetWindowLong(hWnd, GWL_STYLE, BitMask)
Call DrawMenuBar(hWnd)
Public Sub AddMaxBox(Optional Window_Handle As Long)
Dim hWnd As Long
Dim BitMask As Long
Dim WindowStyle As Long
If Window_Handle = 0 Then
hWnd = GetActiveWindow()
Else
hWnd = Window_Handle
End If
WindowStyle = GetWindowLong(hWnd, GWL_STYLE)
BitMask = WindowStyle Or WS_MAXIMIZEBOX
Call SetWindowLong(hWnd, GWL_STYLE, BitMask)
Call DrawMenuBar(hWnd)
You will also need to add these lines to the Userform code:
Private Sub UserForm_Activate()
' Add min and max buttons to userform
Call AddMinBox
Call AddMaxBox
End Sub
Credit for this code goes to Leigh Ross, whose code I copied from this
forum.
As for a pull down menu, I'm not sure what you mean. You can add a combobox
control to the form, and that can be configured as a drop-down control..
Regarding the picture, set the image control's Autosize property to TRUE
before loading the picture:
Me.Controls(yourControlHere).Autosize = True
Hope this helps,
Matthew Pfluger
:
can anyone tell me how to make the user form in excel VBA more similar
to VB version?
As in: VB's user form has maximize and minimize button beside
close[X],However, excel user form hasn't, vb can add in pulldown menu
easily, but excel user form can't.
Another question is: How can i view the full picture by using
Loadpicture function if the picture size is greater than the
picturebox's size?
thank you.- éšè—被引用文字 -
- 显示引用的文字 -
thank you matthew.
regarding the picturebox control, if i set Autosize=True, the picture
will be shown completely. However, what i want is a smaller picture
which is shown fully in the small picturebox. If Autosize=true, the
picture still the large picture regardless the size of picturebox.

Set Autosize = False. It's PictureSizeMode = Stretch or Zoom that
fits the picture into the size of the box you defined. Pick the one
you want.

SteveM- éšè—被引用文字 -

- 显示引用的文字 -

sorry, i tried but still the same.
 
P

pswanie

how do i get it to open maximized?




Matthew Pfluger said:
To add the min or max button to the userform, put this code in a standard
code module:

'///////////////////////////////////////'
'/ /'
'/ API calls to add the Minimize and /'
'/ Restore buttons to the Title Bar /'
'/ of a VBA UserForm. /'
'/ /'
'///////////////////////////////////////'
'
'Written: Jan. 30, 2007
'Author: Leith Ross


'Constants for ShowWindow (nCmdShow)
Const SW_HIDDEN As Long = 0
Const SW_NORMAL As Long = 1
Const SW_MINIMIZED As Long = 2
Const SW_MAXIMIZED As Long = 3
Const SW_NOTACTIVE As Long = 4
Const SW_UNHIDDEN As Long = 5
Const SW_MINWITHFOCUS As Long = 6
Const SW_MINNOTACTIVE As Long = 7
Const SW_RESTORE As Long = 9

'Constants for GetWindow
Const GW_HWNDFIRST As Long = &H0
Const GW_HWNDLAST As Long = &H1
Const GW_HWNDNEXT As Long = &H2
Const GW_HWNDPREV As Long = &H3
Const GW_OWNER As Long = &H4
Const GW_CHILD As Long = &H5

'Window Style constants
Const WS_DISABLE As Long = 0
Const WS_MAXIMIZEBOX As Long = &H10000
Const WS_MINIMIZEBOX As Long = &H20000
Const WS_THICKFRAME As Long = &H40000 'Style to add a sizable Frame
Const WS_SYSMENU As Long = &H80000
Const WS_ENABLE As Long = &HFFFFFFFF

'Get Window Long constants
Const GWL_HINSTANCE As Long = (-6)
Const GWL_HWNDPARENT As Long = (-8)
Const GWL_ID As Long = (-12)
Const GWL_STYLE As Long = (-16)
Const GWL_EXSTYLE As Long = (-20)

Private Declare Function GetWindowLong _
Lib "user32.dll" _
Alias "GetWindowLongA" _
(ByVal hWnd As Long, ByVal nIndex As Long) As
Long

Private Declare Function SetWindowLong _
Lib "user32.dll" _
Alias "SetWindowLongA" _
(ByVal hWnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long

'Function to Change how Window is Displayed
Private Declare Function ShowWindow _
Lib "user32.dll" _
(ByVal hWnd As Long, _
ByVal nCmdShow As Long) As Long

'Returns the Window Handle of the Active Window
Public Declare Function GetActiveWindow _
Lib "user32.dll" () As Long

'Redraw the Icons on the Window's Title Bar
Private Declare Function DrawMenuBar _
Lib "user32.dll" _
(ByVal hWnd As Long) As Long

Public Sub MinimizeWindow(Optional ByVal Window_Handle As Long, _
Optional ByVal With_Focus As Boolean)

Dim RetVal

If With_Focus = True Then
RetVal = ShowWindow(Window_Handle, SW_MINWITHFOCUS)
Else
RetVal = ShowWindow(Window_Handle, SW_MINNOTACTIVE)
End If

End Sub

Public Sub RestoreWindow(Optional ByVal Window_Handle As Long)

Dim RetVal

RetVal = ShowWindow(Window_Handle, SW_NORMAL)

End Sub


Public Sub AddMinBox(Optional Window_Handle As Long)

Dim hWnd As Long
Dim BitMask As Long
Dim WindowStyle As Long

If Window_Handle = 0 Then
hWnd = GetActiveWindow()
Else
hWnd = Window_Handle
End If

WindowStyle = GetWindowLong(hWnd, GWL_STYLE)
BitMask = WindowStyle Or WS_MINIMIZEBOX

Call SetWindowLong(hWnd, GWL_STYLE, BitMask)
Call DrawMenuBar(hWnd)

End Sub

Public Sub AddMaxBox(Optional Window_Handle As Long)

Dim hWnd As Long
Dim BitMask As Long
Dim WindowStyle As Long

If Window_Handle = 0 Then
hWnd = GetActiveWindow()
Else
hWnd = Window_Handle
End If

WindowStyle = GetWindowLong(hWnd, GWL_STYLE)
BitMask = WindowStyle Or WS_MAXIMIZEBOX

Call SetWindowLong(hWnd, GWL_STYLE, BitMask)
Call DrawMenuBar(hWnd)

End Sub

You will also need to add these lines to the Userform code:

Private Sub UserForm_Activate()
' Add min and max buttons to userform
Call AddMinBox
Call AddMaxBox
End Sub

Credit for this code goes to Leigh Ross, whose code I copied from this
forum.

As for a pull down menu, I'm not sure what you mean. You can add a combobox
control to the form, and that can be configured as a drop-down control.

Regarding the picture, set the image control's Autosize property to TRUE
before loading the picture:

Me.Controls(yourControlHere).Autosize = True

Hope this helps,
Matthew Pfluger

can anyone tell me how to make the user form in excel VBA more similar
to VB version?
As in: VB's user form has maximize and minimize button beside
close[X],However, excel user form hasn't, vb can add in pulldown menu
easily, but excel user form can't.

Another question is: How can i view the full picture by using
Loadpicture function if the picture size is greater than the
picturebox's size?

thank you.
 
M

Matthew Pfluger

Sorry, I'm not sure about that one. You might be able to get the screen size
resolution and change the form size that way, but I've never needed to do
that.

Matthew Pfluger
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top