你可以在這裏嘗試使用Windows鉤子。找到瀏覽器窗口併爲其安裝正確的鍵盤鉤子。這應的外部對你會有所幫助:
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, IntPtr windowTitle);
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
private static extern IntPtr SetWindowsHookEx(Int32 idHook, HookProc lpfn, IntPtr hInstance, Int32 threadId);
這裏是一個示例如何使用這些,但你必須根據自己的需要來實現KeyboardHookProcedure。
private IntPtr FindExplorerWindow()
{
IntPtr wnd = Browser.Handle;
if (wnd != IntPtr.Zero)
{
wnd = FindWindowEx(wnd, IntPtr.Zero, "Shell DocObject View", IntPtr.Zero);
if (wnd != IntPtr.Zero)
{
wnd = FindWindowEx(wnd, IntPtr.Zero, "Internet Explorer_Server", IntPtr.Zero);
return wnd;
}
}
return IntPtr.Zero;
}
private void InstallHook()
{
if (_hHook.ToInt32() > 0) return;
IntPtr wnd = FindExplorerWindow();
if (wnd != IntPtr.Zero)
{
if (_hookProc == null)
{
_hookProc = new HookProc(KeyboardHookProcedure);
}
_hHook = SetWindowsHookEx(WH_KEYBOARD, _hookProc, (IntPtr)0, GetCurrentThreadId());
}
}
祝你好運!
這不是不可能的,它只是在WPF很多醜陋的。在谷歌查詢中鍵入「wpf newwindow2」,採取第一擊。 – 2012-04-05 15:50:36