c++winapimshtml

How to setup font rendering in MSHTML


I'm learning to host a browser in my WinApi application using MSHTML and it's IWebBrowser2 and IHTMLDocument2. The first problem is blurry text, the left part of a picture is my app, and the right part is IE: comparison

So, how to setup font rendering?


Solution

    1. Your screen setting looks like 150% scaling so make sure that application is marked as DPI aware (edit manifest or select option in IDE).

    2. In IDocHostUIHandler::GetHostInfo implementation add DOCHOSTUIFLAG_DPI_AWARE to dwFlags.

    HRESULT DocHostUIHandler::GetHostInfo( DOCHOSTUIINFO* pInfo )
    {
        pInfo->cbSize = sizeof(DOCHOSTUIINFO);
        pInfo->dwFlags =
                DOCHOSTUIFLAG_NO3DBORDER
                | DOCHOSTUIFLAG_DPI_AWARE
                | DOCHOSTUIFLAG_DISABLE_SCRIPT_INACTIVE;
        pInfo->dwDoubleClick = DOCHOSTUIDBLCLK_DEFAULT;
        return S_OK;
    }
    
    1. Change emulated IE version by setting registry key (not essential for high DPI, rather for better CSS support).
    BOOL FixIeCompatMode()
    {
        DWORD fix_version = 11001;
    
        // Get full path to application
        WCHAR app_path[ PATH_MAX ];
        DWORD result = GetModuleFileName( NULL, app_path, PATH_MAX );
        if ( result == 0 || result == PATH_MAX )
            return FALSE;
    
        // Find application name part (without path)
        WCHAR* app_name = app_path + wcslen( app_path );
        while ( app_name > app_path && app_name[ -1 ] != '\\' )
            --app_name;
    
        // Create or open FEATURE_BROWSER_EMULATION key
        HKEY hKey;
        WCHAR* reg_path = L"Software\\Microsoft\\Internet Explorer\\Main\\FeatureControl\\FEATURE_BROWSER_EMULATION";
        if ( RegCreateKey( HKEY_CURRENT_USER, reg_path, &hKey) != ERROR_SUCCESS )
            return FALSE;
    
        // Add registy entry for our application e.g
        // DisplayHTML.exe = 11001
        // You can check it (or delete) with regedit
        BOOL set = RegSetValueEx(
                     hKey,
                     app_name,
                     0,
                     REG_DWORD,
                     (void*)&fix_version,
                     sizeof(fix_version) ) == ERROR_SUCCESS )
        RegCloseKey( hKey );
        return set;
    }
    
    // Somewhere in your startup code (before creating WebView)
    FixIeCompatMode();
    

    Edit:

    FixIeCompatMode sets WebBrowser emulation mode. Depending on value assigned to fix_version WebBrowser emulates different versions of IE.

    Internet Feature Controls

    • 11001: Internet Explorer 11. Webpages are displayed in IE11 edge mode, regardless of the declared !DOCTYPE directive. Failing to declare a !DOCTYPE directive causes the page to load in Quirks.
    • 11000: IE11. Webpages containing standards-based !DOCTYPE directives are displayed in IE11 edge mode. Default value for IE11.
    • 10001: Internet Explorer 10. Webpages are displayed in IE10 Standards mode, regardless of the !DOCTYPE directive.
    • 10000: Internet Explorer 10. Webpages containing standards-based !DOCTYPE directives are displayed in IE10 Standards mode. Default value for Internet Explorer 10.
    • 9999: Windows Internet Explorer 9. Webpages are displayed in IE9 Standards mode, regardless of the declared !DOCTYPE directive. Failing to declare a !DOCTYPE directive causes the page to load in Quirks.
    • 9000: Internet Explorer 9. Webpages containing standards-based !DOCTYPE directives are displayed in IE9 mode. Default value for Internet Explorer 9. In Internet Explorer 10, Webpages containing standards-based !DOCTYPE directives are displayed in IE10 Standards mode.
    • 8888: Webpages are displayed in IE8 Standards mode, regardless of the declared !DOCTYPE directive. Failing to declare a !DOCTYPE directive causes the page to load in Quirks.
    • 8000: Webpages containing standards-based !DOCTYPE directives are displayed in IE8 mode. Default value for Internet Explorer 8. Important In Internet Explorer 10, Webpages containing standards-based !DOCTYPE directives are displayed in IE10 Standards mode.
    • 7000: Webpages containing standards-based !DOCTYPE directives are displayed in IE7 Standards mode. Default value for applications hosting the WebBrowser Control.

    What Happens if I Set the FEATURE_BROWSER_EMULATION Document Mode Value Higher than the IE Version on the Client?

    Obviously, the browser control can only support a document mode that is less than or equal to the IE version installed on the client.
    Using the FEATURE_BROWSER_EMULATION key works best for enterprise line of business apps where there is a deployed and support version of the browser. In the case you set the value to a browser mode that is a higher version than the browser version installed on the client, the browser control will choose the highest document mode available.

    If FEATURE_BROWSER_EMULATION is omitted some !DOCTYPE directives (e.g <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">) could activate old IE 7.0 emulation with many drawbacks (e.g. worse CSS support).

    You can check it (or delete) with regedit. regedit browser emulation