全球拦截窗口运动 [英] Globally intercept window movement

查看:296
本文介绍了全球拦截窗口运动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使全局系统挂钩工作。我想在窗口移动时尽快通知,并尽快更改窗口大小。这意味着CBT钩子 HCBT_MOVESIZE 不会剪切它,它只发生在窗口移动后。我想挂钩窗口的实际移动,并且能够在移动期间更改窗口大小。



钩子是从DLL设置的,并且回调函数是在该DLL内。这是我试过的。




  • WH_CALLWNDPROC 。当窗口移动( WM_MOVING 从其他应用程序接收到窗口)时,它会提醒我,但我不能更改消息的内容。

  • WH_CALLWNDPROCRET WH_CALLWNDPROC 相同。

  • c $ c> HCBT_MOVESIZE 。事件发生到后期。

  • WH_GETMESSAGE 。永远不会收到 WM_MOVE WM_MOVING WM_WINDOWPOSCHANGING

  • 更新:Windows事件钩子似乎允许我捕获它:

      hWinEventHook = SetWinEventHook(EVENT_SYSTEM_MOVESIZESTART,
    EVENT_SYSTEM_MOVESIZEEND,NULL,WinEventProc,
    0,0 ,WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS);但是,这会产生一个不同的问题:使用改变窗口的大小,使用

    即使我使用 SWP_NOSENDCHANGING ,SetWindowPos()
    不工作(它更改大小,但立即改回其以前的大小)。想法?



    更新2 :子类似乎工作,但是Visual Studio在每个程序运行后崩溃它工作很好,如果我放置断点和通过取消分类,但不是当我让程序自己运行。想要吗?



    我有一个CBT钩子(从早些时候开始),每当 HCBT_ACTIVATE 新窗口,我删除任何以前的子类使用 SetWindowLongPtr()(这也必须运行在64位),然后子类新窗口。如果我把断点放在任何地方,并立即恢复会话,当它打破,一切工作正常。但是,当我没有任何断点时,Visual Studio会在程序退出时崩溃。

    解决方案

    你只需要在你绝对需要的时候使用它们。



    也就是说,你可以使用一个基本的钩子作为一种进入过程。一旦在这个过程中,你可以子类化你感兴趣的窗口,并处理你的子类proc中的大小调整消息,而不是试图捕获钩子级别的一切。



    根据您在响应调整大小时要执行的操作,您可能需要一些进程间通信。


    I am having trouble getting a global system hook to work. I want to be notified whenever a window is moving, as early as possible, and change the window size. This means the CBT hook HCBT_MOVESIZE won't cut it, it only happens after the window has been moved. I want to hook the actual movement of the window, and be able to change the window size during the move.

    The hooks are set from a DLL, and the callback function is within that DLL. This is what I've tried.

    • WH_CALLWNDPROC. It does alert me when a window is moved (WM_MOVING is received for windows from other applications), but I cannot change the contents of the message.
    • WH_CALLWNDPROCRET Same as WH_CALLWNDPROC.
    • CBT hook HCBT_MOVESIZE. Event happens to late.
    • WH_GETMESSAGE. Never receive WM_MOVE, WM_MOVING or WM_WINDOWPOSCHANGING. This hook would allow me to change the messages.

    Update: Windows event hooks seem to allow me to capture it:

    hWinEventHook = SetWinEventHook(EVENT_SYSTEM_MOVESIZESTART,    
        EVENT_SYSTEM_MOVESIZEEND, NULL, WinEventProc, 
        0, 0, WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS);
    

    However, this creates a different problem: changing the size of the window using SetWindowPos() does not work (it changes size alright, but immediately changes back to its previous size), even though I use SWP_NOSENDCHANGING. Ideas?

    Update 2: Subclassing seems to work, however Visual Studio crashes after each program run (so does a lot of other windows). It works well if I place breakpoints and walk through the "unsubclassing", but not when I let the program run by itself. Ideas?

    I have a CBT hook (it was there from earlier), and whenever HCBT_ACTIVATE is sent for a new window, I remove any previous subclassing using SetWindowLongPtr() (this has to run on 64-bit as well), and then subclass the new window. If I put a breakpoint anywhere, and immediately resume the session when it breaks, everything works fine. However, when I do not have any breakpoints, Visual Studio crashes when the program exits.

    解决方案

    Hooks are pretty heavy. You only want to use them when you absolutely have to.

    That said, you could use one of the basic hooks simply as a way to get into the process. Once in the process, you could subclass the window you're interested in and handle the sizing messages in your subclass proc rather than trying to catch everything at the hook level.

    Depending on what you want to do in response to the resize, you might need some interprocess communication.

    这篇关于全球拦截窗口运动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆