首页 > Excel专区 > Excel函数 >

Excel VBA 窗体之去除窗体关闭按钮 实现代码

Excel函数 2023-01-08

VBA的窗体缺少一般窗体的很多属性,不想许多语言窗体那样可以自由的设置窗体的样式,就连其默认的关闭按钮也不能设置为不显示,一般的处理方法是在窗体的关闭事件中添加代码来阻止窗体的关闭。那还不如就直接运用代码来把他去除掉(如图)。

Excel 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 窗体之去除窗体关闭按钮 实现代码的下载地址:
  • 本地下载


  • Copyright © 2016-2023 office学习教程网 office.tqzw.net.cn. All Rights Reserved.