在VBA中我们有时需要一些特殊形状的窗体来美化我们的程序,比如说几个几何形状的组合样式的窗体。那我们就来作一个同心圆形状的窗体: 本示例主要运用 函数来定制化Excel中的用户窗体,使其显示特殊形状

Office Excel VBA 窗体之特殊形状窗体 几何形状组合窗体 实现代码

附件下载:

点击链接从百度网盘下载

操作如下:

◾在Excel的VBE窗口中插入一个用户窗体,将其命名为EspecialForm。然后再添加一个模块。然后在窗体和模块中添加后面所列代码。

◾在工作薄中的任意工作表中添加一窗体按钮控件,将指定其设置宏为ShowForm。其供示范之用

具体代码:

"mdEspecial"模块代码

Sub btnShowEspecial_Click()
frmEspecial.Show
End Sub

"frmEspecial" 窗体代码

Option Explicit
‘**********************************
‘—此模块主要是创建了一个圆环窗体—
‘**********************************
‘以下声明API函数
#If Win64 Then ’64位
‘视情况向和窗体发送消息
Private Declare PtrSafe Function SendMessage _
Lib "user32" _
Alias "SendMessageA" ( _
ByVal Hwnd As LongPtr, _
ByVal wMsg As Long, _
ByVal wParam As LongPtr, _
lParam As Any) _
As LongPtr
‘创建一个内切于矩形的椭圆
Private Declare PtrSafe Function CreateEllipticRgn _
Lib "gdi32" ( _
ByVal X1 As Long, _
ByVal Y1 As Long, _
ByVal X2 As Long, _
ByVal Y2 As Long) _
As LongPtr
‘以特定的方式合并区域
Private Declare PtrSafe Function CombineRgn _
Lib "gdi32" ( _
ByVal hDestRgn As LongPtr, _
ByVal hSrcRgn1 As LongPtr, _
ByVal hSrcRgn2 As LongPtr, _
ByVal nCombineMode As Long) _
As Long
‘给窗体设置区域,而舍弃此区域外的其他区域
Private Declare PtrSafe Function SetWindowRgn _
Lib "user32" ( _
ByVal Hwnd As LongPtr, _
ByVal hRgn As LongPtr, _
ByVal bRedraw As Long) _
As Long
‘查找窗口
Private Declare PtrSafe Function FindWindow _
Lib "user32" _
Alias "FindWindowA" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String) _
As LongPtr
‘释放鼠标
Private Declare PtrSafe Function ReleaseCapture _
Lib "user32" () _
As Long
#Else
‘视情况向和窗体发送消息
Private Declare Function SendMessage _
Lib "user32" _
Alias "SendMessageA" ( _
ByVal Hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) _
As Long
‘创建一个内切于矩形的椭圆
Private Declare Function CreateEllipticRgn _
Lib "gdi32" ( _
ByVal X1 As Long, _
ByVal Y1 As Long, _
ByVal X2 As Long, _
ByVal Y2 As Long) _
As Long
‘以特定的方式合并区域
Private Declare Function CombineRgn _
Lib "gdi32" ( _
ByVal hDestRgn As Long, _
ByVal hSrcRgn1 As Long, _
ByVal hSrcRgn2 As Long, _
ByVal nCombineMode As Long) _
As Long
‘给窗体设置区域,而舍弃此区域外的其他区域
Private Declare Function SetWindowRgn _
Lib "user32" ( _
ByVal Hwnd As Long, _
ByVal hRgn As Long, _
ByVal bRedraw 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 ReleaseCapture _
Lib "user32" () _
As Long
#End If
‘声明常数及变量
Private Const WM_SYSCOMMAND = &H112
Private Const SC_MOVE_MOUSE = &HF012&
Private Const RGN_XOR = 3 ‘两个源区域并集之外的部分
#If Win64 Then ’64位
Dim FHwnd As LongPtr
Dim FRgn1 As LongPtr
Dim FRgn2 As LongPtr
#Else
Dim FHwnd As Long
Dim FRgn1 As Long
Dim FRgn2 As Long
#End If
‘窗体双击
Private Sub UserForm_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Unload Me
End Sub
‘窗体初始化
Private Sub UserForm_Initialize()
FRgn1 = CreateEllipticRgn(10, 40, 200, 230) ‘创建一个圆
FRgn2 = CreateEllipticRgn(30, 60, 180, 210) ‘创建一个圆
CombineRgn FRgn1, FRgn1, FRgn2, RGN_XOR ‘合并两个圆,取其不相交的部分
FHwnd = FindWindow(vbNullString, Me.Caption) ‘查找窗体句柄
SetWindowRgn FHwnd, FRgn1, 1 ‘设置窗体区域,一个圆环
End Sub
‘窗体鼠标按下
Private Sub UserForm_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
ReleaseCapture ‘释放鼠标
SendMessage FHwnd, WM_SYSCOMMAND, SC_MOVE_MOUSE, 0
End Sub

发表评论

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