Reputation: 1237
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.
Upvotes: 3
Views: 2926
Reputation: 1237
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;
}
Upvotes: 2