vba Excel 窗口和用户窗体最小化和最大化功能

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18275705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 16:26:01  来源:igfitidea点击:

Excel Window & UserForm Minimize and Maximize Functions

windowsexcelvbaminimize

提问by Glynbeard

I currently have a userform open on top of an excel instance and it is set so you can't interact with anything except for the userform. Since there is no way to minimize this program, I created a 'Minimize' button on the userform that, when clicked, hides the userform and shrinks the excel window as expected.

我目前在一个 excel 实例上打开了一个用户表单,它被设置为除了用户表单之外你不能与任何东西交互。由于无法最小化此程序,因此我在用户窗体上创建了一个“最小化”按钮,单击该按钮后,将隐藏用户窗体并按预期缩小 excel 窗口。

However, when I click on the minimized excel application to restore the window, I would like both the userform to appear again and the excel window appear behind it as before (right now only the excel window appears).

但是,当我单击最小化的 excel 应用程序来恢复窗口时,我希望用户窗体再次出现,并且 excel 窗口像以前一样出现在它后面(现在只出现 excel 窗口)。

Is there any function or trigger I can listen for which will allow me to show my userform again when the excel instance is clicked and restored? This is how I am currently minimizing everything:

是否有任何功能或触发器可以让我在单击并恢复 excel 实例时再次显示我的用户表单?这就是我目前最小化一切的方式:

Private Sub CommandButton15_Click()
Me.Hide
Application.WindowState = xlMinimized
End Sub

回答by David Zemens

When you display the form, you are likely doing something like:

当您显示表单时,您可能会执行以下操作:

UserForm.Show

UserForm.Show

The Showmethod takes an optional argument, whether to display the form Modalor Modeless. Modal display is default, and does not allow interaction with the worksheet/workbook objects. Instead, when you display the form, do:

Show方法采用可选参数,是显示表单Modal还是Modeless. 模态显示是默认的,并且不允许与工作表/工作簿对象交互。相反,当您显示表单时,请执行以下操作:

UserForm.Show vbModeless

UserForm.Show vbModeless

This will allow the user to interact with the worksheet/workbook, which alleviates the need for your custom button and you will not need to do Me.Hide. Minimizing the Application will minimize the UserForm. Maximizing the Application will re-display the workbook andthe userform.

这将允许用户与工作表/工作簿进行交互,从而减少对自定义按钮的需求,而您无需执行Me.Hide. 最小化应用程序将最小化用户窗体。最大化应用程序将重新显示工作簿用户窗体。

If you must use the vbModaldisplay of the UserForm (and in many applications this is a deliberate requirement to prevent user from interacting with the Workbook/Worksheets), let me know. There may be some events or application events that could better trap the minimize/maximize.

如果您必须使用vbModal用户窗体的显示(并且在许多应用程序中,这是防止用户与工作簿/工作表交互的故意要求),请告诉我。可能有一些事件或应用程序事件可以更好地捕获最小化/最大化。

UPDATE

更新

Alternatively, you could do something like this. This approach Hides the Excel Application, and shrinks the size of the UserForm, then resizes it when you click back on the user form and shows the Excel Application again.

或者,你可以做这样的事情。这种方法隐藏 Excel 应用程序,并缩小用户窗体的大小,然后在您单击用户窗体并再次显示 Excel 应用程序时调整其大小。

Private Sub CommandButton15_Click()
'Hide Excel and minimize the UserForm
    Application.Visible = False
    Me.Height = 10
    Me.Width = 10

End Sub

Private Sub UserForm_Click()
'Show Excel and resize the UserForm
    Application.Visible = True
    Me.Height = 180
    Me.Width = 240
End Sub

Private Sub UserForm_Terminate()
'Ensure that the Application is visible and the form resized
    UserForm_Click
End Sub