0

I have created a windows application in C++ and I want to make so whenever I run it, it doesn't steal focus from whichever window is currently focused(or maybe steal the focus and give it back right away). I'm not creating any window so i'm not sure how to change the window style, my program runs in the background.

I couldn't find any answer that worked for C++, is there any way I can do this?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Dan
  • 63
  • 6
  • 1
    possible duplicate of [My application stealing focus](http://stackoverflow.com/questions/6406000/my-application-stealing-focus) – camelccc Jul 02 '15 at 19:14
  • What ways did you find that are wronmg for C++? –  Jul 02 '15 at 19:29
  • I've tried getting the current window handle and setting EnableWindow() to false, also SetActiveWindow(), none of them worked for me – Dan Jul 02 '15 at 19:37
  • How are you running the program? – Harry Johnston Jul 03 '15 at 04:02
  • I'm using a keyboard shortcut to run the .exe of the program, as soon as I press the keyboard keys my program starts(in the background) but my window loses focus – Dan Jul 03 '15 at 07:37
  • 1
    It will be the program which processes the keyboard shortcut (Explorer?) that is taking the focus. There probably isn't much you can do about that. – Harry Johnston Jul 03 '15 at 22:56

2 Answers2

2

When you start your application by clicking on the EXE or shortcut, Windows Explorer takes focus, not your app. The only way to start your app and not let Windows Explorer take focus is to start your program when Windows starts, via registry key.

0

Make sure you use the extended style WS_EX_NOACTIVATE when using CreateWindowEx(). See the Microsoft Docs for CreateWindowEx.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Menace
  • 1,061
  • 13
  • 15
  • I've seen the WS_EX_NOACTIVATE on a few other threads, but i'm not using CreateWindowEx(), i'm not creating a window at all... The application is hidden and runs in the background, but it still steals focus from the current window. I also edited the question to make it more clear – Dan Jul 02 '15 at 19:33
  • If you dont create a window , where does the focus go ? its just in limbo ? you type and it just goes no place ? That almost seems like a Windows bug. ::GetActiveWindow() and ::GetFocus() just return null ? weird. – Menace Apr 10 '19 at 16:37