我有一个简单的问题。我用MSDN中的窗口api创建了一个标准窗口,创建了一个窗口站点。我想要程序做的是当我按下键盘上的键时,改变鼠标光标所在窗口的标题。为此,我安装了一个低级鼠标钩,如下所示:
LRESULT CALLBACK LowLevelMouseProc(__in int nCode, __in WPARAM wParam, __in LPARAM lParam) {
MSLLHOOKSTRUCT* p = (MSLLHOOKSTRUCT*)lParam;
HWND hiWnd = WindowFromPoint(p->pt); //Get a handle to the top-most window
ScreenToClient(hiWnd, &p->pt); //Converts the cursor position from screen to the specified window
char buf[33];
switch (wParam) {
case WM_MOUSEMOVE:
snprintf(buf, sizeof(buf) - 1, "X:%ld, Y:%ld", p->pt.x, p->pt.y); //Put the cursor coordinates into a char buffer
SendMessage(hiWnd, WM_SETTEXT, 0, (LPARAM)buf); //Send a message to the other window to change the title
break;
}
return CallNextHookEx(0, nCode, wParam, lParam);
}
在调用鼠标钩子时,只有我的程序更改文本。此外,文字是一堆汉字,而不是一两个,但一堆像这样:
藡覶 跾 瑍痸碚 齫儽戃 羭聧蔩, 圪妀 跾 鶀嚵巆 堔埧娾 爂犤繵 摿斠榱 軥軱逴 潫 徖梜, 薍薝
我是否必须弄乱前景窗口而不是获取背景窗口?或当前鼠标位置所在的窗口?我会认为
ScreenToClient(hiWnd, &p->pt)
感谢帮助人员!
答案 0 :(得分:0)
我想让程序做的是当我按下键盘上的键时改变鼠标光标所在窗口的标题
那你为什么要使用鼠标钩子而不是键盘钩子呢?在键盘钩子中,您可以使用GetCursorPos()
来获取当前鼠标屏幕的位置。
至于你的实际问题,你没有检查确保WindowFromPoint()
甚至找不到一个窗口,你没有将正确对齐的内存指针传递给WM_SETTEXT
,而你不是考虑目标窗口是Ansi还是Unicode。
尝试更像这样的东西:
键盘钩:
LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if ((nCode == HC_ACTION) && (wParam == WM_KEYUP))
{
KBDLLHOOKSTRUCT* p = (KBDLLHOOKSTRUCT*)lParam;
if (p->vkCode == ...) // whatever key you are looking for
{
POINT pt;
GetCursorPos(&pt);
HWND hiWnd = WindowFromPoint(pt);
if (hiWnd)
{
ScreenToClient(hiWnd, &pt);
if (IsWindowUnicode(hiWnd))
{
LPWSTR buf = (LPWSTR) GlobalAlloc(GMEM_FIXED, 33 * sizeof(WCHAR));
if (buf)
{
snwprintf(buf, 33, L"X:%ld, Y:%ld", pt.x, pt.y);
SendMessageW(hiWnd, WM_SETTEXT, 0, (LPARAM)buf);
GlobalFree(buf);
}
}
else
{
LPSTR buf = (LPSTR) GlobalAlloc(GMEM_FIXED, 33);
if (buf)
{
snprintf(buf, 33, "X:%ld, Y:%ld", pt.x, pt.y);
SendMessageA(hiWnd, WM_SETTEXT, 0, (LPARAM)buf);
GlobalFree(buf);
}
}
}
}
}
return CallNextHookEx(0, nCode, wParam, lParam);
}
鼠标钩:
LRESULT CALLBACK LowLevelMouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if ((nCode == HC_ACTION) && (wParam == WM_MOUSEMOVE))
{
MSLLHOOKSTRUCT* p = (MSLLHOOKSTRUCT*)lParam;
HWND hiWnd = WindowFromPoint(p->pt);
if (hiWnd)
{
POINT pt = p->pt;
ScreenToClient(hiWnd, &pt);
if (IsWindowUnicode(hiWnd))
{
LPWSTR buf = (LPWSTR) GlobalAlloc(GMEM_FIXED, 33 * sizeof(WCHAR));
if (buf)
{
snwprintf(buf, 33, L"X:%ld, Y:%ld", pt.x, pt.y);
SendMessageW(hiWnd, WM_SETTEXT, 0, (LPARAM)buf);
GlobalFree(buf);
}
}
else
{
LPSTR buf = (LPSTR) GlobalAlloc(GMEM_FIXED, 33);
if (buf)
{
snprintf(buf, 33, "X:%ld, Y:%ld", pt.x, pt.y);
SendMessageA(hiWnd, WM_SETTEXT, 0, (LPARAM)buf);
GlobalFree(buf);
}
}
}
}
return CallNextHookEx(0, nCode, wParam, lParam);
}