C# winform 检查控件是否物理可见
是否可以确定是否可以看到控件的至少一个像素(通过属性或可能使用事件通知)。
注意:我并不是在寻找即使其他窗口隐藏该控件也可以返回 true 的 Visible 属性
Is it possible to determine if at least one pixel of a control can be seen (by a property or maybe using event notification).
NB : I am not looking for the Visible property that can return true even if an other window hides the control
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
一个实用的解决方案是使用表单的 GetChildAtPoint() 方法,传递控件的 4 个角。如果其中之一返回 true,则该控件绝对可见。它不是 100% 可靠,所有 4 个角都可以被另一个控件重叠,但仍然保留部分内部可见。我不会担心这个,太奇怪了。
A pragmatic solution is to use the form's GetChildAtPoint() method, passing the 4 corners of the control. If one of them returns true then the control is definitely visible. It is not 100% reliable, all 4 corners could be overlapped by another control but still leave part of interior visible. I would not worry about that, too bizarre.
您可以使控件无效,然后调用 GetUpdateRect (Win32 api 函数)找出这一点。不过,它确实有导致重新绘制的副作用。
You can invalidate the control and then call GetUpdateRect (Win32 api function) to find this out. It does have the side effect of causing a repaint, though.
受到汉斯回答的启发,我以这种方式实现了这种行为;
Inspired by Hans's answer I've implemented this behavior in this way;
为了方便之前回答您的问题。
以下是使用 GetUpdateRect 函数作为 jdv-Jan de Vaan 回答。
当您需要检查指定是否可见时,只需执行如下操作:
祝您好运。
In order to facilitate a previous answer to your question.
Here is the source code that you will need to work with the GetUpdateRect function as jdv-Jan de Vaan answered.
When you need to check if a specified is visible just do something like the following:
Good luck.
如果控件可见,则将(重复)调用 Paint 事件。
通常对于不可见的控件,该事件不会被调用。
If a control is visible the Paint event will be called (repeatedly).
Normally for not visible controls, this event will not be called.
尝试了上面的方法,但即使 winform 被另一个应用程序覆盖,它仍然是正确的。
最终使用了以下内容(在我的 winform 类中):
Tried the above but kept getting true even if the winform was covered by another app.
Ended up using the following (inside my winform class):
您可以使用控件的布局事件。
当控件到达屏幕并尝试布局其子控件时会触发它。
例如,假设 TabPage 中有 GroupBox。
单击相关选项卡时,第一个选项卡页将触发布局事件,然后是 GroupBox
您可以将它与可见性属性结合使用
You may use Layout event of controls.
it is triggered when control comes to screen and tries to layout its child controls.
For example, let's say there is GroupBox inside a TabPage.
When relevant tab clicked, layout event will fire for first tabpage then for GroupBox
You may use it combined with visibility property
您可以检查父控件的可见性。
You can check for visibility of parent control.
我稍微完成了汉斯·帕桑特的回答。下面的函数测试表单的所有四个角。
I somewhat finished the answer by Hans Passant. The function below tests for all four corners of the form.