Home > OS >  How to setup font rendering in MSHTML
How to setup font rendering in MSHTML

Time:12-02

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?

CodePudding user response:

  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.

regedit browser emulation

  • Related