我们已经有了一系列怎样增强 VBA 窗体的文章,VBA 的窗体和一般正常的窗体区别很大,比如就不可以通过用鼠标拖动窗体的边框,我们今天就讲讲怎样给 VBA 窗体添加这个功能。

Office Excel VBA 窗体之拖动边界改变大小窗体 实现代码
 

操作如下:
◾在Excel 的VBE窗口中插入一个用户窗体,将其命名为 frmNotEnabledCloseIcon。然后再添加一个模块。在窗体和模块中添加后面所列代码。
◾在工作薄中的工作表中添加一窗体按钮控件,指定其设置宏 ShowNotEnabledCloseIconForm, 其供示范之用.

 

具体代码:

 

"frmThickFram" 窗体代码

‘********************************
‘—此模块演示怎样删除窗体标题栏—

www.office26.com
‘********************************
‘以下声明API函数
#If Win64 Then ’64位
‘取得窗体样式位
Private Declare PtrSafe Function GetWindowLong _
Lib "user32" _
Alias "GetWindowLongPtrA" ( _
ByVal Hwnd As LongPtr, _
ByVal nIndex As Long) _
As LongPtr
‘查找窗口
Private Declare PtrSafe Function FindWindow _
Lib "user32" _
Alias "FindWindowA" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String) _
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
‘取得窗体样式位
Private Declare Function GetWindowLong _
Lib "user32" _
Alias "GetWindowLongA" ( _
ByVal hwnd As Long, _
ByVal nIndex As Long) _
As Long
‘查找窗口
Private Declare Function FindWindow _
Lib "user32" _
Alias "FindWindowA" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String) _
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 hWndForm As LongPtr
Private FIstype As LongPtr
#Else
Private hWndForm As Long
Private FIstype As Long
#End If
‘以下定义常数和变量
Private Const GWL_STYLE = (-16) ‘窗口样式
Private Const WS_THICKFRAME = &H40000 ‘可更改大小
‘—关闭按钮—
Private Sub btnClose_Click()
Unload Me
End Sub
‘—窗体初始化—
Private Sub UserForm_Initialize()
On Error Resume Next
‘查找窗体句柄
hWndForm = FindWindow("ThunderDFrame", Me.Caption)
‘取得窗体样式
FIstype = GetWindowLong(hWndForm, GWL_STYLE)
‘窗体样式:原样式和可拖动边框改变大小
FIstype = FIstype Or WS_THICKFRAME
‘重设窗体样式
SetWindowLong hWndForm, GWL_STYLE, FIstype
‘重绘窗体标题栏
DrawMenuBar hWndForm
End Sub

"mdThickfram" 模块代码

Option Explicit ‘
此过程为工作表内按钮调用
Sub ShowThickFramForm()
frmThickFram.Show
End Sub

1文件名称 1下载链接
1禁用窗体关闭按钮.zip http://pan.baidu.com/s/1i39T4X3

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注