0

I have to put a window to foreground using its name, for example "images". With

findWindowW(NULL, stringName)

I get the handle to the process (HWND). Then with

SetForegroundWindow(windowHandle);

I think that I put it into the foreground automatically, but I have to press 'Enter'. Am I doing something wrong or there's another way to do that? I can use also the PID of the process.
My final purpose is to send shortcuts like CTRL+V to the process after put it into the foreground. Thank you.

A. Wolf
  • 1,309
  • 17
  • 39

1 Answers1

2

From MSDN

The system restricts which processes can set the foreground window. A process can set the foreground window only if one of the following conditions is true:

  • The process is the foreground process.
  • The process was started by the foreground process.
  • The process received the last input event.
  • There is no foreground process.
  • The process is being debugged.
  • The foreground process is not a Modern Application or the Start Screen.
  • The foreground is not locked (see LockSetForegroundWindow).
  • The foreground lock time-out has expired (see SPI_GETFOREGROUNDLOCKTIMEOUT in SystemParametersInfo).
  • No menus are active.

So, if your program does not correspond to the above, you can not set foreground automatically.

I think you can use below code for your case and this link can be help as well.

void SetForegroundWindowForce(HWND hWnd)
{
   HWND hWndForeground = ::GetForegroundWindow();
   if(hWndForeground == hWnd) return;

   DWORD Strange = ::GetWindowThreadProcessId(hWndForeground, NULL);
   DWORD My = ::GetWindowThreadProcessId(hWnd, NULL);
   if( !::AttachThreadInput(My, Strange, TRUE) )
   {
      ASSERT(0);
   }
   ::SetForegroundWindow(hWnd);
   ::BringWindowToTop(hWnd);
   if( !::AttachThreadInput(My, Strange, FALSE) )
   {
      ASSERT(0);
   }
}
Community
  • 1
  • 1
hyun
  • 2,135
  • 2
  • 18
  • 20
  • Thank you. I didn't try it, but looking at MSDN I think I've understood. Here's my interpretation: first keep the handle to the foreground window and the corrent window; then with AttachThreadInput() the process share the input buffer (not so sure about that...) than I set the other window on top and "split" the two process.
    I'm not so sure about that, if something is completely wrong please point out it to me, thank you!!
    – A. Wolf Sep 12 '16 at 15:15
  • You're almost correct. By using `AttachThreadInput`, two different thread can share their input states such as keyboard, mouse, etc. Your window handle can acquire **right condition** as calling `AttatchThreadInput` function with specifying **TRUE** for second parameter. – hyun Sep 13 '16 at 00:02
  • Ok. I run that method with the handle of the window. AttachThreadInput returns a value greater than 0 so it does what is in the if ( I don't know how assert works with only a parameter..). What if I want to send a combination of keys to the second window? Even without change the focus. – A. Wolf Sep 13 '16 at 08:08