使用C#放大Web浏览器控件 [英] Zoom in Web Browser Control with C#

查看:81
本文介绍了使用C#放大Web浏览器控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨..



我的应用程序有一个表格,上面有两个网页浏览器控件,

我想放大/缩小设施两个人。



那么,我怎样才能在c#中做到这一点??



我发现了一个与此相关的链接,但它在VB.net中

将ExecWB与原生.NET 2.0 WebBrowser控件一起使用 [ ^ ]



请帮忙...

解决方案

我有同样的问题,在googleing时我找到了这个页面。

我尝试了Valery发布的代码:它非常有用(明确地说是一个主要的暗示,谢谢Valery!),虽然它并不完全是我追求的。

经过更多googleing后我找到了这个链接: http://support.microsoft.com/kb/304103

确实有帮助,即使代码是在VB.NET中。

基本上,诀窍是使用 OLECMDID.OLECMDID_ZOOM 而不是 OLECMDID.OLECMDID_OPTICAL_ZOOM

所以我玩了一点,最后得到了这个。

就像魅力一样。



 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用 System.Windows.Forms;
使用 SHDocVw;

命名空间 ColorCodeTest
{
public partial class MyBrowser:System.Windows.Forms.WebBrowser
{
private IWebBrowser2 axIWebBrowser2;
受保护 覆盖 void AttachInterfaces (
object nativeActiveXObject)
{
base .AttachInterfaces(nativeActiveXObject) );
this .axIWebBrowser2 =(IWebBrowser2)nativeActiveXObject;
}

受保护 覆盖 void DetachInterfaces()
{
base .DetachInterfaces();
this .axIWebBrowser2 = null ;
}

/// < 摘要 >
/// 更改浏览器文本大小。
/// < / summary >
/// < param < span class =code-summarycomment> name =textSize > < / param >
private void ChangeTextSize( int textSize)
{
尝试
{
thi s .axIWebBrowser2.ExecWB(OLECMDID.OLECMDID_ZOOM,
OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER,
textSize, IntPtr .Zero);
}
catch (例外)
{
throw ;
}
}

// 来源:http:// support.microsoft.com/kb/304103
// 缩放级别的有效范围是0到4,
// 其中0最小,4最大
private const int MIN_TEXT_SIZE = 0 ;
private const int MAX_TEXT_SIZE = 4 ;
private const int DEFAULT_TEXT_SIZE = 2 ;

/ *
///< summary>
///返回文本的当前大小。
///注意:不工作!始终返回零!
///< / summary>
///< returns>< / returns>
private int GetTextSize()
{
object pvaIn = 0;
this.axIWebBrowser2.ExecWB(OLECMDID.OLECMDID_ZOOM,
OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER,IntPtr.Zero,pvaIn);
return(int)pvaIn;
}
* /


/// < 摘要 >
/// 如果可以增加文本大小,则返回True。
/// < / summary >
public bool CanZoomIn
{
get { return textSize < MAX_TEXT_SIZE; }
}

/// < 摘要 >
/// 如果可以减小文本大小,则返回True。
/// < / summary >
public bool CanZoomOut
{
get {返回 textSize > MIN_TEXT_SIZE; }
}
/// < 摘要 >
/// 当前文字大小(默认为2)。
/// < / summary >
private int textSize = DEFAULT_TEXT_SIZE;

/// < 摘要 >
/// 增加文字大小。
/// < / summary >
public void ZoomIn()
{
if (textSize < MAX_TEXT_SIZE)
{
textSize ++;
this .ChangeTextSize(textSize);
}
}

/// < 摘要 >
/// 减小文字大小。
/// < / summary >
public void ZoomOut()
{
if (textSize > MIN_TEXT_SIZE)
{
textSize--;
this .ChangeTextSize(textSize);
}
}

}
}





我不是能够检索当前文本大小,如上面的链接所述(请参阅注释 private int GetTextSize()),但这不是一个大问题:

浏览器默认文本size是2,所以我只使用私有变量作为默认值。



我在表单中放置了两个按钮: btnZoomIn btnZoomOut

几乎不需要任何解释。

当达到最大或最小文字大小时,它们被禁用。

wbrCode MyBrowser 类的一个实例)。代码是:

  private   void  btnZoomIn_Click( object  sender,EventArgs e)
{
wbrCode.ZoomIn();
RefreshZoomButtons();
}

private void btnZoomOut_Click( object sender,EventArgs e)
{
wbrCode.ZoomOut();
RefreshZoomButtons();
}

private void RefreshZoomButtons()
{
btnZoomIn.Enabled = wbrCode.CanZoomIn;
btnZoomOut.Enabled = wbrCode.CanZoomOut;
}





HTH

Jack Griffin


我在这里找到了这个解决方案: winforms - 放大使用WebBrowser .NET控件的网页 - Stack Overflow [ ^ ]



由此人:

[ QHNPROF ]



谁提出这个优雅的解决方案:



  private   void  webBrowser1_DocumentCompleted( object  sender,WebBrowserDocumentCompletedEventArgs e)
{
webBrows er1.Document.Body.Style = zoom:50%;
}






您需要创建自己的继承控件来自webbrowser



  public   partial   class  MyBrowser:WebBrowser 





并使用IWebBrowser2接口

(此处的代码: http:// www。 pinvoke.net/default.aspx/Interfaces/IWebBrowser2.html [ ^ ])





  private  IWebBrowser2 axIWebBrowser2; 
受保护 覆盖 void AttachInterfaces (
object nativeActiveXObject)
{
base .AttachInterfaces(nativeActiveXObject) );
this .axIWebBrowser2 =(IWebBrowser2)nativeActiveXObject;
}

受保护 覆盖 void DetachInterfaces()
{
base .DetachInterfaces();
this .axIWebBrowser2 = null ;
}





然后只需添加缩放方法

  public   void 缩放( int  factor)
{
object pvaIn = factor;
尝试
{
.axIWebBrowser2.ExecWB(OLECMDID。 OLECMDID_OPTICAL_ZOOM,
OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER,
ref pvaIn, IntPtr .Zero);
}
catch (例外)
{
throw ;
}
}





您可以在pinvoke中找到列值

< a href =http://www.pinvoke.net/search.aspx?search=OLECMDID&namespace=[All]>

[ ^ ]



http://www.pinvoke.net/search.aspx?search = OLECMDEXECOPT& namespace = [All] [ ^ ]



希望它有所帮助。



Valery。


Hi..

My application has a form with two web browser control on it,
I want zoom in/out facility on both of them.

So, How can i do that in c#..?

I found a link related this, but it's in VB.net
Using ExecWB with the native .NET 2.0 WebBrowser control[^]

Please help...

解决方案

I had the same issue and while googleing I found this page.
I tried the code posted by Valery : it was very useful (definetely a major hint, thank you Valery!), though it did not exactly was I was after .
After more googleing I found this link : http://support.microsoft.com/kb/304103 .
It helped indeed, even though the code is in VB.NET .
Basically, the trick was to use OLECMDID.OLECMDID_ZOOM instead of OLECMDID.OLECMDID_OPTICAL_ZOOM .
So I played a little and ended up with this.
Works like a charm.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SHDocVw;

namespace ColorCodeTest
{
    public partial class MyBrowser : System.Windows.Forms.WebBrowser
    {
        private IWebBrowser2 axIWebBrowser2;
        protected override void AttachInterfaces(
            object nativeActiveXObject)
        {
            base.AttachInterfaces(nativeActiveXObject);
            this.axIWebBrowser2 = (IWebBrowser2)nativeActiveXObject;
        }

        protected override void DetachInterfaces()
        {
            base.DetachInterfaces();
            this.axIWebBrowser2 = null;
        }
        
        /// <summary>
        /// Changes browser text size .
        /// </summary>
        /// <param name="textSize"></param>
        private void ChangeTextSize(int textSize)
        {
            try
            {
                this.axIWebBrowser2.ExecWB(OLECMDID.OLECMDID_ZOOM,
                     OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER,
                     textSize, IntPtr.Zero);
            }
            catch (Exception)
            {
                throw;
            }            
        }

        // source : http://support.microsoft.com/kb/304103
        // the valid range for the zoom level is 0 through 4, 
        // in which 0 is smallest and 4 is largest
        private const int MIN_TEXT_SIZE = 0;
        private const int MAX_TEXT_SIZE = 4;
        private const int DEFAULT_TEXT_SIZE = 2;
        
        /*
        /// <summary>
        /// Returns the current size of text .
        /// NOTE : DOES NOT WORK ! Always returns zero !
        /// </summary>
        /// <returns></returns>
        private int GetTextSize()
        {           
            object pvaIn = 0;
            this.axIWebBrowser2.ExecWB (OLECMDID.OLECMDID_ZOOM, 
            OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, IntPtr.Zero, pvaIn );
            return (int)pvaIn;
        }
        */
      
        /// <summary>
        /// Returns True if text size can be increased .
        /// </summary>
        public bool CanZoomIn
        {
            get { return textSize < MAX_TEXT_SIZE; }
        }

        /// <summary>
        /// Returns True if text size can be decreased .
        /// </summary>
        public bool CanZoomOut
        {
            get { return textSize > MIN_TEXT_SIZE; }
        }
        /// <summary>
        /// Current text size ( default is 2 ) .
        /// </summary>
        private int textSize = DEFAULT_TEXT_SIZE;

        /// <summary>
        /// Increases text size . 
        /// </summary>
        public void ZoomIn()
        {            
            if (textSize < MAX_TEXT_SIZE)
            {
                textSize++;
                this.ChangeTextSize(textSize);
            }
        }

        /// <summary>
        /// Decreases text size .
        /// </summary>
        public void ZoomOut()
        {            
            if (textSize > MIN_TEXT_SIZE)
            {
                textSize--;
                this.ChangeTextSize(textSize);
            }
        }

    }
}



I was not able to retrieve the current text size as explained in the link above ( see the commented private int GetTextSize() ) but this is not a big issue :
the browser default text size is 2, so I just use a private variable with this value as default .

I placed two buttons in a form : btnZoomIn and btnZoomOut ,
which hardly need any explanation .
They are disabled when the maximum or minimum text size is reached .
(wbrCode is an instance of MyBrowser class ).The code is :

private void btnZoomIn_Click(object sender, EventArgs e)
{
    wbrCode.ZoomIn();
    RefreshZoomButtons();
}

private void btnZoomOut_Click(object sender, EventArgs e)
{
    wbrCode.ZoomOut();
    RefreshZoomButtons();
}

private void RefreshZoomButtons()
{
    btnZoomIn.Enabled = wbrCode.CanZoomIn;
    btnZoomOut.Enabled = wbrCode.CanZoomOut;
}



HTH
Jack Griffin


I found this solution here: winforms - Zoom in on a web page using WebBrowser .NET control - Stack Overflow[^]

By this guy:
[QHNPROF]

Whom proposes this ellegant solution:

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        webBrowser1.Document.Body.Style = "zoom:50%";
    }


Hi,

You need to create your own control that inherits from webbrowser

public partial class MyBrowser : WebBrowser



and use the IWebBrowser2 interface
(code here: http://www.pinvoke.net/default.aspx/Interfaces/IWebBrowser2.html[^])


private IWebBrowser2 axIWebBrowser2;
protected override void AttachInterfaces(
    object nativeActiveXObject)
{
    base.AttachInterfaces(nativeActiveXObject);
    this.axIWebBrowser2 = (IWebBrowser2)nativeActiveXObject;
}

protected override void DetachInterfaces()
{
    base.DetachInterfaces();
    this.axIWebBrowser2 = null;
}



Then just add a Zoom method

 public void Zoom(int factor)
{
    object pvaIn = factor;
    try
    {
        this.axIWebBrowser2.ExecWB(OLECMDID.OLECMDID_OPTICAL_ZOOM,
           OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER,
           ref pvaIn, IntPtr.Zero);
    }
    catch (Exception)
    {
        throw;
    }
}



you can find the enumns values in pinvoke

[^]

http://www.pinvoke.net/search.aspx?search=OLECMDEXECOPT&namespace=[All][^]

Hope it helps.

Valery.


这篇关于使用C#放大Web浏览器控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆