SendInput after EnableWindow in Windows 7
I have a child window that is maximized within a parent.
It needs to be disabled so that it doesn't receive inputs until I press a key. When I press key 'A' for instance, I want the child window to be enabled, receive inputs sent with SendInput() and disable again.
So I do this:
EnableWindow( hwnd, TRUE );
SetForegroundWindow( hwnd);
SetFocus( hwnd);
Sleep(50);
SendInput()...x7-8 times
EnableWindow( hwnd, FALSE );
Now, EnableWindow functions work fine, except window misses some of the inputs. I tried to put some delay after EnableWindow (like 6-7 seconds!!) and still it doesn't work right.
I tried SetWindowPos() to have it update its frame, I tried setting the WS_DISABLE bit manually but still no luck. Inputs work fine if the child window is enabled all the time.
Any help i开发者_JAVA技巧s appreciated.
A Child window is serviced by the same thread as it's parent window. So the send SendInput doesn't do any good unless you go back to the pump and handle the events before you disable the window again.
If you explain what you are trying to accomplish, we could probably give you a better way to do it. But in any case, at the very least you need to run a message pump after SendEvents until you run out of events.
Be aware the a pump will also pump other messages, so it may make your whole design fall over. But, here it is.
// process messages until the queue is empty.
//
MSG msg;
while (PeekMessage(&msg, NULL, 0, PM_REMOVE))
{
// make sure we don't eat the quit message.
if (WM_QUIT == msg.message)
{
PostQuitMessage();
break;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
Oh, and the Sleep() isn't doing anything useful, you can take it out.
Edit: this code goes after the SendInput
call and before the window is disabled.
You may also want to use it instead of Sleep()
to get the window to settle down before
you SendInput
.
Instead of SendInput
, I would try sending keyboard or mouse messages directly to the child window. For example:
EnableWindow(hwnd, TRUE);
SendMessage(hwnd, WM_KEYDOWN, ..., ...);
SendMessage(hwnd, WM_KEYUP, ..., ...);
// etc...
EnableWindow(hwnd, FALSE);
精彩评论