获取窗口坐标信息失败

CrazyCode1981 2015-12-17 12:08:11
程序的功能是 安装全局钩子 当键盘按下F1键时获取当前鼠标位置信息,全局钩子代码写在Dll中,
程序出现的问题:只有在Windows窗口或者当前程序是前景窗口的时候能获取鼠标位置信息,
如果当前窗口是别的应用程序获取位置信息,调用 GetCursorPos())的时候返回值都是0,也就是失败了,该怎么解决?

钩子和回调函数的代码

LRESULT CALLBACK KeyboardProc(
_In_ int code,
_In_ WPARAM wParam,
_In_ LPARAM lParam
)
{

//HWND currentW = GetForegroundWindow();

//HWND c = GetForegroundWindow();
#pragma region F1-F4按键处理
if (VK_F1 == wParam)//如果两个判断都放在一起 那么VK_F2按键弹起的时候还是会调用CallNextHookEx
{
//MessageBox(dllHWND, TEXT("哈哈"), TEXT("测试"), 0);
if ((lParam >> 31 & 1))
{
KeyType = 1;
//Condition = ClientToScreen(currentW, &(mouseP->F1));
Condition = GetCursorPos(&(mouseP->F1));在这里如果当前窗口是别的应用程序获取位置信息的返回值总是0,是什么原因呢?
::SendMessage(dllHWND, WM_MYMSG, WPARAM(KeyType), LPARAM(Condition));
}
return 1;
}

#pragma endregion

return CallNextHookEx(G_keyboard, code, wParam, lParam);

}

void HookProc(HWND winhandle, PointStruct* MousePoint)
{
dllHWND = winhandle; mouseP = MousePoint;
G_keyboard = SetWindowsHookEx(WH_KEYBOARD, KeyboardProc, GetModuleHandle(TEXT("HookProcDll")), 0);
}
...全文
291 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2015-12-24
  • 打赏
  • 举报
回复
膜拜4楼 Orz
CrazyCode1981 2015-12-24
  • 打赏
  • 举报
回复
最后解决了,是需要使用WH_KEYBOD_LL低级钩子,之前不行不是赵四老师说的问题,是因为WH_KEYBOD_LL和WH_KEYBOD这两个回调函数中的参数是不一样的, WH_KEYBOD_LL回调函数㕜使用结构体KBDLLHOOKSTRUCT *Key_Info = (KBDLLHOOKSTRUCT*)lParam; Key_Info->vkCode == VK_SPACE来判断是否为指定按钮 而在WH_KEYBOD中VK_F1 == wParam这样判断就行了
赵4老师 2015-12-22
  • 打赏
  • 举报
回复
if (VK_F2 == wParam) {
    if ((lParam >> 31 & 1)) {
        DllMessage->KeyType = 2;
        DllMessage->Condition = ::GetCursorPos(&(DllMessage->mouseP->F2));
        if (0 == DllMessage->Condition) {
            DllMessage->ErrorId = GetLastError();
        } else {
            DllMessage->ErrorId = 0;
        }
        ::SendMessage(AppHWND, WM_MYMSG, WPARAM(DllMessage), 0);
    }
    return 1;
}
Eleven 2015-12-22
  • 打赏
  • 举报
回复
WH_KEYBOD_LL
CrazyCode1981 2015-12-21
  • 打赏
  • 举报
回复
按照赵四老是的思路,找到原因,本来想获取ErrorId,发现ID不会变,通过测试发现,只要是别的应用程序虽然触发了我的代码,但是传出的值却没有修改。 if (VK_F2 == wParam) { if ((lParam >> 31 & 1)) { DllMessage->KeyType = 2; DllMessage->Condition = ::GetCursorPos(&(DllMessage->mouseP->F2)); DllMessage->ErrorId = GetLastError();// 上面设置的值都没有改变,并且下面的SendMessage还正常执行了 比较奇怪 ::SendMessage(AppHWND, WM_MYMSG, WPARAM(DllMessage), 0); } return 1; } 我的系统是Win7,为什么会这样呢?
赵4老师 2015-12-17
  • 打赏
  • 举报
回复
GetCursorPos The GetCursorPos function retrieves the cursor's position, in screen coordinates. BOOL GetCursorPos( LPPOINT lpPoint // address of structure for cursor position ); Parameters lpPoint Pointer to aPOINT structure that receives the screen coordinates of the cursor. Return Values If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, callGetLastError. Remarks The cursor position is always given in screen coordinates and is not affected by the mapping mode of the window that contains the cursor. The calling process must have WINSTA_READATTRIBUTES access to the window station. QuickInfo Windows NT: Requires version 3.1 or later. Windows: Requires Windows 95 or later. Windows CE: Unsupported. Header: Declared in winuser.h. Import Library: Use user32.lib. See Also Cursors Overview, Cursor Functions, ClipCursor,POINT, SetCursor, SetCursorPos, ShowCursor

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧