C++ 如何查找窗口的 SW_SHOW/SW_HIDE 状态

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1432336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:57:18  来源:igfitidea点击:

how to find a window's SW_SHOW/SW_HIDE status

c++winapimfcshow-hide

提问by geocoin

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

我正在尝试确定已使用 CWnd::ShowWindow() 隐藏或启用的窗口控件的可见性。(或 ::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.

我不能简单地使用 ::IsWindowVisible(hWnd),因为控件位于选项卡表上,它本身可能会被关闭,导致 IsWindowVisible 返回 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?

有没有办法获得 SW_SHOW/HIDE(或其他)窗口状态,或者我是否需要使用 ShowWindow() 的返回值并相应地重置?

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.

编辑:由于控件已启用(或禁用)以显示,但当前可能不可见,因为选项卡已切换,我认为它的 SW_SHOW 状态将保持不变,即使窗口本身并未实际切换. 如果我的期望不切实际,那很好。

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

所以我真的在寻找'可以显示这个窗口/控件吗'

采纳答案by Kirill V. Lyadvinsky

Use GetWindowPlacement. It fills WINDOWPLACEMENTstructure, which has field showCmd.

使用GetWindowPlacement。它填充WINDOWPLACEMENT结构,该结构具有字段showCmd

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

showCmd
指定窗口的当前显示状态。此成员可以是以下值之一。

回答by sharptooth

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

调用GetWindowLong( handle, GWL_STYLE),检查 WS_VISIBLE 样式存在的返回值。

回答by jdehaan

I would use GetWindowPlacement, however I am not sure if I understood what you want. It fills in a WINDOWPLACEMENTstructure and then use the showCmdmember.

我会使用GetWindowPlacement,但是我不确定我是否理解你想要的。它填充一个WINDOWPLACEMENT结构,然后使用该showCmd成员。

回答by Flavio

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?

GetWindowPlacement() 函数只有在窗口最大化或最小化时才会起作用。否则,showCmd 成员在窗口被隐藏时也将返回 SW_SHOWNORMAL,正如 StackOverflow 回答中所指出的: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.

您可以使用更直接的布尔函数IsWindowVisible() 来获取指定的 Window 是否处于可见状态。

回答by Irfan

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().

在 BEGIN_MESSAGE_MAP 中,添加 ON_WM_SHOWWINDOW()。

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

如果窗口是 SW_SHOW 或 SW_HIDE,m_nCmdShow 现在具有状态。

回答by Nick Dandoulakis

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

我不知道这个任务是否有合适的方法,但我会尝试WindowFromPoint 函数。

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.

评论

WindowFromPoint 函数不会检索隐藏或禁用窗口的句柄,即使该点位于窗口内。应用程序应使用 ChildWindowFromPoint 函数进行非限制性搜索。

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

例如,我会将控件的角坐标转换为屏幕坐标,然后尝试从这些点获取它的句柄。