How to use the Back and Forward navigation events in WPF WebBrowser?

The WPF WebBrowser element is inherited from UIElement , but we cannot register event handlers in UIElement events. Why is this? In Mouse Events, WPF WebBrowser is not working properly , responding to it, but I still can not understand.

In any case, connecting handlers to the events provided by the WebBrowser document can capture most mouse events, but cannot use the Back and Forward events. Since an Internet researcher can do this, I think it is possible. Is there any way to solve this problem?

UPDATE: In this question, 'Back' & 'Forward' navigation buttons means XButton1 and XButton2 in a 5-button mouse system.

UPDATE2: I fixed this question with the answer of Navid Rahmani. I think someone will need this answer, so I am attaching the main part. If you find any problem or more reasonable solution, let me know.

  //This code assumes the `WebBrowser` field named _webBrowser is already initiated. //For the detail out of this code, please refer to the Navid Rahmani 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, } 
+7
source share
3 answers

You can use the low-level hook of the mouse and check if xbutton1 or xbutton2 is clicked

look here

for WM_XBUTTONDOWN value see http://msdn.microsoft.com/en-us/library/ms646245(VS.85).aspx

+2
source

Much easier ...

This works for me in WPF and .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"); } 
+3
source

The WebBrowser control is a really thin shell around the Trident COM object. It is not "pure WPF" like other built-in controls ... so many normal things do not work with it. To answer your question, the closest you can connect to the navigation event. This will not tell you whether the user will try to go forward or backward or to another place, but he will give you the URL and the ability to set e.Cancel = true to stop navigation (usually it should be followed by navigation (url) the user somewhere something else).

0
source

All Articles