如何在 MSHTML 中设置字体渲染

How to setup font rendering in MSHTML

我正在学习使用 MSHTML 在我的 WinApi 应用程序中托管浏览器,它是 IWebBrowser2 和 IHTMLDocument2。第一个问题是文字模糊,图片左边是我的app,右边是IE:

那么,如何设置字体渲染?

  1. 您的屏幕设置看起来像 150% 缩放,因此请确保应用程序被标记为 DPI 感知(编辑清单或 IDE 中的 select 选项)。

  2. IDocHostUIHandler::GetHostInfo 实现中添加 DOCHOSTUIFLAG_DPI_AWAREdwFlags.

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. 通过设置注册表项更改模拟的 IE 版本(对于高 DPI 不是必需的,而是为了更好的 CSS 支持)。
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();

编辑:

FixIeCompatMode 设置 WebBrowser 仿真模式。根据分配给 fix_version 的值,WebBrowser 模拟不同版本的 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.

如果省略 FEATURE_BROWSER_EMULATION 某些 !DOCTYPE 指令(例如 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">)可能会激活旧的 IE 7.0 仿真,但有很多缺点(例如更差的 CSS 支持)。

你可以用regedit查看(或删除)。