Changing display font of WebBrowser control in c#?

11,466

Solution 1

Well, from the looks of this you need to use the execWB command as outlined in this Microsoft article.

Update

however with a further look at the documentation, I'm not seeing the execWB OR the execCommand method that you are currently using as options within the .NET browser control.

Therefore, you might have to futz with the actual IE settings, which more than likely are in the registry..

Solution 2

Actually, you can call the ExecWB method, you just have to do so indirectly. I have the following code working for zooming up and down (using C# 4.0 makes it a bit easier):

    private const int OLECMDID_ZOOM = 63;
    private const int OLECMDEXECOPT_DONTPROMPTUSER = 2;

    private void SetZoom(int zoom)
    {
        dynamic obj = webBrowser1.ActiveXInstance;

        obj.ExecWB(OLECMDID_ZOOM, OLECMDEXECOPT_DONTPROMPTUSER, zoom, IntPtr.Zero);
    }
Share:
11,466
somatic rev
Author by

somatic rev

Quick learner.

Updated on June 04, 2022

Comments

  • somatic rev
    somatic rev almost 2 years

    I'm trying to change the display font of WebBrowser control.

    I tried

    doc.execCommand("FontName", false, "Arial");
    

    But it seems it works for selected text.

    I want exact same effect as setting font inside IE -> Internet Options -> General -> Appearance -> Fonts.

    Thanks in advance.

    Byoungjo

    -------- Update -------------

    Like Mitchell has pointed out, doing the same work as ExeWB is doing in C#.Net is the goal.

    Also, changing registry is somewhat overwork for this and might need simpler solution if exists. Otherwise, I'll just say no to this FR.

  • somatic rev
    somatic rev over 14 years
    Yes, modifying the registry is only solution I've found from the web so far. But, another intention is not to change IE's default settings. Understandably, I don't want my application to change user's IE display settings.
  • Mitchel Sellers
    Mitchel Sellers over 14 years
    Well, a more 'Tricky' way would be to inject an embedded stylesheet to every page before it renders. otherwise, since the WebBrowser is just IE inside, the settings would need to be changed at a higher level.
  • somatic rev
    somatic rev over 14 years
    Embedded stylesheet sounds good solution, but when I try this: IHTMLStyleSheet styleSheet = doc.createStyleSheet("", 0); styleSheet.cssText = "BODY {font-family:\"Arial\"}"; It gets 'Error HRESULT E_FAIL has been returned from a call to COM component'. A little more help on this?
  • somatic rev
    somatic rev over 14 years
    I chose to follow Mitchel's suggestion. However thanks for the answer.
  • somatic rev
    somatic rev almost 14 years
    Although this has been out of functional requirement for now, your suggestion sounds good solution for controlling browser object. Thanks.