的唯一可靠方法似乎是掛接到Windows事件隊列和抑制對話框(如各種事情可以獲得用戶訪問權限)。這是我們的輔助類做什麼:
void ListenForDialogCreation()
{
// Listen for name change changes across all processes/threads on current desktop...
_WinEventHook = WinAPI.SetWinEventHook(WinAPI.EVENT_OBJECT_CREATE, procDelegate);
}
void StopListeningForDialogCreation()
{
WinAPI.UnhookWinEvent(_WinEventHook);
}
void WinEventProc(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
{
const uint OBJID_WINDOW = 0;
const uint CHILDID_SELF = 0;
// filter out non-HWND, and things not children of the current application
if (idObject != OBJID_WINDOW || idChild != CHILDID_SELF)
return;
//Get the window class name
StringBuilder ClassName = new StringBuilder(100);
WinAPI.GetClassName(hwnd, ClassName, ClassName.Capacity);
// Send close message to any dialog
if (ClassName.ToString() == "#32770")
{
WinAPI.SendMessage(hwnd, WinAPI.WM.CLOSE, IntPtr.Zero, IntPtr.Zero);
if (OnDialogCancelled != null)
OnDialogCancelled();
}
if (ClassName.ToString() == "#32768")
{
WinAPI.SendMessage(hwnd, WinAPI.WM.CLOSE, IntPtr.Zero, IntPtr.Zero);
if (OnDialogCancelled != null)
OnDialogCancelled();
}
}
public delegate void OnDialogCancelledEvent();
public event OnDialogCancelledEvent OnDialogCancelled;
- #32770是Dialog類
- #32768是彈出菜單
- 的WinAPI的命名空間是我們的PInvoke包裝。
如果您不希望阻止所有對話框,您將在添加一些額外的過濾器後添加到該類中。這取決於你需要的安全程度。在$ WORK中,我們需要阻止所有上傳和下載。
抑制彈出式菜單是必要的,因爲它允許訪問幫助應用程序,該應用程序可以鏈接到microsoft的網站,從而可以啓動完整的IE實例。然後他們可以做任何他們想做的事。
讓我得到這個海峽......你想允許你的用戶訪問互聯網(這不過是把文件下載到電腦裏),你想阻止所有的下載? – Sergio 2009-01-27 12:56:13
@Sergio:我想,Jens想阻止所有文件,這些文件不能直接在webbrowser中顯示。 – TcKs 2009-01-27 14:05:41
TcKs是正確的,我想阻止無法顯示的所有內容。重點不在於防止下載,而是爲了防止任何「保存文件爲」對話框顯示,以便用戶無法訪問硬盤。我的應用程序被安裝爲Windows外殼程序(沒有資源管理器,沒有開始菜單)。 – 2009-01-29 06:30:10