views:

560

answers:

3

Hi all,

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.

+2  A: 

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..

Mitchel Sellers
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.
Noel Bj Kim
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.
Mitchel Sellers
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?
Noel Bj Kim
A: 

Since the setting is under HKEY_CURRENT_USER/Software/Microsoft/Internet Explorer, you should be able to override this setting by implementing IDocHostUIHandler2::GetOverrideKeyPath on your webbrowser site. Because Windows Forms made its IDocHostUIHandler implementation internal, I don't think you can implement IDocHostUIHandler2 on top of your WebBrowserSite. I guess you need to start from scratch, e.g. something like http://www.codeproject.com/KB/miscctrl/csEXWB.aspx Another possibility is to override the default css in your IDocHostUIHandler::GetHostInfo implementation.

Sheng Jiang 蒋晟
I chose to follow Mitchel's suggestion. However thanks for the answer.
Noel Bj Kim
A: 

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);
    }
SteveWilkinson
Although this has been out of functional requirement for now, your suggestion sounds good solution for controlling browser object.Thanks.
Noel Bj Kim