16

I am trying to determine a window control's visibility that has been hidden or enabled with CWnd::ShowWindow(). (or ::ShowWindow(hWnd,nCmdShow))

I cannot simply use ::IsWindowVisible(hWnd) as the control is on a tab sheet, which may itself be switched out, causing IsWindowVisible to return FALSE.

Is there a way to get the SW_SHOW/HIDE (or others) window status or do I need to use the retun value of ShowWindow() and reset accordingly?

edit: as the control is enabled (or disabled) to show, but may not be currently visible, as the tab is switched ot, I would think that it's SW_SHOW status would remain the same, even if the window itself is not actually switched in. If I'm unrealistic in my expectations that that's fine.

So really I'm looking for 'can this window/control be shown'

geocoin
  • 1,281
  • 1
  • 14
  • 30

6 Answers6

14

Call GetWindowLong( handle, GWL_STYLE), check the returned value for WS_VISIBLE style presence.

sharptooth
  • 167,383
  • 100
  • 513
  • 979
  • 2
    surely this is the same as IsWindowVisible - which I already said was giving me the wrong result (for this situation) – geocoin Sep 16 '09 at 11:38
  • 4
    MSDN is your friend ( http://msdn.microsoft.com/en-us/library/ms633530(v=vs.85).aspx ) - IsWindowVisible checks the WS_VISIBLE style for the window and all the ancestors ("If the specified window, its parent window, its parent's parent window, and so forth, have the WS_VISIBLE style, the return value is nonzero. Otherwise, the return value is zero."), but this just checks only the window's own state. So consider this to be "local" visibility of that HWND relative to its parent (which sounds like what you were looking for); while IsWindowVisible is the net "global" result. – BrendanMcK Jul 23 '11 at 05:05
  • 1
    This is the right approach to use; `GetWindowPlacement` is not as accurate. I've found that if you have a dialog with controls flagged as "Visible: False" and you don't otherwise call `ShowWindow` on them, `GetWindowPlacement` on those controls will still return `SW_SHOWNORMAL` in the `showCmd` field. – StilesCrisis Mar 26 '13 at 22:17
7

Use GetWindowPlacement. It fills WINDOWPLACEMENT structure, which has field showCmd.

showCmd
Specifies the current show state of the window. This member can be one of the following values.

Midas
  • 7,012
  • 5
  • 34
  • 52
Kirill V. Lyadvinsky
  • 97,037
  • 24
  • 136
  • 212
  • 1
    I've found that if you have a dialog with controls flagged as "Visible: False" and you don't otherwise call `ShowWindow` on them, `GetWindowPlacement` on those controls will still return `SW_SHOWNORMAL` in the `showCmd` field. The `GetWindowLong(wnd, GWL_STYLE)` approach works correctly. – StilesCrisis Mar 26 '13 at 22:17
  • IsWindowVisible worked better for me based on this answer: https://stackoverflow.com/a/16946171/3950759 – mitch Dec 02 '18 at 07:02
2

I would use GetWindowPlacement, however I am not sure if I understood what you want. It fills in a WINDOWPLACEMENT structure and then use the showCmd member.

jdehaan
  • 19,700
  • 6
  • 57
  • 97
0

GetWindowPlacement() function will work only if the window is maximized or minimized. Otherwise, showCmd member will return SW_SHOWNORMAL also when window is hidden, as pointed out in this StackOverflow answer: WINDOWPLACEMENT's showCmd... always 1?

You can use the more straightforward boolean function IsWindowVisible() to get if the specified Window is in a visible state or not.

Community
  • 1
  • 1
Flavio
  • 451
  • 3
  • 26
0

If it is a multi-tab dialog and not a control, then override as

void MyClass::OnShowWindow(BOOL bShow, UINT nStatus)
{
    m_nCmdShow = bShow;
    CDialog::OnShowWindow(bShow, nStatus);
}

In BEGIN_MESSAGE_MAP, add ON_WM_SHOWWINDOW().

m_nCmdShow now has the status if the window is SW_SHOW or SW_HIDE.

Irfan
  • 51
  • 1
  • 3
-1

I don't know if there is a proper method for this task but I would try WindowFromPoint Function.

Remarks

The WindowFromPoint function does not retrieve a handle to a hidden or disabled window, even if the point is within the window. An application should use the ChildWindowFromPoint function for a nonrestrictive search.

For example I would convert control's corner coords into screen coords and then try to get it's handle from those points.

Nick Dandoulakis
  • 42,588
  • 16
  • 104
  • 136
  • This may cause unexpected results with controls like groupboxes which can overlap their siblings and appear to visually contain them. Will also break if there's an unrelated popup that just happens to be on the wrong place in the screen at the wrong time. – BrendanMcK Jul 23 '11 at 04:59
  • @Brendan, you're right about that. I should had mentioned that's not a reliable method. – Nick Dandoulakis Jul 23 '11 at 12:30