Sign in to follow this  
v0dKA

Activating a window

Recommended Posts

I'm having trouble activating a minimized window and setting it focused. Given that hWnd is a valid HWND handle to the correct window, the following code does not work for me:
ShowWindow( hWnd, SW_RESTORE );
SetFocus( hWnd );
The effect is that my window simply never receives focus, as if I never called the SetFocus() function. Even worse, if my window was not minimized, it is not shown (to say nothing of focused). Using SW_SHOWNORMAL, the other logical option, does not work either. So my question is, how can I bring a window from whatever state it was before to the foreground and focused? (Note that SetForegroundWindow() works only if the window is not minimized.) My full program in its current state, if needed:
#undef UNICODE

#include <windows.h>
#include <WinAble.h>
#include <iostream>
#include <string>

BOOL CALLBACK EnumWindowsProc( HWND hWnd, LPARAM lParam );

int main()
{
	EnumWindows( EnumWindowsProc, NULL );

	std::cin.get();
	return 0;
}

BOOL CALLBACK EnumWindowsProc( HWND hWnd, LPARAM lParam )
{
	char szWindowTitle[ 255 ];
	GetWindowText( hWnd, szWindowTitle, 255 );
	std::string windowTitle = szWindowTitle;

	if( windowTitle.find( "Foo", 0 ) != std::string::npos )
	{
		std::cout << "Found Foo!" << std::endl;

		ShowWindow( hWnd, SW_SHOWNORMAL );
		SetFocus( hWnd );

		::Sleep( 1000 );   // Do i need this?

		INPUT input[ 8 ];
		::ZeroMemory( input, sizeof( input ) );
		input[ 0 ].type = input[ 1 ].type = input[ 2 ].type
			 = input[ 3 ].type = input[ 4 ].type = input[ 5 ].type
			  = input[ 6 ].type = input[ 7 ].type = INPUT_KEYBOARD;
		input[ 0 ].ki.wVk = input[ 7 ].ki.wVk = VK_SHIFT;
		input[ 2 ].ki.dwFlags = input[ 4 ].ki.dwFlags
			= input[ 6 ].ki.dwFlags = input[ 7 ].ki.dwFlags = KEYEVENTF_KEYUP;
		input[ 1 ].ki.wVk = input[ 2 ].ki.wVk = input[ 3 ].ki.wVk =
			input[ 4 ].ki.wVk = input[ 5 ].ki.wVk = input[ 6 ].ki.wVk = 0x38;
		::SendInput( 8, input, sizeof( INPUT ) );

		return FALSE;
	}

	return TRUE;
}


Share this post


Link to post
Share on other sites
Another question (and yes, also a bump in disguise):

How can I quickly reset the state of the keyboard? That is, my program sends keyboard messages and I want to provide an emergency-quit option. If the escape key is pressed, I want to stop sending keyboard messages and simply quit the program. However, it is possible that when the program quits, the SHIFT, CTRL, or any other key was pressed but not released. The keyboard will, even after the program exits, continue to behave as if these keys are still pressed. So before I quit, I want to "unpress" all keys. Is there a way to do that?

Share this post


Link to post
Share on other sites
Seems to me like you provided the answers to your own question above...


HWND pTargetWindowHandle = FindWindow("Notepad", char());
...
ShowWindow(pTargetWindowHandle, SW_RESTORE);
SetForegroundWindow(pTargetWindowHandle);








If the window is minimized ShowWindow will bring it back up, and the next call will give it focus.

If the window is at the bottom of the z-order, GetForegroundWindow will bring it back to the top, and give you focus...

Also, keep in mind that SetFocus doesn't work because your window isn't attached to your thread's message queue.

Hope that helps...

P.S.

Also, if you want to send characters to a specific window, for example, notepad, which has an "Edit" control, you can send it the WM_SETTEXT message, that way you won't need to worry about window focus, and key states.

Or if you'd like to append a character you could send WM_CHAR.

For example,

HWND pTargetWindowHandle = FindWindowEx(FindWindow("Notepad", char()), HWND(), "Edit", char());
std::string pString("abc");

...

SendMessage(pTargetWindowHandle, WM_SETTEXT, WPARAM(), reinterpret_cast <LPARAM> (pString.c_str()));

for(std::string::iterator iIndex = pString.begin() ; iIndex < pString.end() ; iIndex ++)
SendMessage(pTargetWindowHandle, WM_CHAR, * iIndex, LPARAM());



Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this