我不知道如何制作世界上最简单的可调整大小的用户窗体。我在不同的论坛帖子上看到的都是可怕的庞然大物(巨大,因为宇宙库做了太多的事情)。但我需要一个简单的一键解决方案,我希望它存在。此时我有这个代码:
Dim myForm As UserForm1
Set myForm = New UserForm1
myForm.Caption = "Attributes"
myForm.Show
我有
UserForm_Initialize()
可以做一些额外的工作。可怕(不合理?)的是默认情况下表单不可调整大小。
这里有一个关于如何使用户窗体拖动和调整大小的简单指南。
http://www.mrexcel.com/forum/excel-questions/558649-userform-movable-ressized.html
这是转录的解决方案
https://www.mrexcel.com/board/threads/resize-a-userform.485489/
我测试过,有效
首先将这些声明添加到您的标题中
'Declaration for form resize
Private Declare Function GetActiveWindow Lib "user32.dll" () As Long
Private Declare Function SetLastError Lib "kernel32.dll" (ByVal dwErrCode As Long) As Long
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
将此子项添加到您的表单中
Private Sub MakeFormResizable()
'Written: August 02, 2010
'Author: Leith Ross
'Summary: Makes the UserForm resizable by dragging one of the sides. Place a call
' to the macro MakeFormResizable in the UserForm'
'from https://www.mrexcel.com/board/threads/resize-a-userform.485489/
Dim lStyle As Long
Dim hWnd As Long
Dim RetVal
Const WS_THICKFRAME = &H40000
Const GWL_STYLE As Long = (-16)
hWnd = GetActiveWindow
'Get the basic window style
lStyle = GetWindowLong(hWnd, GWL_STYLE) Or WS_THICKFRAME
'Set the basic window styles
RetVal = SetWindowLong(hWnd, GWL_STYLE, lStyle)
'Clear any previous API error codes
SetLastError 0
'Did the style change?
If RetVal = 0 Then MsgBox "Unable to make UserForm Resizable."
End Sub
最后从你的 Userform_Activate 中调用这个子程序
Private Sub UserForm_Activate()
MakeFormResizable
End Sub
更新我的声明以适应 64 位后,对我来说非常有用。见下图:
#If VBA7 Then
Private Declare PtrSafe Function SetLastError _
Lib "kernel32.dll" _
(ByVal dwErrCode As Long) _
As Long
Public Declare PtrSafe Function GetActiveWindow _
Lib "user32.dll" () As Long
Private Declare PtrSafe Function GetWindowLong _
Lib "user32.dll" Alias "GetWindowLongA" _
(ByVal hWnd As Long, _
ByVal nIndex As Long) _
As Long
Private Declare PtrSafe Function SetWindowLong _
Lib "user32.dll" Alias "SetWindowLongA" _
(ByVal hWnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long) _
As Long
#Else
Private Declare Function SetLastError _
Lib "kernel32.dll" _
(ByVal dwErrCode As Long) _
As Long
Public Declare Function GetActiveWindow _
Lib "user32.dll" () As Long
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
#End If