|
楼主 |
发表于 2009-5-6 07:47
|
显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
第8部分 控件与用户窗体
技巧139 无标题栏和边框的窗体
如果希望制作无标题栏和边框的窗体,那么可以使用API函数。
在VBE窗口中单击菜单“插入”→“用户窗体”,双击窗体,在其代码窗口中输入下面的代码:- #001 Private Declare Function DrawMenuBar Lib "user32" (ByVal Hwnd As Long) As Long
- #002 Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal Hwnd As Long, ByVal nIndex As Long) As Long
- #003 Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal Hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
- #004 Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
- #005 Private Const GWL_STYLE As Long = (-16)
- #006 Private Const GWL_EXSTYLE = (-20)
- #007 Private Const WS_CAPTION As Long = &HC00000
- #008 Private Const WS_EX_DLGMODALFRAME = &H1&
- #009 Private Sub UserForm_Initialize()
- #010 Dim IStyle As Long
- #011 Dim Hwnd As Long
- #012 If Val(Application.Version) < 9 Then
- #013 Hwnd = FindWindow("ThunderXFrame", Me.Caption)
- #014 Else
- #015 Hwnd = FindWindow("ThunderDFrame", Me.Caption)
- #016 End If
- #017 IStyle = GetWindowLong(Hwnd, GWL_STYLE)
- #018 IStyle = IStyle And Not WS_CAPTION
- #019 SetWindowLong Hwnd, GWL_STYLE, IStyle
- #020 DrawMenuBar Hwnd
- #021 IStyle = GetWindowLong(Hwnd, GWL_EXSTYLE) And Not WS_EX_DLGMODALFRAME
- #022 SetWindowLong Hwnd, GWL_EXSTYLE, IStyle
- #023 End Sub
- #024 Private Sub UserForm_Click()
- #025 Unload Me
- #026 End Sub
复制代码 代码解析:
窗体初始化时使用API函数去除其标题栏和边框。
第1行到第8行代码,API函数的声明。
第12行到第16行代码,获取窗口句柄。
第17行到第20行代码,去除窗体标题栏。
第21、22行代码,去除窗体边框。
第24行到第26行代码,窗体的单击事件,单击窗体后关闭该窗体。
窗体运行后如图所示,单击后关闭该窗体。
|
|