C# winform check if control is physicaly visible

后端 未结 9 1641
有刺的猬
有刺的猬 2020-12-09 01:58

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 p

9条回答
  •  遥遥无期
    2020-12-09 02:59

    I somewhat finished the answer by Hans Passant. The function below tests for all four corners of the form.

            /// 
        /// determines if a form is on top and really visible.
        /// a problem you ran into is that form.invalidate returns true, even if another form is on top of it. 
        /// this function avoids that situation
        /// code and discussion:
        /// https://stackoverflow.com/questions/4747935/c-sharp-winform-check-if-control-is-physicaly-visible
        /// 
        /// 
        /// 
        public bool ChildReallyVisible(Control child)
        {
            bool result = false;
    
            var pos = this.PointToClient(child.PointToScreen(Point.Empty));
    
            result = this.GetChildAtPoint(pos) == child;
            //this 'if's cause the condition only to be checked if the result is true, otherwise it will stay false to the end
            if(result)
            {
                result = (this.GetChildAtPoint(new Point(pos.X + child.Width - 1, pos.Y)) == child);
            }
            if(result)
            {
                result = (this.GetChildAtPoint(new Point(pos.X, pos.Y + child.Height - 1)) == child);
            }
            if(result)
            {
                result = (this.GetChildAtPoint(new Point(pos.X + child.Width - 1, pos.Y + child.Height - 1)) == child) ;
            }
            return result;
        }
    

提交回复
热议问题