VB 中自定义弹出提示框的位置
2020-02-18 06:28
281 查看
窗体代码:
Private Sub Command1_Click()
Dim hInst As Long
Dim Thread As Long
'Set up the CBT hook
hInst = GetWindowLong(Me.hWnd, GWL_HINSTANCE)
Thread = GetCurrentThreadId()
hHook = SetWindowsHookEx(WH_CBT, AddressOf WinProc1, hInst, _
Thread)
'Display the message box
MsgBox "This message box has been positioned at (0,0)."
End Sub
Private Sub Command2_Click()
Dim hInst As Long
Dim Thread As Long
'Set up the CBT hook
hInst = GetWindowLong(Me.hWnd, GWL_HINSTANCE)
Thread = GetCurrentThreadId()
hHook = SetWindowsHookEx(WH_CBT, AddressOf WinProc2, hInst, Thread)
'Display the message box
MsgBox "This message box is centered over Form1."
End Sub
模块代码:
Option Explicit
Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Public Declare Function UnhookWindowsHookEx Lib "user32" ( _
ByVal hHook As Long ) As Long
Public Declare Function GetWindowLong Lib "user32" Alias _
"GetWindowLongA" ( ByVal hwnd As Long , ByVal nIndex As Long ) _
As Long
Public Declare Function GetCurrentThreadId Lib "kernel32" () As Long
Public Declare Function SetWindowsHookEx Lib "user32" Alias _
"SetWindowsHookExA" ( ByVal idHook As Long , ByVal lpfn As Long , _
ByVal hmod As Long , ByVal dwThreadId As Long ) As Long
Public Declare Function 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 ) As Long
Public Declare Function GetWindowRect Lib "user32" ( ByVal hwnd _
As Long , lpRect As RECT) As Long
Public Const GWL_HINSTANCE = (- 6 )
Public Const SWP_NOSIZE = &H1
Public Const SWP_NOZORDER = &H4
Public Const SWP_NOACTIVATE = &H10
Public Const HCBT_ACTIVATE = 5
Public Const WH_CBT = 5
Public hHook As Long
Function WinProc1( ByVal lMsg As Long , ByVal wParam As Long , _
ByVal lParam As Long ) As Long
If lMsg = HCBT_ACTIVATE Then
'Show the MsgBox at a fixed location (0,0)
SetWindowPos wParam, 0 , 0 , 0 , 0 , 0 , _
SWP_NOSIZE or SWP_NOZORDER or SWP_NOACTIVATE
'Release the CBT hook
UnhookWindowsHookEx hHook
End If
WinProc1 = False
End Function
Function WinProc2( ByVal lMsg As Long , ByVal wParam As Long , _
ByVal lParam As Long ) As Long
Dim rectForm As RECT, rectMsg As RECT
Dim x As Long , y As Long
'On HCBT_ACTIVATE, show the MsgBox centered over Form1
If lMsg = HCBT_ACTIVATE Then
'Get the coordinates of the form and the message box so that
'you can determine where the center of the form is located
GetWindowRect Form1.hwnd, rectForm
GetWindowRect wParam, rectMsg
x = (rectForm.Left + (rectForm.Right - rectForm.Left) / 2 ) - _
((rectMsg.Right - rectMsg.Left) / 2 )
y = (rectForm.Top + (rectForm.Bottom - rectForm.Top) / 2 ) - _
((rectMsg.Bottom - rectMsg.Top) / 2 )
'Position the msgbox
SetWindowPos wParam, 0 , x, y, 0 , 0 , _
SWP_NOSIZE or SWP_NOZORDER or SWP_NOACTIVATE
'Release the CBT hook
UnhookWindowsHookEx hHook
End If
WinProc2 = False
End Function
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- VBA,两个sheet根据 4000 第一列id相同比较其余列,不同的值放到sheet3中
- solaris vbs巡检脚本
- vb的LINQ实现
- VB.NET 使用CHART控件做实时曲线
- VB总结
- 用VB制作文件下载程序(转)
- LGT8F328P-SSOP20 MiniEVB与USB--TTL烧录器连接
- VB调用.NET DLL(一)
- VB.NET水晶报表Crystal Reports学习(开发环境安装+程序示例+报表示例+客户端运行环境)
- 【求助】如何将循环检测中的代码检测到后只循环一次(vbs语言)
- VB6.0的DllFunctionCall
- VB程序中如何找到自定义函数
- VB 获取 Internet Explorer_Server 里面的内容
- VB钓鱼
- VB语言入门学习
- VBA编程实现汉字注音
- C# params VB.NET ParamArray
- 编程怎么做·VB.NET如何启动一个程序
- 【VB6至VB.Net的进步】字符串转UTF-8的URL
- Excel vba获取文件夹内文件名