如何在特定窗口中使用InjectTouchInput?

时间:2014-03-09 22:01:51

标签: windows winapi

我正在尝试使用InjectTouchInput将触摸事件发送到特定窗口。 POINTER_INFO structurehwndTarget字段,据我所知,它指定了发送输入的目标窗口。

但是当我尝试使用此参数时,注入失败,代码为ERROR_INVALID_PARAMETER (87)

代码:

void MakePointerTouchInfo(POINTER_TOUCH_INFO& contact, int x, int y, int radius, int id, int orientation = 90, int pressure = 32000)
{
ZeroMemory(&contact, sizeof(POINTER_TOUCH_INFO));
contact.pointerInfo.pointerType = PT_TOUCH;
contact.pointerInfo.ptPixelLocation.x = x;
contact.pointerInfo.ptPixelLocation.y = y;
contact.pointerInfo.pointerId = id;


contact.touchFlags = TOUCH_FLAG_NONE;
contact.pointerInfo.pointerFlags = POINTER_FLAG_DOWN | POINTER_FLAG_INRANGE | POINTER_FLAG_INCONTACT;
contact.touchMask = TOUCH_MASK_NONE;
}

POINTER_TOUCH_INFO contacts[2];

// to start injection in separate thread.
DWORD makeInjection_(LPVOID p)
{
 InitializeTouchInjection(5, TOUCH_FEEDBACK_DEFAULT);

MakePointerTouchInfo(contacts[0], 200, 300, 2, 1);
MakePointerTouchInfo(contacts[1], 250, 300, 2, 6);

HWND hwnd = hWnd;

    // if I don't set hwnd (default 0), it works
contacts[0].pointerInfo.hwndTarget 
    = contacts[1].pointerInfo.hwndTarget 
    = hwnd;

if (!InjectTouchInput(1, contacts))
{
        //
        //here it fails
        //
        LPWSTR str = new wchar_t[255];
    auto t = GetLastError();
    wsprintf(str, L"%d\n", t);
    OutputDebugString(str);
}

contacts[0].pointerInfo.pointerFlags = POINTER_FLAG_UPDATE | POINTER_FLAG_INRANGE | POINTER_FLAG_INCONTACT;
contacts[1].pointerInfo.pointerFlags = POINTER_FLAG_UPDATE | POINTER_FLAG_INRANGE | POINTER_FLAG_INCONTACT;

//drag them from/to each other
for (int i = 0; i < 100; i++)
{
    contacts[0].pointerInfo.ptPixelLocation.x += 1;
    contacts[1].pointerInfo.ptPixelLocation.x -= 1;
    bool s = InjectTouchInput(1, contacts);
    Sleep(10);
}

//release them
contacts[0].pointerInfo.pointerFlags = POINTER_FLAG_UPDATE;
contacts[1].pointerInfo.pointerFlags = POINTER_FLAG_UPDATE;

bool success2 = InjectTouchInput(1, contacts);

return 0;

}

1 个答案:

答案 0 :(得分:0)

我认为问题是hwndTarget 您发送输入的窗口,它是输出字段,它告诉您哪个窗口输入捕获和/或位于您为注入的联系人指定的(x,y)位置。正如MSDN所说:

msdn

注入的输入不会发送到特定窗口,但是 - 再次引用from MSDN - 发送到桌面:

  

注入的输入被发送到会话的桌面   注射过程正在运行。

也许你需要探索函数RegisterPointerInputTarget,其中“允许调用者注册一个目标窗口,指定类型的所有指针输入都被重定向到该目标窗口”。但是,在所有情况下都无法重定向输入:

msdn