2011-06-16 19 views
7

WPF中的WebBrowser控件繼承自UIElement,但我們無法在UIElement事件中註冊事件處理程序。爲什麼?在WPF WebBrowser Mouse Events not working as expected,這是回答,但我仍然不明白。如何使用WPF WebBrowser中的'Back'和'Forward'導航按鈕事件?

無論如何,將處理程序連接到由WebBrowser文檔提供的事件可以捕獲大多數鼠標事件,但不能使用「後退」&「前進」導航按鈕事件。由於Internet Explorer可以做到這一點,我認爲這是可能的。有什麼辦法可以解決這個問題嗎?

UPDATE: 在這個問題中,'Back' & 'Forward' navigation buttons意味着XButton1和XButton2在5鍵鼠標系統。

UPDATE2:我用Navid Rahmani的答案解決了這個問題。我想有人會需要這個答案,所以我附上主要部分。如果發現任何問題或更合理的解決方案,請讓我知道。

//This code assumes the `WebBrowser` field named _webBrowser is already initiated. 
    //For the detail out of this code, please refer to the Navid Rahmani's answer. 

    private bool _isMouseOver; 
    private HTMLDocumentEvents2_Event _docEvent;  

    public ctor() 
    { 
     _webBrowser.LoadCompleted += _webBrowser_LoadCompleted; 
    } 

    private void _webBrowser_LoadCompleted(object sender, NavigationEventArgs e) 
    { 
     if (_docEvent != null) 
     { 
      _docEvent.onmouseover -= _docEvent_onmouseover; 
      _docEvent.onmouseout -= _docEvent_onmouseout; 
     } 
     if (_webBrowser.Document != null) 
     { 
      _docEvent = (HTMLDocumentEvents2_Event)_webBrowser.Document; 
      _docEvent.onmouseover += _docEvent_onmouseover; 
      _docEvent.onmouseout += _docEvent_onmouseout; 
     } 
    } 

    void _docEvent_onmouseout(IHTMLEventObj pEvtObj) 
    { 
     _isMouseOver = false; 
    } 

    void _docEvent_onmouseover(IHTMLEventObj pEvtObj) 
    { 
     _isMouseOver = true; 
    } 


    private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam) 
    { 
     if (_isMouseOver) 
     { 
      if (nCode >= 0 && (MouseMessages)wParam == MouseMessages.XBUTTON) 
      { 
       var hookStruct = (Msllhookstruct)Marshal.PtrToStructure(lParam, typeof(Msllhookstruct)); 
       if (hookStruct.mouseData == 0x10000) 
       { 
        //do something when XButto1 clicked 
       } 
       else if (hookStruct.mouseData == 0x20000) 
       { 
        //do something when XButto2 clicked 
       } 
      } 
     } 
     return CallNextHookEx(_hookID, nCode, wParam, lParam); 
    } 


    private enum MouseMessages 
    { 
     //WM_LBUTTONDOWN = 0x00A1, 
     //WM_LBUTTONUP = 0x0202, 
     //WM_MOUSEMOVE = 0x0200, 
     //WM_MOUSEWHEEL = 0x020A, 
     //WM_RBUTTONDOWN = 0x0204, 
     //WM_RBUTTONUP = 0x0205, 
     XBUTTON = 0x020B, 
    } 
+0

感謝您的更新 – 2011-06-20 09:27:40

+0

沒問題。這是通過你的努力來解決的。 – 2011-06-21 06:57:59

回答

2

你可以使用鼠標的較低水平掛鉤,並檢查xbutton1或xbutton2點擊
here

的價值WM_XBUTTONDOWNhttp://msdn.microsoft.com/en-us/library/ms646245(VS.85).aspx

+0

我嘗試了你的回答,但是'WM_LBUTTONDOWN'事件很酷,'XBUTTON'事件不是。我嘗試了'0x0001和0x0020'作爲'XBUTTON'事件的'MouseMessages enum'的值。我做錯了什麼? – 2011-06-18 17:26:03

+0

你是否檢查過高價位的文字?看看這個鏈接http://msdn.microsoft.com/en-us/library/ms997498.aspx – 2011-06-18 17:40:21

+0

不,你應該檢查'0x020C'和按鈕檢查高位字參數'0x0001'和'0x0002' – 2011-06-18 17:41:45

0

WebBrowser controll真的只是三叉戟COM對象的薄包裝。它不像其他內置控件那樣是「純WPF」......所以很多普通的東西都不適合它。爲了回答你的問題,你可以得到最接近的導航事件。這不會告訴你用戶是否嘗試前進或後退或其他地方,但它會給你的URL和機會來設置e.Cancel = true以停止導航(通常隨後調用Navigate(url)用戶在其他地方)。

+0

首先,我想感謝您的回答。然而,不管方向如何,我都無法用鼠標點擊「XButton」來註冊事件句柄到「WebBrowser」的導航事件。 – 2011-06-18 10:07:49

3

更簡單的方法....

這適用於我在WPF和.net 4.5

private void Window_MouseDown(object sender, MouseButtonEventArgs e) 
{ 
if (e.ChangedButton.Equals(MouseButton.XButton1)) MessageBox.Show(@"back"); 
if (e.ChangedButton.Equals(MouseButton.XButton2)) MessageBox.Show(@"forward"); 
} 
+1

MouseBrowser和PreviewMouseDown被WebBrowser控件吞噬,所以它不適用於我。 – Dave 2014-07-18 21:42:59