2012-03-28 179 views
0

我想弄清楚如果一個窗體窗體控件對用戶是可見的,或者被另一個控件或窗體(選項卡式視圖)阻止查看。我已經嘗試過GetUpdateRect技巧,但它只適用於窗口最小化的情況。我找到RectVisible函數,但不確定如何使用它從Windows窗體用戶控件。在Windows窗體控件上使用RectVisible

由於提前

回答

0

我不知道你所說的「如果窗口最小化只適用」的意思。 GetUpdateRect解決方案的工作原理如下:

[StructLayout(LayoutKind.Sequential)] 
internal struct RECT 
{ 
    public int Left; 
    public int Top; 
    public int Right; 
    public int Bottom; 
    public int Width { get { return this.Right - this.Left; } } 
    public int Height { get { return this.Bottom - this.Top; } } 
} 

[DllImport("user32.dll")] 
internal static extern bool GetUpdateRect(IntPtr hWnd, ref Rect rect, bool bErase); 

public static bool IsControlVisibleToUser(Control control) 
{ 
    control.Invalidate(); 
    var bounds = control.Bounds; 
    var rect = new Rect {Left=bounds.Left, Right = bounds.Right, Top = bounds.Top, Bottom = bounds.Bottom}; 
    return GetUpdateRect(control.Handle, ref rect, false); 
} 
+0

我有兩個用戶控件託管在選項卡控件的不同選項卡頁面上。當我使用您的代碼測試它們時,它們都會顯示可見,即使一次只有一個活動選項卡 – unclepaul84 2012-03-28 21:51:02

+0

@ unclepaul84我無法複製您的失敗。您正在調用Is ... Visible兩次 - 每個用戶控制變量一次?你什麼時候打電話? – 2012-03-28 23:43:52

+0

我使用control.Invoke每10秒從一個計時器線程調用它 – unclepaul84 2012-03-29 13:59:18