如果将Windows窗体控件的Visible属性设置为true,则如果隐藏任何控件的父窗口,该属性仍将返回false.有没有办法在隐藏父窗口的情况下获取控件的真实底层可见性标志?
好吧,常规实现会检查控件堆栈,以确保所有父项都可见.我知道躲避这一切的唯一方法是用反思欺骗,并要求GetState(2)
,但这是脆弱的:
// dodgy; not recommended Panel query; Form form = new Form { Controls = { new Panel { Visible = false, Controls = { (query = new Panel {Visible = true}) } } } }; form.Show(); // this is the dodgy bit... bool visible = (bool)typeof(Control) .GetMethod("GetState", BindingFlags.Instance | BindingFlags.NonPublic) .Invoke(query, new object[] { 2 });