首页 > 解决方案 > 使用用户窗体调整列表框大小

问题描述

我有一个调整用户sameCustomerForm窗体窗口大小的代码:

Private Declare Function GetForegroundWindow 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

Private Const WS_THICKFRAME As Long = &H40000
Private Const GWL_STYLE As Long = -16

Public Sub FormResizable()

Dim lStyle As Long
Dim hWnd As Long
Dim RetVal

hWnd = GetForegroundWindow

lStyle = GetWindowLong(hWnd, GWL_STYLE) Or WS_THICKFRAME
RetVal = SetWindowLong(hWnd, GWL_STYLE, lStyle)

End Sub

在用户窗体sameCustomerForm代码中:

Private Sub UserForm_Activate()
    Call FormResizable
End Sub

然后我lstSelector在 userform 上有一个 Listbox sameCustomerForm。在设计 WinForms 应用程序并将对象锚定到窗体的侧面时,我想坚持lstSelector用户窗体的侧面,就像您在 Visual Studio 中所做的那样。

任何想法如何在 Excel 用户表单中实现?


编辑:

这几乎可以工作了。现在除了底部之外,所有东西都按比例调整大小。.Height = sameCustomerForm.Height / 1如果.Height = sameCustomerForm.Height / 2底部滚动条太高,我在调整窗口大小时缺少底部滚动条。有一些粘底的问题...

Private Sub UserForm_Resize()

With sameCustomerForm.lstSelector
    .Width = sameCustomerForm.Width - 12
    .Height = sameCustomerForm.Height / 2
    .Top = 54
    .Left = 0
End With

End Sub

编辑2:

这个成功了:

Private Sub UserForm_Resize()

With sameCustomerForm.lstSelector
    .Width = sameCustomerForm.Width - 12
    .Height = sameCustomerForm.InsideHeight - 54
    .Top = 54
    .Left = 0
End With

End Sub

标签: excelvba

解决方案


您应该使用您的用户表单指标来实现这一点。
下面是一个使用按钮将列表框靠边停靠的示例:
在此处输入图像描述

更新
重构了要使用的代码InsideHeightInsideWidth用户窗体的属性。

这是按钮的代码:

Private Sub bottomButton_Click()
' dock bottom
With myForm.lstSelector
    .Width = myForm.InsideWidth
    .Height = myForm.InsideHeight / 3
    .Top = myForm.InsideHeight - .Height
    .Left = 0
End With
End Sub

Private Sub leftButton_Click()
' dock left
With myForm.lstSelector
    .Width = myForm.InsideWidth / 3 ' just for example
    .Top = 0
    .Left = 0
    .Height = myForm.InsideHeight
End With
End Sub

Private Sub rightButton_Click()
' dock right
With myForm.lstSelector
    .Width = myForm.InsideWidth / 3 ' just for example
    .Top = 0
    .Left = myForm.InsideWidth - .Width
    .Height = myForm.InsideHeight
End With

End Sub

Private Sub stickButton_Click()
' stick
With myForm.lstSelector
    .Width = myForm.InsideWidth ' just for example
    .Top = 0
    .Left = 0
    .Height = myForm.InsideHeight
End With

End Sub

Private Sub topButton_Click()
'  dock up
With myForm.lstSelector
    .Width = myForm.InsideWidth ' just for example
    .Top = 0
    .Left = 0
    .Height = myForm.InsideHeight / 3
End With

End Sub

这是如何将其停靠到不同侧面的一般想法。如果您实现了可调整大小的 UserForm(手动或通过工作流) - 想法仍然相同,但您应该考虑将这些处理程序放入UserForm_Resize事件。

如果您需要将其锚定在其他地方 - 您必须计算位置比例。


推荐阅读