Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to activate "disable display scaling on high dpi settings" by default

Our application does not work correctly on some Windows 8.1 devices with high DPI settings (150% or higher). Specifically, we are hosting embedded web browsers using CEF. All these embedded CEF browsers are rendering the elements offset.

The application works fine when "Disable display scaling on high DPI settings" is checked. However, this is not checked by default.

How do I ensure that my application (MFC based) builds with this setting ticked by default?

I've tried switching DPI awareness off in the manifest as per posts like: https://msdn.microsoft.com/en-us/magazine/dn574798.aspx and http://blogs.msdn.com/b/vcblog/archive/2010/03/11/mfc-applications-now-default-to-being-dpi-aware.aspx. However, this didn't seem to work.

like image 390
James Bateson Avatar asked Dec 15 '25 14:12

James Bateson


1 Answers

The underlying issue was fixed with an update to CEF.

However, the solution in the interim (and the actual answer to this question, which will hopefully be useful to someone else) was to switch on the "Disable display scaling on high DPI settings" checkbox using a custom action during our (WiX) installation. Here is some code in C++:

#include "shlwapi.h"

#include <winreg.h>
//
// Include the MSI declarations etc
//  - Also ensure the dll is linked with msi.lib
//
#include <msi.h>
#include <msiquery.h>
#pragma comment(lib, "msi.lib")

UINT __stdcall DisableHighDPIAware(MSIHANDLE hInstaller)
{
    HKEY key;
    DWORD dwDisposition;
    LONG error = RegCreateKeyEx(HKEY_LOCAL_MACHINE,(LPCWSTR)L"Software\\Microsoft\\Windows NT\\CurrentVersion\\AppCompatFlags\\Layers", 0, NULL, 0, KEY_ALL_ACCESS | KEY_WRITE | KEY_WOW64_64KEY, NULL, &key, &dwDisposition);
    if (error != ERROR_SUCCESS)
    {
        return ERROR_INSTALL_FAILURE;
    }

    wchar_t pathToApp[MAX_PATH];
    DWORD PathSize = sizeof(pathToApp);
    error = MsiGetProperty(hInstaller, L"CustomActionData", pathToApp, &PathSize);
    if (error != ERROR_SUCCESS)
    {
        return ERROR_INSTALL_FAILURE;
    }

    wchar_t* value = L"~ HIGHDPIAWARE";
    PathAppend(pathToApp, L"app.exe");

    error = RegSetValueEx(key, (LPCWSTR)pathToApp, 0, REG_SZ, (const BYTE*)value, (DWORD)(lstrlen(value) + 1)*sizeof(TCHAR));
    if (error != ERROR_SUCCESS)
    {
        return ERROR_INSTALL_FAILURE;
    }

    return ERROR_SUCCESS;
}
like image 73
James Bateson Avatar answered Dec 19 '25 05:12

James Bateson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!