聆听到另一个窗口调整在C#中的事件 [英] Listening to another window resize events in C#
问题描述
我实现,需要自己附加到另一个窗口(观察)底部的小应用(观察员)。后者不是在应用程序内的一个窗口。
I am implementing a small application (observer) that needs to "attach" itself to the bottom of another window (observed). The latter is not a window inside the application.
在这一刻我解决了获取hWnd中的窗口,并在线程定期查询观察窗口的位置,相应地移动观测窗。
At this moment I solved by getting the hWnd of the window and querying periodically in a thread the location of the observed window, moving the observer window accordingly.
然而,这是一个非常不雅溶液。我想要做的就是听观察窗口的resize事件,使得观察者会作何反应只在必要时。
However this is a very inelegant solution. What I would like to do is to listen to the resize event of the observed window so that the observer will react only when necessary.
我想我应该用一个钩子,我发现很多做这件事的方法,但我缺乏的C WinAPI的知识挡住我的理解,我需要创建一个钩子和如何(PInvoke的/参数/等)
I assume I should use a hook, and I found plenty of ways of doing it, but my lack of knowledge of the C WinAPI is blocking me in understanding which hook I need to create and how (pinvoke/parameters/etc).
我是pretty肯定这是相当琐碎,和一些你熟悉C / C ++和WinAPI的将有答案准备在手;)
I'm pretty sure this is quite trivial, and some of you familiar with C/C++ and WinAPI will have the answer ready at hand ;)
感谢
推荐答案
拓展克里斯·泰勒的回答:与其自己做本地互操作,您可以使用的 ManagedWinApi ,其中包含了挂钩
类。
Expanding on Chris Taylor's answer: Instead of doing the native interop yourself, you can use ManagedWinApi, which contains a Hook
class.
编辑:要使用ManagedWinApi。在某处,code:
To use ManagedWinApi. Somewhere in your code:
Hook MyHook = new Hook(HookType.WH_CALLWNDPROC, false, false);
MyHook.Callback += MyHookCallback;
MyHook StartHook();
有关回调,参考 CallWndProc 并的 CWPSTRUCT :
private static int MyHookCallback(int code, IntPtr wParam, IntPtr lParam, ref bool callNext)
{
if (code >= 0)
{
// You will need to define the struct
var message = (CWPSTRUCT)Marshal.PtrToStructure(lParam, typeof(CWPSTRUCT));
// Do something with the data
}
return 0; // Return value is ignored unless you set callNext to false
}
这篇关于聆听到另一个窗口调整在C#中的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!