Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Draw the window on top of other applications  (Read 13242 times)

0 Members and 2 Guests are viewing this topic.

ZeroZ30o

  • Newbie
  • *
  • Posts: 47
    • View Profile
Draw the window on top of other applications
« on: January 22, 2019, 11:03:14 pm »
Is there a way to draw a RenderWindow in front of all other windows?

So my program is running in the background, and wants to show something in a window -it would create one to do so, but that window will, in many cases, still be hidden behind other applications. Is there a way to make it show on top of all other applications?

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Draw the window on top of other applications
« Reply #1 on: January 23, 2019, 12:00:28 am »
Does requestFocus do what you want?

Actually at least on Windows, it won't bring a window to front so on Windows try handle from getSystemHandle with: https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-bringwindowtotop

« Last Edit: January 23, 2019, 12:17:00 am by FRex »
Back to C++ gamedev with SFML in May 2023

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Draw the window on top of other applications
« Reply #2 on: January 23, 2019, 12:29:49 am »
Force to top was not implemented on purpose as focus stealing is generally a bad user experience.

You can request focus with the aforementioned function. On Windows it usually just makes the tab in the tab bar blink.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Draw the window on top of other applications
« Reply #3 on: January 23, 2019, 12:41:17 am »
On Windows BringWindowToTop will put the window on top but it'll not steal focus/input. And yes, it's obnoxious.
Back to C++ gamedev with SFML in May 2023

ZeroZ30o

  • Newbie
  • *
  • Posts: 47
    • View Profile
Re: Draw the window on top of other applications
« Reply #4 on: January 23, 2019, 10:25:44 am »
Thanks.

As for it being obnoxious -it's a non-moveable window that is only meant to act as a "hud". It would be even better if I could make the background of the window semi-transparent so that it would cover even less space.

I do get that some apps can choose to make annoying pop-ups or games that will move themselves in front, but the feature is still useful when used properly.

And to be honest, I don't get the "don't add a feature because some people will misuse it", after all, c++'s mentality with the standard is quite the opposite: make hundreds of tools available to anyone, even if they end up misused.

Buuut I don't care about that, thanks again.

ZeroZ30o

  • Newbie
  • *
  • Posts: 47
    • View Profile
Re: Draw the window on top of other applications
« Reply #5 on: January 23, 2019, 11:03:17 am »
I'm running into errors:

When including <Windows.h> and <WinUser.h> along with <SFML/Graphics.hpp>, I get error C2589, "'(' illegal token on right side of '::'" in rect.inl.

Other errors that don't show up in output (in visual studio) include "Rect is not a template", "Vector2 is not a template".

I'm assuming this is a name clash due to improper namespace usage, but don't know how to fix it properly:

I've attempted doing "namespace nameShield { #include <SFML/Graphics.hpp> }", but then the compiler will error with ""nameShield::operator new" non-member operator new or delete functions may not be declared static or in a namespace other than the global namespace", and other very similar errors with new[], delete, round...
« Last Edit: January 23, 2019, 11:13:22 am by ZeroZ30o »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Draw the window on top of other applications
« Reply #6 on: January 23, 2019, 11:18:42 am »
IIRC Windows headers #define Top and Bottom somewhere.
One option is to include the Windows header after the SFML header, another solution is to #undef Top and Bottom after the Windows header inclusion.

See Laurent's response below

And to be honest, I don't get the "don't add a feature because some people will misuse it", after all, c++'s mentality with the standard is quite the opposite: make hundreds of tools available to anyone, even if they end up misused.
SFML is not a language, but a library and every library maintainer can make their own rules of what to dis-/allow and what guidelines to follow. "But others have it" is never a real argument. ;)

And don't forget, that SFML isn't trying to be a window-manager, so if you have a need to move & arrange windows, then SFML may not be a fitting library.
« Last Edit: January 23, 2019, 11:24:58 am by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Draw the window on top of other applications
« Reply #7 on: January 23, 2019, 11:22:26 am »
windows.h defines the min and max macros.

The cleanest solution is to define the NOMINMAX preprocessor symbol in your project settings.
Laurent Gomila - SFML developer

ZeroZ30o

  • Newbie
  • *
  • Posts: 47
    • View Profile
Re: Draw the window on top of other applications
« Reply #8 on: January 23, 2019, 11:30:28 am »
windows.h defines the min and max macros.

The cleanest solution is to define the NOMINMAX preprocessor symbol in your project settings.

Thanks, that worked, tough I have no clue as to why.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Draw the window on top of other applications
« Reply #9 on: January 23, 2019, 11:33:35 am »
Because windows.h doesn't define those macros if the NOMINMAX preprocessor symbol is defined :)

#ifndef NOMINMAX
   #define min ...
   #define max ...
#endif
Laurent Gomila - SFML developer

ZeroZ30o

  • Newbie
  • *
  • Posts: 47
    • View Profile
Re: Draw the window on top of other applications
« Reply #10 on: January 23, 2019, 11:36:20 am »
Because windows.h doesn't define those macros if the NOMINMAX preprocessor symbol is defined :)

#ifndef NOMINMAX
   #define min ...
   #define max ...
#endif

Sure, but how does that fix it? Does SFML define them too?

ZeroZ30o

  • Newbie
  • *
  • Posts: 47
    • View Profile
Re: Draw the window on top of other applications
« Reply #11 on: January 23, 2019, 11:46:51 am »
"BringWindowToTop()" does not work.

Instead,
"SetWindowPos(window.getSystemHandle(), HWND_TOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE | SWP_ASYNCWINDOWPOS | SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE);"
should be used. I have no clue why setting the positions to 0 works, nor what the flags are. But it works.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Draw the window on top of other applications
« Reply #12 on: January 23, 2019, 11:50:30 am »
Quote
Sure, but how does that fix it? Does SFML define them too?
No, but SFML uses std::min and std::max. If the preprocessor replaces 'min' and 'max' with the Windows macros, the result is an invalid C++ expression.
Laurent Gomila - SFML developer

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Draw the window on top of other applications
« Reply #13 on: January 23, 2019, 02:22:17 pm »
Quote
"BringWindowToTop()" does not work.
Yes, it turns out it only worked when I had focus in the console of the program actually.

Quote
SetWindowPos(window.getSystemHandle(), HWND_TOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE | SWP_ASYNCWINDOWPOS | SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE);
Yes, it works, where did you get that if you don't know these flags? You can read what flags do (NOMOVE and NOSIZE are obvious and that's why those 4 zeroes don't do anything and TOPMOST is why the window ends up on top) on: https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-setwindowpos
Back to C++ gamedev with SFML in May 2023