Excel VBA 窗体之去除窗体关闭按钮 实现代码
时间:2023-03-04 12:47:28
VBA的窗体缺少一般窗体的很多属性,不想许多语言窗体那样可以自由的设置窗体的样式,就连其默认的关闭按钮也不能设置为不显示,一般的处理方法是在窗体的关闭事件中添加代码来阻止窗体的关闭。那还不如就直接运用代码来把他去除掉(如图)。
附件下载:
操作如下:
◾在Excel的VBE窗口中插入一个用户窗体,将其命名为frmNoCloseIcon。然后再添加一个模块。在窗体和模块中添加后面所列代码。
◾在工作薄中的任意工作表中添加一窗体按钮控件,将指定其设置宏为btnShowfrmMaxMin_Click。其供示范之用。
具体代码:
"mdClose"模块代码
Sub btnShowfrmMaxMin_Click()
frmNoCloseIcon.Show
End Sub
"frmNoCloseIcon" 窗体代码
'****************************************
'---此模块演示了去除窗体关闭按钮---
'****************************************
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 GetWindowLong _
Lib "user32" _
Alias "GetWindowLongPtrA" ( _
ByVal hwnd As LongPtr, _
ByVal nIndex As Long) _
As LongPtr
Private Declare PtrSafe Function SetWindowLong _
Lib "user32" _
Alias "SetWindowLongPtrA" ( _
ByVal hwnd As LongPtr, _
ByVal nIndex As Long, _
ByVal dwNewLong As LongPtr) _
As LongPtr
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 GetWindowLong _
Lib "User32" _
Alias "GetWindowLongA" ( _
ByVal Hwnd As Long, _
ByVal nIndex As Long) _
As Long
'设置窗口样式位
Private Declare Function SetWindowLong _
Lib "User32" _
Alias "SetWindowLongA" ( _
ByVal Hwnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long) _
As Long
'重绘窗体标题栏
Private Declare Function DrawMenuBar _
Lib "User32" ( _
ByVal Hwnd As Long) _
As Long
#End If
#If Win64 Then '64位
Private FHwnd As LongPtr
Private FIstype As LongPtr
#Else
Private FHwnd As Long
Private FIstype As Long
#End If
'以下定义常数
Private Const GWL_STYLE = (-16) '窗口样式
Private Const WS_SYSMENU = &H80000 '系统菜单
Private Sub btnClose_Click()
Unload Me
End Sub
Private Sub btnReset_Click()
'取得窗口样式位
FIstype = GetWindowLong(FHwnd, GWL_STYLE)
'窗体样式位: 原样式和系统菜单
FIstype = FIstype Or WS_SYSMENU
'重设窗体样式位
SetWindowLong FHwnd, GWL_STYLE, FIstype
'重绘窗体标题栏
DrawMenuBar FHwnd
End Sub
'**************************************
'---------------主程序-----------------
'**************************************
Private Sub UserForm_Initialize()
'查找窗口句柄
FHwnd = FindWindow("ThunderDFrame", Me.Caption)
'取得窗口样式位
FIstype = GetWindowLong(FHwnd, GWL_STYLE)
'窗体样式位: 原样式和无系统菜单
FIstype = FIstype And Not WS_SYSMENU
'重设窗体样式位
SetWindowLong FHwnd, GWL_STYLE, FIstype
'重绘窗体标题栏
DrawMenuBar FHwnd
End Sub
Excel VBA 窗体之去除窗体关闭按钮 实现代码的下载地址:
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
WPS应用技巧—分秒提取一万个文件名
WPS怎么双面打印?WPS双面打印方法
![](https://img.aspxhome.com/file/2023/4/a164204_0s.jpg)
QQ云输入法可以在各种页面里面输入吗
![](https://img.aspxhome.com/file/2023/5/a318321_0s.jpg)
升级Win10更新四月版17134.48遭遇黑屏的解决方法
![](https://img.aspxhome.com/file/2023/28/a241544_0s.jpg)
在安装win10预览版9879时卡在进度18%的解决方法
![](https://img.aspxhome.com/file/2023/29/a252976_0s.jpg)
金山wps的云文档怎么删除文档
![](https://img.aspxhome.com/file/2023/3/a164123_0s.jpg)
win8.1系统安装程序提示错误0×80004005解决方法
![](https://img.aspxhome.com/file/2023/29/a261438_0s.png)
PPT演示技巧—让文字段落变得更整齐
如何自己重装系统Win10正版系统?
![](https://img.aspxhome.com/file/2023/25/a217489_0s.png)
如何把pdf快速有效转换成word的图文教程
![](https://img.aspxhome.com/file/2023/1/33711_0s.png)
Win10 1909夜间模式怎么设置?Win10 1909夜间模式设置教程
![](https://img.aspxhome.com/file/2023/26/a224330_0s.jpg)
免费提供会员的 Apple Music,为什么在国内很难普及?
![](https://img.aspxhome.com/file/2023/2/a197362_0s.jpg)
外星人电脑如何升级Win11?外星人电脑怎么更新系统Win11?
![](https://img.aspxhome.com/file/2023/2/a284056_0s.jpeg)
Win10如何禁用应用商店推广功能?Win10禁用应用商店推广功能的方法
![](https://img.aspxhome.com/file/2023/28/a242612_0s.jpg)
宝德笔记本电脑重装win7系统教程
![](https://img.aspxhome.com/file/2023/30/a268231_0s.png)
告诉你怎么还原电脑系统
![](https://img.aspxhome.com/file/2023/1/a276982_0s.png)
电脑版wps文档中的水印显示不全怎么办
![](https://img.aspxhome.com/file/2023/1/a183461_0s.png)
WPS文字编辑器怎么编辑图片
win7系统开机黑屏提示ntoskrnl.exe文件丢失如何解决?
![](https://img.aspxhome.com/file/2023/28/a243741_0s.jpg)
51模拟器中摇一摇功能如何使用?51模拟器中摇一摇功能使用教程
![](https://img.aspxhome.com/file/2023/8/a337877_0s.jpg)