我有2個可見的窗體。是否有可能檢測到消息框是否可見/顯示在另一個表單上?如何檢測消息框是否可見?
1
A
回答
2
做到這將是使周圍MessageBox.Show
跟蹤在Shared
財產呼叫自己的包裝最簡單的方法,然後專門調用它,而不是MessageBox.Show
和MsgBox
。
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
相關問題
- 1. 如何檢測mediacontroller是否可見?
- 2. 如何檢測SWT對話框是否已打開並可見?
- 3. 是否可以從SQS中檢索消息但不可見?
- 4. 消息框並不總是可見
- 5. 檢測是否有任何打開的確認消息框
- 6. 檢查窗口是否是消息框
- 7. 檢測元素是否真的可見
- 8. 檢測窗口是否可見
- 9. 是否可以檢查通知是否可見或取消?
- 10. Android M檢測權限對話框是否可見
- 11. 如何檢查圖像是否可見?
- 12. 如何檢查JOptionPane是否可見?
- 13. PyQt - 如何檢查QDialog是否可見?
- 14. 如何檢查AdView是否可見?
- 15. 如何檢查EKCalendar是否可見
- 16. 如何檢查NSWindow是否可見
- 17. Selenium C#Webdriver如何檢測元素是否可見
- 18. 如何使用PHP檢測元素是否可見?
- 19. 如何檢測Unity3D應用程序的狀態 - 是否可見
- 20. 如何檢測自動隱藏的任務欄是否可見?
- 21. 如何從IOS上的JS檢測瀏覽器是否可見?
- 22. Magento函數getMessages - 如何檢測消息是否已設置
- 23. 是否可以在protobuf-csharp-port中檢測消息類型?
- 24. wp7:如何檢查進度條是否可見或不可見?
- 25. 檢查UIAlertView是否可見
- 26. 如何使用jQuery在聊天中檢查消息狀態「消息可見」
- 27. 敲除檢測是否有任何在foreach中可見
- 28. 如何檢測類是否可注入
- 29. 如何檢測mod_rewrite是否可用?
- 30. 檢測消息框的創建
我不知道爲什麼System.Windows.Forms.MessageBox擁有一個私人的構造函數。如果你可以繼承基本實現並更改hide/show方法,那將會容易得多。 – 2010-02-25 19:20:01
這是一個共享類。那是不可能的。 – SLaks 2010-02-25 19:23:02
好點。這就說得通了。 – 2010-02-25 19:36:34