我想监控的是excel主程序窗口大小的变化。即Excel最大化最小化和窗口大小有改变时,我的程序可以响应,调整到合适的位置。
我只会vb呢。
从网上我搜索出很多键盘,鼠标类的钩子代码。也可以用。
监控窗口大小变化的,也有几个。但是经过调试,都没有成功监测到窗口大小变化。
其中有一个是这样的:Public Declare Function RegisterShellHookWindow Lib "user32" (ByVal hWnd As Long) As Long ''use in NT5
Public Declare Function RegisterWindowMessage Lib "user32" Alias "RegisterWindowMessageA" (ByVal lpString As String) As Long
Public Declare Function DeregisterShellHookWindow Lib "user32" (ByVal hWnd As Long) As Long
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthW" (ByVal hWnd As Long) As Long
Private Const HSHELL_WINDOWCREATED = 1 '' 顶级窗体被创建
Private Const HSHELL_WINDOWDESTROYED = 2 '' 顶级窗体即将被关闭
Private Const HSHELL_ACTIVATESHELLWINDOW = 3 '' SHELL 的主窗体将被激活
Private Const HSHELL_WINDOWACTIVATED = 4 '' 顶级窗体被激活
Private Const HSHELL_GETMINRECT = 5 '' 顶级窗体被最大化或最小化'一个窗口正在被最小化或者最大化,系统需要该窗口被最小化时的矩形坐标.'win95
Private Const HSHELL_REDRAW = 6 '' Windows 任务栏被刷新
Private Const HSHELL_TASKMAN = 7 '' 任务列表的内容被选中
Private Const HSHELL_LANGUAGE = 8 '' 中英文切换或输入法切换
Private Const HSHELL_SYSMENU = 9 ''显示系统菜单
Private Const HSHELL_ENDTASK = 10 ''顶级窗体被强制关闭
Private Const HSHELL_ACCESSIBILITYSTATE = 11 '对Windows NT5.0或以上版本有效,指示"可访问性"已被改变.
Private Const HSHELL_APPCOMMAND = 12 ''没有被程序处理的APPCOMMAND。见WM_APPCOMMAND
Private Const HSHELL_WINDOWREPLACED = 13 ''wParam=被替换的顶级窗口的hWnd
Private Const HSHELL_WINDOWREPLACING = 14 ''wParam=替换顶级窗口的窗口hWnd
Private Const HSHELL_HIGHBIT = &H8000& ''掩码
Private Const HSHELL_FLASH = (HSHELL_REDRAW Or HSHELL_HIGHBIT) ''标题闪烁
Private Const HSHELL_RUDEAPPACTIVATED = (HSHELL_WINDOWACTIVATED Or HSHELL_HIGHBIT) ''不知道干吗的
Public Const GWL_WNDPROC = -4
Private Const MAX_PATH = 260
Public Shell_Hook_Msg_ID As Long
Public LogWinOldProc As Long
Public Function WndProc(ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Select Case wMsg
Case Shell_Hook_Msg_ID
Dim szTmp As String, Jg As String
Form1.text2 = Shell_Hook_Msg_ID
Select Case wParam
Case 1: Jg = "创建或打开新的窗口"
Case 2: Jg = "关闭窗口"
Case 3: Jg = "激活sheel程序窗口"
Case 4: Jg = "激活普通窗口"
Case 5: Jg = "窗口大小改变"
Case 6: Jg = "任务栏被刷新"
Case 7: Jg = "任务列表内容被选中"
Case 8: Jg = "输入法切换"
Case 9: Jg = "显示系统菜单"
Case 10: Jg = "窗口被强制关闭"
Case Else: Jg = wParam
End Select
szTmp = String(MAX_PATH, vbNullChar)
Call GetWindowText(lParam, szTmp, MAX_PATH)
Form1.Text1 = wMsg & ">" & wParam & ">" & Format(Time, "hh:mm:ss: ") & Jg & "———" & Left$(szTmp, GetWindowTextLength(lParam)) & vbCrLf & Form1.Text1
End Select
WndProc = CallWindowProc(LogWinOldProc, hWnd, wMsg, wParam, lParam)
End Function
这一个可以成功运行。可以监测到窗口创建,关闭,任务栏刷新,而其它的几项则监控不到。