2010-02-25 62 views

回答

2

做到這將是使周圍MessageBox.Show跟蹤在Shared財產呼叫自己的包裝最簡單的方法,然後專門調用它,而不是MessageBox.ShowMsgBox

+0

我不知道爲什麼System.Windows.Forms.MessageBox擁有一個私人的構造函數。如果你可以繼承基本實現並更改hide/show方法,那將會容易得多。 – 2010-02-25 19:20:01

+0

這是一個共享類。那是不可能的。 – SLaks 2010-02-25 19:23:02

+0

好點。這就說得通了。 – 2010-02-25 19:36:34

2

這是可能的,但需要相當大的P/Invoke服務。訣竅是枚舉UI線程擁有的窗口,並檢查它們中的一個是否是Windows對話窗口。這段代碼將會訣竅。我無法保證100%的準確性,應用程序中可能會有另一個非託管對話框,類似於消息框模板。

using System; 
using System.Text; 
using System.Runtime.InteropServices; 

static class MessageBoxFinder { 
    public static bool IsPresent() { 
    // Enumerate windows to find the message box 
    EnumThreadWndProc callback = new EnumThreadWndProc(checkWindow); 
    return false == EnumThreadWindows(GetCurrentThreadId(), callback, IntPtr.Zero); 
    } 
    private static bool checkWindow(IntPtr hWnd, IntPtr lp) { 
    // Checks if <hWnd> is a dialog 
    StringBuilder sb = new StringBuilder(260); 
    GetClassName(hWnd, sb, sb.Capacity); 
    if (sb.ToString() != "#32770") return true; 
    // Got a dialog, check if the the STATIC control is present 
    IntPtr hText = GetDlgItem(hWnd, 0xffff); 
    return hText == IntPtr.Zero; 
    } 
    // P/Invoke declarations 
    private delegate bool EnumThreadWndProc(IntPtr hWnd, IntPtr lp); 
    [DllImport("user32.dll")] 
    private static extern bool EnumThreadWindows(int tid, EnumThreadWndProc callback, IntPtr lp); 
    [DllImport("kernel32.dll")] 
    private static extern int GetCurrentThreadId(); 
    [DllImport("user32.dll")] 
    private static extern int GetClassName(IntPtr hWnd, StringBuilder buffer, int buflen); 
    [DllImport("user32.dll")] 
    private static extern IntPtr GetDlgItem(IntPtr hWnd, int item); 
} 
0

我想通了,最簡單的答案是使用這個和尋找消息框的標題:

<System.Runtime.InteropServices.DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _ 
Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr 
End Function