How to detect if autohidden taskbar is visible or

2019-01-26 02:02发布

问题:

At the moment I need to detect in C++/Qt if a taskbar, which is set to "autohide" is visible on the screen or not. I have tried already following solution, unfortunately with no success:

  1. Checked the autohide state with uState = (UINT) SHAppBarMessage(ABM_GETSTATE, pabd), this only returns whether autohide property is set or not

  2. Getting work area with SystemParametersInfo(SPI_GETWORKAREA, 0, &rectWorkArea, 0); Unfortunately the work area is always of size of the entire screen, when taskbar is set to "autohiden", even if it is actually visible on the screen

  3. Geting AppBarData with SHAppBarMessage(ABM_GETTASKBARPOS, &abd); With this function I can get both size and coordinates of the taskbar, however they are always returned as if the taskbar is being visible, even if it is hidden.

So with those methods I cannot tell, whether taskbar with "autohide" on is at given moment visible on the screen or not :-(

I would appreciate any help :-)

回答1:

HWND hTaskbarWnd = FindWindow("Shell_TrayWnd", null);
bool isVisible = IsWindowVisible(hTaskbarWnd);

or

bool IsTaskbarWndVisible() {
HWND hTaskbarWnd = FindWindow("Shell_TrayWnd", null);
HMONITOR hMonitor = MonitorFromWindow(hTaskbarWnd , MONITOR_DEFAULTTONEAREST);
MONITORINFO info = { sizeof(MONITORINFO) };
if (GetMonitorInfo(hMonitor, &info))
{
  RECT rect;
  GetWindowRect(hTaskbarWnd , &rect);
  if ((rect.top >= info.rcMonitor.bottom - 4) ||
      (rect.right <= 2) ||
      (rect.bottom <= 4) ||
      (rect.left >= info.rcMonitor.right - 2))
  return false;

  return true;
}