如何在鼠标挂钩过程中检测拖动

问题描述 投票:0回答:1

我已经成功地为窗口设置了一个鼠标挂钩,并且我想在单击鼠标左键时检测到拖动操作。我尝试使用DragDetect,如下所示,但是它从不返回DragDetect,并且从不抑制后续的鼠标上移事件(在此代码中,TRUE是目标窗口):

hwnd

LRESULT CALLBACK MouseHookProc(int nCode, WPARAM wParam, LPARAM lParam) { if (nCode < 0) { return CallNextHookEx(NULL, nCode, wParam, lParam); } switch (wParam) { case WM_LBUTTONDOWN: { // Get the click point LPMOUSEHOOKSTRUCT pMouseHookStruct = reinterpret_cast<LPMOUSEHOOKSTRUCT>(lParam); CPoint ptClick = pMouseHookStruct->pt; // Drag detect BOOL fDrag = DragDetect(hwnd, ptClick); if (fDrag) { MessageBox(NULL, L"Drag", NULL, MB_OK); } break; } } return CallNextHookEx(NULL, nCode, wParam, lParam); } 是否在挂钩过程中不起作用,或者我只是做错了什么?

c++ winapi hook drag
1个回答
0
投票

正如评论所指出,DragDetect不能预测未来。但是您可以在单击时间内比较DragDetectPOINTWM_LBUTTONDOWN

示例:

WM_MOUSEMOVE

DLL:

#include <windows.h>
#include <iostream>
using namespace std;
typedef BOOL(*SETHOOK)(HWND hWnd);
typedef BOOL(*UNHOOK)();
LRESULT CALLBACK WndProcFunc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }
    return DefWindowProc(hwnd, message, wParam, lParam);
}
int main(int argc, char* argv[])
{
    WNDCLASS wc{};
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = WndProcFunc;
    wc.hInstance = GetModuleHandle(NULL);
    wc.lpszClassName = L"Class_Name";
    wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
    RegisterClass(&wc);

    HWND hWnd = CreateWindow(L"Class_Name", L"Test", WS_OVERLAPPEDWINDOW, 0, 0, 1000, 500, NULL, NULL, GetModuleHandle(NULL), NULL);

    SETHOOK SetHook;
    UNHOOK UnHook;
    HMODULE h = LoadLibraryA("PATH\\DLL4.dll");
    SetHook = (SETHOOK)GetProcAddress(h, "SetHook");
    UnHook = (UNHOOK)GetProcAddress(h, "UnHook");
    BOOL i = SetHook(hWnd);

    ShowWindow(hWnd, 1);
    UpdateWindow(hWnd);

    MSG Msg;
    while (GetMessage(&Msg, NULL, 0, 0))
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }

    UnHook();
    return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.