5

How can I check if an other app is running in full screen mode & topmost in c++ MFC? I just want to disable all of my auto dialogs (warnings) if media player or other players are running. (Like silent/gamer mode in Avast.) How could I do that?

Thank you.

2 Answers 2

8

using a combination of EnumWindows, GetWindowInfo and GetWindowRect does the trick.

bool IsTopMost( HWND hwnd )
{
  WINDOWINFO info;
  GetWindowInfo( hwnd, &info );
  return ( info.dwExStyle & WS_EX_TOPMOST ) ? true : false;
}

bool IsFullScreenSize( HWND hwnd, const int cx, const int cy )
{
  RECT r;
  ::GetWindowRect( hwnd, &r );
  return r.right - r.left == cx && r.bottom - r.top == cy;
}

bool IsFullscreenAndMaximized( HWND hwnd )
{
  if( IsTopMost( hwnd ) )
  {
    const int cx = GetSystemMetrics( SM_CXSCREEN );
    const int cy = GetSystemMetrics( SM_CYSCREEN );
    if( IsFullScreenSize( hwnd, cx, cy ) )
      return true;
  }
  return false;
}

BOOL CALLBACK CheckMaximized( HWND hwnd, LPARAM lParam )
{
  if( IsFullscreenAndMaximized( hwnd ) )
  {
    * (bool*) lParam = true;
    return FALSE; //there can be only one so quit here
  }
  return TRUE;
}

bool bThereIsAFullscreenWin = false;
EnumWindows( (WNDENUMPROC) CheckMaximized, (LPARAM) &bThereIsAFullscreenWin );

edit2: updated with tested code, which works fine here for MediaPlayer on Windows 7. I tried with GetForeGroundWindow instead of the EnumWindows, but then the IsFullScreenSize() check only works depending on which area of media player the mouse is in exactly.

Note that the problem with multimonitor setups mentioned in the comment below is still here.

6
  • So I have to forget your sample code, and should concentrate to these new functions?
    – TlMPEER
    Sep 26, 2010 at 14:07
  • depends on what behaviour you want, there are some caveats: suppose a dual monitor system, an app can be fullscreen on screen 1, but that does not necessarily mean it also has focus since the user can be using another app on screen 2 at the same time. So you have to decide what to do in that case as well..
    – stijn
    Sep 26, 2010 at 14:21
  • The IsFullscreenAndMaximized function returns always TRUE, why?
    – TlMPEER
    Sep 26, 2010 at 16:20
  • Ok, thank you! It's almost done. If I open the Task manager and I maximize it, the function works, but if I open a media player and make it to full screen, it doesn't work. Only if I maximize it and then I make it to full screen. Should I only focus to the WS_EX_TOPMOST ?
    – TlMPEER
    Sep 26, 2010 at 16:47
  • Will not this just compare against the primary screen? If another monitor is introduced with a different resolution I cannot see how this will check that. For a more thorough comparison you would need to compare against the monitor the HWND is on. Feb 22, 2016 at 23:34
0

in my oppinion a very little improvement

bool AreSameRECT (RECT& lhs, RECT& rhs){
    return (lhs.bottom == rhs.bottom && lhs.left == lhs.left && lhs.right == rhs.right && lhs.top == rhs.top) ? true : false;
}


bool IsFullscreenAndMaximized(HWND hWnd)
{
    RECT screen_bounds;
    GetWindowRect(GetDesktopWindow(), &screen_bounds);

    RECT app_bounds;
    GetWindowRect(hWnd, &app_bounds);

    if(hWnd != GetDesktopWindow() && hWnd != GetShellWindow()) {
        return AreSameRECT(app_bounds, screen_bounds);
    }

    return false;
}

And thanks to priviose answer

BOOL CALLBACK CheckFullScreenMode ( HWND hwnd, LPARAM lParam )
{
    if( IsFullscreenAndMaximized(GetForegroundWindow()) )
    {
        * (bool*) lParam = true;
        std::cout << "true";

        return FALSE; 
    }
    return TRUE;
}


int main() {

    bool bThereIsAFullscreenWin = false;
    EnumWindows( (WNDENUMPROC) CheckFullScreenMode, (LPARAM) &bThereIsAFullscreenWin );
}
2
  • What about multi-monitor setups? How do you distinguish between a window, that has been resized to cover an entire display, and a window that is truly in fullscreen mode? Have you verified that the window rectangle of a maximized window really is the same as the window rectangle of the desktop? Jul 21, 2015 at 8:27
  • Note: (lhs.left == lhs.left) should be (lhs.left == rhs.left) in AreSameRECT
    – msoft
    Feb 18, 2018 at 19:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.