vba 如何从excel用户表单中删除close(x)选项?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13150186/
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-11 18:18:25  来源:igfitidea点击:

How to remove the close(x) option from excel userform?

excel-vbavbaexcel

提问by user441978

How to remove the close( X ) option from excel userform ? I got the below link which already discussed. http://www.excelforum.com/excel-programming-vba-macros/694008-remove-user-form-borders.html

如何从 excel 用户表单中删除 close( X ) 选项?我得到了下面已经讨论过的链接。 http://www.excelforum.com/excel-programming-vba-macros/694008-remove-user-form-b​​orders.html

But i'm getting what they given, please help me....

但是我得到了他们给的东西,请帮帮我....

回答by Reafidy

Firstly: make sure you include at least one obvious method to close the form!!

首先:确保您至少包含一种明显的方法来关闭表单!

Instead of bothering the end user with message box's after they click close, I would hide the close altogether:

我不会在单击关闭后用消息框打扰最终用户,而是将关闭完全隐藏:

'//Find the userform's Window
Private Declare Function FindWindow Lib "user32" _
        Alias "FindWindowA" ( _
        ByVal lpClassName As String, _
        ByVal lpWindowName As String) As Long

'//Get the current window style
Private Declare Function GetWindowLong Lib "user32" _
        Alias "GetWindowLongA" ( _
        ByVal hWnd As Long, _
        ByVal nIndex As Long) As Long

'//Set the new window style
Private Declare Function SetWindowLong Lib "user32" _
        Alias "SetWindowLongA" ( _
        ByVal hWnd As Long, _
        ByVal nIndex As Long, _
        ByVal dwNewLong As Long) As Long

Const GWL_STYLE = -16
Const WS_SYSMENU = &H80000

Private Sub UserForm_Initialize()
   Dim hWnd As Long, lStyle As Long

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

   '//Get the current window style and turn off the Close button
   lStyle = GetWindowLong(hWnd, GWL_STYLE)
   SetWindowLong hWnd, GWL_STYLE, (lStyle And Not WS_SYSMENU)
End Sub

If you want to prevent closing via ALT-F4 then use this as well:

如果你想阻止通过 ALT-F4 关闭,那么也可以使用它:

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
      If CloseMode = vbFormControlMenu Then
        Cancel = True  
    End If
End Sub