我们前面讲了怎样去除 VBA 窗体的关闭按钮,其实个人感觉没有这个关闭按钮会使整个窗体感觉有点怪,但有时候又想让他不可用,变得不可点击,那该怎么实现呢? 好,我们马上就来试一试看看。
操作如下:
?在Excel 的VBE窗口中插入一个用户窗体,将其命名为 frmNotEnabledCloseIcon。然后再添加一个模块。在窗体和模块中添加后面所列代码。
?在工作薄中的工作表中添加一窗体按钮控件,指定其设置宏 ShowNotEnabledCloseIconForm, 其供示范之用.
具体代码:
"frmNotEnabledCloseIcon" 窗体代码
Option Explicit
'****************************************
'---此模块演示了禁用窗体关闭按钮---
'****************************************
'以下声明API函数
#If Win64 Then '64位
'查找窗口
Private Declare PtrSafe Function FindWindow _
Lib "user32" _
Alias "FindWindowA" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String) _
As LongPtr
'取得窗体系统菜单
Private Declare PtrSafe Function GetSystemMenu _
Lib "user32" ( _
ByVal hwnd As LongPtr, _
ByVal bRevert As Long) _
As LongPtr
'删除菜单项
Private Declare PtrSafe Function DeleteMenu _
Lib "user32" ( _
ByVal hMenu As LongPtr, _
ByVal nPosition As Long, _
ByVal wFlags As Long) _
As Long
'重绘窗体标题栏
Private Declare PtrSafe Function DrawMenuBar _
Lib "user32" ( _
ByVal hwnd As LongPtr) _
As Long
#Else '32位
'查找窗口
Private Declare Function FindWindow _
Lib "User32" _
Alias "FindWindowA" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String) _
As Long
'取得窗体系统菜单
Private Declare Function GetSystemMenu _
Lib "User32" ( _
ByVal Hwnd As Long, _
ByVal bRevert As Long) _
As Long
'删除菜单项
Private Declare Function DeleteMenu _
Lib "User32" ( _
ByVal HMenu As Long, _
ByVal nPosition As Long, _
ByVal wFlags As Long) _
As Long
'重绘窗体标题栏
Private Declare Function DrawMenuBar _
Lib "User32" ( _
ByVal Hwnd As Long) _
As Long
#End If
'以下定义常数
Private Const MF_BYCOMMAND = &H0&
Private Const SC_CLOSE = &HF060
'以下下定义变量
#If Win64 Then '64位
Private FHwnd As LongPtr
Private hMenu As LongPtr
#Else
Private FHwnd As Long
Private hMenu As Long
#End If
'关闭按钮点击
Private Sub BtCancel_Click()
Unload Me
End Sub
'恢复按钮点击
Private Sub BtResume_Click()
'恢复原菜单
hMenu = GetSystemMenu(FHwnd, 1)
'重绘窗体标题栏
DrawMenuBar FHwnd
End Sub
'**************************************
'---------------主程序-----------------
'**************************************
Private Sub UserForm_Initialize()
'查找窗口句柄
FHwnd = FindWindow("ThunderDFrame", Me.Caption)
'取得窗体系统菜单
hMenu = GetSystemMenu(FHwnd, 0)
'删除系统菜单中关闭菜单
DeleteMenu hMenu, SC_CLOSE, MF_BYCOMMAND
'重绘窗体标题栏
DrawMenuBar FHwnd
End Sub
"mdNotEnabledCloseIcon" 模块代码
Option Explicit'
此过程为工作表内按钮调用
Sub ShowNotEnabledCloseIconForm()
frmNotEnabledCloseIcon.Show
End Sub
1文件名称 | 1下载链接 |
---|---|
1禁用窗体关闭按钮.zip | http://pan.baidu.com/s/1ntG6Odj |
……