Excel VBA UserForm窗体置顶

VBA窗口置顶功能

  1. 新建模块
  2. 插入下述代码
  3. 在UserForm中调用SetWinTopOrNot()
  4. 整理了一波,原创不易,收藏加评论,你的支持就是我的动力。
'  Forms
'->Module
'  ClassModules
'Function: VBA窗口置顶
Const HWND_TOPMOST = -1
Const HWND_NOTOPMOST = -2
Const SWP_NOSIZE = &H1
Const SWP_NOMOVE = &H2
Const SWP_NOACTIVATE = &H10
Const SWP_SHOWWINDOW = &H40
Private Declare Sub SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long)
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
'[In] 1: TopWindows 0: Cancel TopWindows
'[Out] Msg of Result.
Public Function SetWinTopOrNot(ByVal Top As Boolean) As String
    'HWND FindWindowA(
    '  [in, optional] LPCSTR lpClassName,
    '  [in, optional] LPCSTR lpWindowName
    ');
    'Type: HWND
    'If the function succeeds, the return value is a handle to the window that has the specified class name and window name.
    Dim hwnd1
    hwnd1 = FindWindow("ThunderDFrame", vbNullString)
    'hwnd1 = FindWindow(vbNullString, Me.Caption)  'Recommand
    If Top = True Then
        SetWindowPos hwnd1, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE Or SWP_NOMOVE
        SetWinTopOrNot "WindowIsPlacedTheTop Done!"
    Else
        SetWindowPos hwnd1, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE Or SWP_NOMOVE
        SetWinTopOrNot "WindowIsPlacedTheTop Cancel!"
    End If
End Function

VS2010 VB Form 置顶功能

  • Form初始化事件是 Active
    在这里插入图片描述
'Declare Area
'  Forms
'->Module
'  ClassModules
'Function: VBA窗口置顶
Const HWND_TOPMOST = -1
Const HWND_NOTOPMOST = -2
Const SWP_NOSIZE = &H1
Const SWP_NOMOVE = &H2
Const SWP_NOACTIVATE = &H10
Const SWP_SHOWWINDOW = &H40
Private Declare Sub SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long)
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
'[In] 1: TopWindows 0: Cancel TopWindows
'[Out] Msg of Result.
Public Sub SetWinTopOrNot(ByVal Top As Boolean)
    Dim hwnd As Long
    hwnd = FindWindow(vbNullString, Me.Text)
    If Top = True Then
        SetWindowPos(hwnd:=hwnd, hWndInsertAfter:=HWND_TOPMOST, x:=0, y:=0, cx:=0, cy:=0, wFlags:=SWP_NOSIZE Or SWP_NOMOVE)
    Else
        SetWindowPos(hwnd:=hwnd, hWndInsertAfter:=HWND_NOTOPMOST, x:=0, y:=0, cx:=0, cy:=0, wFlags:=SWP_NOSIZE Or SWP_NOMOVE)
    End If
End Sub
  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CDamogu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值