所以基本上我有一个Window Handler,它是一个Pic(矩形),包含16个插槽。
好吧,所以我想对这个hWnd使用SendMessage并将keyPressed作为消息提供给第一个和第二个插槽(因为它们不是单独的窗口,我不能把每个插槽窗口处理)。问题在于实现这些偏移以设置我想要发送这些消息的区域。
基本上我正在为离线游戏制作培训师,以便通过内存管理来练习,并了解WINAPI的工作原理。这是代码(不包括DllMain函数)。
#include <windows.h>
#include <stdio.h>
#define VIDAACTUAL 0x70D27E
#define VIDAMAX 0x70D27C
#define MANAACTUAL 0x70D282
#define MANAMAX 0x70D280
void AutoPot(){
Sleep(5000); //Giving myself time to place the cursor.
POINT P;
GetCursorPos(&P);
HWND hwnd = WindowFromPoint(P);
Sleep(50);
while (1){
WORD hpMax = *(WORD*)(VIDAMAX);
WORD hpAct= *(WORD*)(VIDAACTUAL);
WORD mpMax = *(WORD*)(MANAMAX);
WORD mpAct = *(WORD*)(MANAACTUAL);
if (hpAct != 0)
{
if (hpAct != hpMax)
{
SendMessage(hwnd, WM_LBUTTONUP, 0, 0);
//SendMessage(hwnd, WM_LBUTTONDBLCLK, 0, 0);
//SendMessage(hwnd, VK_SPACE, 0, 0);
}
else if (mpAct!= mpMax && mpMax != 0){ //Giving priority to red pots.
SendMessage(hwnd, WM_LBUTTONUP, 0,0);
//SendMessage(hwnd, WM_LBUTTONDBLCLK, 0,0);
//SendMessage(hwnd, VK_SPACE, 0, 0);
}
}
Sleep(50);
}
}
那么,有没有办法在hWnd中选择一个特定区域来发送这些消息?
答案 0 :(得分:0)
发送消息时未指定正确的鼠标坐标。 GetCursorPos()
返回屏幕坐标中的当前鼠标位置,然后在向窗口发送鼠标消息时需要将其转换为客户端坐标,例如:
#include <windows.h>
#include <stdio.h>
#define VIDAACTUAL 0x70D27E
#define VIDAMAX 0x70D27C
#define MANAACTUAL 0x70D282
#define MANAMAX 0x70D280
void AutoPot()
{
Sleep(5000); //Giving myself time to place the cursor.
POINT P;
GetCursorPos(&P);
HWND hwnd = WindowFromPoint(P);
ScreenToClient(hwnd, &P); // <-- here
Sleep(50);
while (1){
WORD hpMax = *(WORD*)(VIDAMAX);
WORD hpAct= *(WORD*)(VIDAACTUAL);
WORD mpMax = *(WORD*)(MANAMAX);
WORD mpAct = *(WORD*)(MANAACTUAL);
if (hpAct != 0)
{
if (hpAct != hpMax)
{
PostMessage(hwnd, WM_LBUTTONUP, 0, MAKELPARAM(P.x, P.y));
//PostMessage(hwnd, WM_LBUTTONDBLCLK, 0, MAKELPARAM(P.x, P.y));
}
else if ((mpAct != mpMax) && (mpMax != 0)) //Giving priority to red pots.
{
PostMessage(hwnd, WM_LBUTTONUP, 0, MAKELPARAM(P.x, P.y));
//PostMessage(hwnd, WM_LBUTTONDBLCLK, 0, MAKELPARAM(P.x, P.y));
}
}
Sleep(50);
}
}
FYI,VK_SPACE
不是有效的消息标识符(其值为0x20
,与WM_SETCURSOR
消息的值相同。您必须直接使用WM_KEYDOWN
和WM_KEYUP
,和/或WM_CHAR
。但是,you can’t simulate keyboard input with PostMessage。
答案 1 :(得分:-2)
Windows将鼠标消息发送到基于左上角的窗口。所以窗口的左上角是(0,0)。如果您知道要发送消息的项目的位置,然后将其传递给您的sendmessage。如果不是,你将不得不调用getClientRect(hWnd ...)并做一些数学运算:)
//we get the client rect
RECT rect;
GetClientRect(hWnd, &rect);
//rect = { LONG top, left, right bottom }
//i guess here we divide the rect into 4x4 squares (no idea of your layout)
//so pick a square to get xpos and ypos
int x, y; //can't calculate these i have no idea of your layout
LPARAM lParam = MAKELPARAM(x, y);
PostMessage(hWnd, WM_LBUTTONDOWN, 0, lParam); //NOT SendMessage!!!
美中不足的可能是Windows将WM_LBUTTON消息坐标解释为屏幕坐标。在这种情况下,我们必须倒退。找出了我们想要在客户区域中单击的位置,我们需要知道屏幕坐标。在那种情况下我们使用(一个用户指出它不转换坐标所以忽略这一点 - 只是发送客户端坐标)
POINT thePointWeWantOnTheClient;
ClientToScreen(hWnd, &thePointWeWantOnTheClient);
而是使用这一点。我明天早上才能测试这个。
值得指出SendMessage是阻塞的,所以它会阻止GUI。 PostMessage只是填充在你想要的消息队列中。