| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3455 人关注过本帖
标题:vb如何监控其它程序窗口的大小改变
取消只看楼主 加入收藏
rmoon
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2012-12-19
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
vb如何监控其它程序窗口的大小改变
vb如何监控其它程序窗口的大小改变。用api 钩子 应该可以实现。谁有这方面的源码。
百度了很长时间,只找到了一些键盘鼠标钩子。
还有没有其它方法?
搜索更多相关主题的帖子: 如何 百度 其它 
2012-12-19 15:20
rmoon
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2012-12-19
收藏
得分:0 
用timer控件定时检测应该是可以的。只是感觉资源占用比较大。
用钩子应该是可以的。
研究了好长时间,没有成功。
2012-12-19 15:46
rmoon
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2012-12-19
收藏
得分:0 
我想监控的是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


这一个可以成功运行。可以监测到窗口创建,关闭,任务栏刷新,而其它的几项则监控不到。
2012-12-19 16:45
快速回复:vb如何监控其它程序窗口的大小改变
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018610 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved