Skip to content

Instantly share code, notes, and snippets.

@renaudbedard
Created August 12, 2017 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save renaudbedard/f85cc4eb8ee3ae6ec0852be91adefd11 to your computer and use it in GitHub Desktop.
Save renaudbedard/f85cc4eb8ee3ae6ec0852be91adefd11 to your computer and use it in GitHub Desktop.
static void SetupDpiAwareness()
{
bool success = false;
List<string> errorCauses = new List<string>();
string successfulMethod = null;
try
{
var result = DpiAwareness.SetProcessDpiAwareness(DpiAwareness.PROCESS_DPI_AWARENESS.PROCESS_PER_MONITOR_DPI_AWARE);
if (result == DpiAwareness.HRESULT.E_ACCESSDENIED)
{
Logger.Log("SetupDpiAwareness", "DPI-awareness enabled via manifest");
return;
}
success = result == DpiAwareness.HRESULT.S_OK;
if (!success)
errorCauses.Add("SetProcessDpiAwareness => " + result.ToString());
}
catch (Exception ex)
{
errorCauses.Add("SetProcessDpiAwareness => " + ex.ToString());
}
if (!success)
{
try
{
success = DpiAwareness.SetProcessDPIAware();
if (!success)
errorCauses.Add("SetProcessDPIAware => false");
}
catch (Exception ex)
{
errorCauses.Add("SetProcessDPIAware => " + ex.ToString());
}
if (success)
successfulMethod = "SetProcessDPIAware";
}
else
successfulMethod = "SetProcessDpiAwareness";
if (!success)
Logger.Log("SetupDpiAwareness", "Tried unsuccessfully to enable DPI-awareness\nCauses : " + string.Join(Environment.NewLine, errorCauses));
else
{
Logger.Log("SetupDpiAwareness", "Successfully enabled DPI-awareness using '" + successfulMethod + "'");
if (errorCauses.Count > 0)
Logger.Log("SetupDpiAwareness", "Prior failing attempts : " + string.Join(Environment.NewLine, errorCauses));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment