检测/挂钩窗口移动/拖动其他外部进程

时间:2013-04-01 19:12:44

标签: windows hook setwindowshookex setwindowlong

检测窗口移动/拖动其他进程的最佳方法是什么?在Windows7 64位

我目前正在使用C ++& amp ;;从DLL调查Global Hooks。 C#。这是一种痛苦,因为它不想正常工作。我在键盘和鼠标钩子方面取得了一些成功。但是对于窗口消息,我不知道什么是错的。

这是我的.dll文件中的代码

#include <windows.h>
#include <iostream>
#include <stdio.h>

HINSTANCE hinst;
#pragma data_seg(".shared")
HHOOK hhk;
WNDPROC realProc;
#pragma data_seg()
//#pragma comment(linker, "/SECTION:.shared,RWS") compiler error in VC++ 2008 express

LRESULT CALLBACK wireKeyboardProc(int code, WPARAM wParam,LPARAM lParam) {  
    //open the standard out stream for writing
    freopen("CONOUT$","w+t", stdout);
    printf("code:%d wparam:%d lparam:%d\n", code, wParam, lParam);

    /*
    if (code < 0) {
        return CallNextHookEx(0, code, wParam, lParam);
    }
    */
    //Beep(1000, 20);

    return CallNextHookEx(hhk, code, wParam, lParam);
}

LRESULT CALLBACK hookProc(HWND h, UINT msg, WPARAM wp, LPARAM lp)
{
    //open the standard out stream for writing
    freopen("CONOUT$","w+t", stdout);
    printf("h:%d msg:%d wp:%d lp:%d\n", h, msg, wp, lp);
    return CallWindowProc(realProc, h, msg, wp, lp);
}

extern "C" __declspec(dllexport) void install(unsigned long threadId, HWND hwnd) {
    //open the standard out stream for writing
    freopen("CONOUT$","w+t", stdout);

    //works for WH_KEYBOARD WH_MOUSE but doesnt work for WH_CALLWNDPROC
    hhk = SetWindowsHookEx(WH_CALLWNDPROC, wireKeyboardProc, hinst, threadId);
    printf("threadId: %d xxx: %d\n", threadId, hhk);

    /*
    //dont know whats wrong the return value of realProc is 0
    realProc = (WNDPROC)SetWindowLongPtr(hwnd, GWL_WNDPROC, (LONG_PTR)hookProc);
    printf("hwnd: %d xxx: %d\n", hwnd, realProc);
    */
}

extern "C" __declspec(dllexport) void uninstall() {
    UnhookWindowsHookEx(hhk); 
}

BOOL WINAPI DllMain(__in HINSTANCE hinstDLL, __in  DWORD fdwReason, __in  LPVOID lpvReserved) {
    hinst = hinstDLL;
    return TRUE;
}

我正在考虑制作自己的Aero Snap。这只是为了好玩。

感谢您的帮助。

1 个答案:

答案 0 :(得分:4)

经过一些额外的谷歌搜索后,我发现了一个几乎完全符合我想要的开源项目。

http://sourceforge.net/projects/powerresizer/

它编译容易而且没有错误。它在代码中显示它使用

SetWinEventHook(EVENT_SYSTEM_MOVESIZESTART, EVENT_SYSTEM_MOVESIZEEND,

和钩子程序的自定义DLL。它还展示了其他一些技巧。从未在其他任何地方见过SetWinEventHook。如果你学到了什么,那就恭维。

该死的当然也有一些窗户错误。