How to check if an other program is running in fullscreen mode, eg. a media player

谁说胖子不能爱 提交于 2019-12-17 19:06:32

问题


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.


回答1:


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.




回答2:


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 );
}


来源:https://stackoverflow.com/questions/3797802/how-to-check-if-an-other-program-is-running-in-fullscreen-mode-eg-a-media-play

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!