excel VBA 窗体 如何禁用窗体关闭按钮
时间:2022-07-15 15:06:50
我们前面讲了怎样去除 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 |
excel VBA 窗体 如何禁用窗体关闭按钮的下载地址:
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Excel中要经常输入重复的文字,无需复制粘贴,可以使用下拉列表
![](https://img.aspxhome.com/file/2023/5/a140545_0s.jpg)
win10如何设置开机密码?win10设置开机密码教程
![](https://img.aspxhome.com/file/2023/6/48516_0s.jpg)
Win10开始菜单变成灰色怎么处理?开始菜单变灰解决方法
![](https://img.aspxhome.com/file/2023/4/51754_0s.jpg)
如何在excel表格里用函数制作日历
![](https://img.aspxhome.com/file/2023/7/a161617_0s.jpg)
打印Word文档出现"文档字体错误"提示
![](https://img.aspxhome.com/file/2023/5/19355_0s.jpg)
excel2007乘法函数的使用方法
在Excel中如何筛选出不规则的数据?
![](https://img.aspxhome.com/file/2023/7/a142417_0s.png)
Excel中无法直接输入数字怎么办?
![](https://img.aspxhome.com/file/2023/9/a153339_0s.jpg)
为什么新款HomePod只支持Wi-Fi 4?
![](https://img.aspxhome.com/file/2023/7/44717_0s.png)
PDF文档怎么设置目录跳转链接?
![](https://img.aspxhome.com/file/2023/2/15812_0s.png)
看看VBA的Sub过程和Function过程
![](https://img.aspxhome.com/file/2023/2/a141952_0s.png)
windows10桌面图标怎么随意摆放?
![](https://img.aspxhome.com/file/2023/0/53100_0s.png)
Excel表格默认的回车键方向怎么更换?
![](https://img.aspxhome.com/file/2023/0/40300_0s.png)
如何套用Excel表格格式,使你自己做的表格更加高大上
![](https://img.aspxhome.com/file/2023/8/a155428_0s.gif)
怎样在Word2007中快速生成指定段落数量的文字
![](https://img.aspxhome.com/file/2023/0/29590_0s.jpg)
如何在Word2010中进行专业型公式和线性公式之间切换
![](https://img.aspxhome.com/file/2023/5/29595_0s.jpg)
Win10电脑怎么关闭系统提示“安全警告提示”?
![](https://img.aspxhome.com/file/2023/2/49112_0s.jpg)
Excel2013设置数据有效性方法
![](https://img.aspxhome.com/file/2023/8/a143578_0s.jpg)
无需截图工具 将Excel单元格变图片
![](https://img.aspxhome.com/file/2023/6/38986_0s.jpg)