WPF WebBrowser support to increase / decrease support?

For the WPF WebBrowser control, is there a way to duplicate the scaling features of Internet Explorer?

In other words, Internet Explorer has a View> Zoom menu> 75%, which makes a web page at a scale of 75%. Is there a way to make a web browser control embedded in a WPF application do the same?

I saw this post: WPF WebBrowser - How to increase content?

But this only scales the page, not the content of the page.

+6
source share
4 answers
public partial class TestWindow: UserControl { public TestWindow() { InitializeComponent(); browser.LoadCompleted += new LoadCompletedEventHandler(browser_LoadCompleted); } private void browser_LoadCompleted(object sender, NavigationEventArgs e) { try { FieldInfo webBrowserInfo = browser.GetType().GetField("_axIWebBrowser2", BindingFlags.Instance | BindingFlags.NonPublic); object comWebBrowser = null; object zoomPercent = 120; if (webBrowserInfo != null) comWebBrowser = webBrowserInfo.GetValue(browser); if (comWebBrowser != null) { InternetExplorer ie = (InternetExplorer)comWebBrowser; ie.ExecWB(SHDocVw.OLECMDID.OLECMDID_OPTICAL_ZOOM, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, ref zoomPercent, IntPtr.Zero); } } catch (Exception ex) { } } public void SetBrowser(string url) { browser.Navigate(url,null,null,null); } internal void Destroy() { try { if (browser.Parent != null) { ((Grid)browser.Parent).Children.Remove(browser); browser.Navigate("about:blank"); browser.Dispose(); browser = null; } } catch { } } } 
+5
source

Here is how I did it:

 // Needed to expose the WebBrowser underlying ActiveX control for zoom functionality [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] [Guid("6d5140c1-7436-11ce-8034-00aa006009fa")] internal interface IServiceProvider { [return: MarshalAs(UnmanagedType.IUnknown)] object QueryService(ref Guid guidService, ref Guid riid); } static readonly Guid SID_SWebBrowserApp = new Guid("0002DF05-0000-0000-C000-000000000046"); private void ZoomListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { object zoomPercent; // A VT_I4 percentage ranging from 10% to 1000% switch(ZoomListBox.SelectedItem.ToString()) { case "System.Windows.Controls.ListBoxItem: 200%": zoomPercent = 200; break; case "System.Windows.Controls.ListBoxItem: 100%": zoomPercent = 100; break; case "System.Windows.Controls.ListBoxItem: 50%": zoomPercent = 50; break; default: zoomPercent = 100; break; } // grab a handle to the underlying ActiveX object IServiceProvider serviceProvider = null; if (m_webView.Document != null) { serviceProvider = (IServiceProvider)m_webView.Document; } Guid serviceGuid = SID_SWebBrowserApp; Guid iid = typeof(SHDocVw.IWebBrowser2).GUID; SHDocVw.IWebBrowser2 browserInst = (SHDocVw.IWebBrowser2)serviceProvider.QueryService(ref serviceGuid, ref iid); // send the zoom command to the ActiveX object browserInst.ExecWB(SHDocVw.OLECMDID.OLECMDID_OPTICAL_ZOOM, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, ref zoomPercent, IntPtr.Zero); } 

All of the content from the service provider is provided by ActiveX because the WPF WebBrowser control does not directly expose it. Other than that, it is almost the same as alexei's solution.

+4
source

This is not an exact answer, as it is designed to control WinForms, but it may be useful if you decide to use it in WindowsFormsHost instead of a WPF control, which helps too little to use.

You can use OLE commands through ExecWB in an ActiveX instance: OLECMDID_ZOOM for text size and OLECMDID_OPTICAL_ZOOM for optical zoom. For instance,

 object pvaIn = 200; // A VT_I4 percentage ranging from 10% to 1000% var browserInst = ((SHDocVw.IWebBrowser2)(browserContol.ActiveXInstance)); browserInst.ExecWB(SHDocVw.OLECMDID.OLECMDID_OPTICAL_ZOOM, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, ref pvaIn, IntPtr.Zero); 

Some notes:

  • reference to assembly Interop.SHDocVw required
  • the command was successful only after loading the document
  • pvaIn range can be obtained through OLECMDID_GETZOOMRANGE
  • for a reference list of commands located on MSDN
  • I experienced this strange behavior that seemed to occur only at non-96 dpi. At startup, the size of the displayed text does not match the one stored in the OLECMDID_ZOOM state. Setting a value (for any value) does not resolve the inconsistency: the rendering size still looks like [saved size + 2]. When the optical zoom was set to 100%, the difference in the text size went away (the text size decreased noticeably after scaling to 100%). This did not happen in IE, and maybe it was just a weird artifact in my environment, but just fyi.
+2
source

When using other solutions, I always get errors like

HRESULT: 0x80040100
DRAGDROP_E_NOTRector

I found a solution on this page that worked for me:

 var wb = webBrowser.ActiveXInstance.GetType(); object o = zoomPercentage; // Between 10 and 1000. wb.InvokeMember( @"ExecWB", BindingFlags.InvokeMethod, null, webBrowser.ActiveXInstance, new[] { OLECMDID.OLECMDID_OPTICAL_ZOOM, OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, o, o }); 

If OLECMDID_OPTICAL_ZOOM is 63 and OLECMDEXECOPT_DONTPROMPTUSER is 2.

0
source

Source: https://habr.com/ru/post/890726/


All Articles