fix blurred UI by using high DPI awareness setting. Get/Set window position still uses DPI unaware setting

This commit is contained in:
Kang Yu 2023-07-27 23:38:07 -07:00
parent 2fca222be2
commit 0920e2533c
8 changed files with 114 additions and 5 deletions

View file

@ -1809,6 +1809,7 @@ namespace PersistentWindows.Common
public string GetDisplayKey()
{
int result = User32.SetThreadDpiAwarenessContext(User32.DPI_AWARENESS_CONTEXT_UNAWARE);
DesktopDisplayMetrics metrics = new DesktopDisplayMetrics();
metrics.AcquireMetrics();
return metrics.Key;
@ -1956,6 +1957,8 @@ namespace PersistentWindows.Common
private void CaptureApplicationsOnCurrentDisplays(string displayKey, bool saveToDB = false, bool immediateCapture = false)
{
int result = User32.SetThreadDpiAwarenessContext(User32.DPI_AWARENESS_CONTEXT_UNAWARE);
Log.Trace("");
Log.Trace("Capturing windows for display setting {0}", displayKey);
@ -2855,6 +2858,8 @@ namespace PersistentWindows.Common
return false;
}
User32.SetThreadDpiAwarenessContext(User32.DPI_AWARENESS_CONTEXT_UNAWARE);
Log.Info("");
Log.Info("Restoring windows pass {0} for {1}", restoreTimes, displayKey);

View file

@ -445,6 +445,16 @@ namespace PersistentWindows.Common.WinApiBridge
[DllImport("user32.dll")]
public static extern bool IsWindowOnCurrentVirtualDesktop(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern bool SetProcessDpiAwarenessContext(int dpi_awareness_cxt);
[DllImport("user32.dll")]
public static extern int SetThreadDpiAwarenessContext(int dpi_awareness_cxt);
public const int DPI_AWARENESS_CONTEXT_UNAWARE = -1;
public const int DPI_AWARENESS_CONTEXT_SYSTEM_AWARE = -2;
public const int DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE = -3;
public const int DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 = -4;
public const int DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED = -5;
#region Hooks
[DllImport("user32.dll")]
public static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);

View file

@ -32,6 +32,9 @@ namespace PersistentWindows.SystrayShell
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Log.Init();
pwp = new PersistentWindowProcessor();
@ -212,9 +215,6 @@ namespace PersistentWindows.SystrayShell
}
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
string productName = System.Windows.Forms.Application.ProductName;
string appDataFolder = redirect_appdata ? "." :
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
@ -297,6 +297,7 @@ namespace PersistentWindows.SystrayShell
}
Application.Run();
}
static void ShowRestoreTip()

View file

@ -60,6 +60,16 @@ namespace PersistentWindows.SystrayShell.Properties {
}
}
/// <summary>
/// Looks up a localized resource of type System.Byte[].
/// </summary>
internal static byte[] app {
get {
object obj = ResourceManager.GetObject("app", resourceCulture);
return ((byte[])(obj));
}
}
/// <summary>
/// Looks up a localized resource of type System.Drawing.Icon similar to (Icon).
/// </summary>

View file

@ -118,6 +118,9 @@
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<data name="app" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\app.manifest;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="pwIcon" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\pwIcon.ico;System.Drawing.Icon, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>

View file

@ -68,7 +68,7 @@ namespace PersistentWindows.SystrayShell
//
this.label2.AutoSize = true;
this.label2.Font = new System.Drawing.Font("Courier New", 10F, System.Drawing.FontStyle.Underline);
this.label2.Location = new System.Drawing.Point(192, 180);
this.label2.Location = new System.Drawing.Point(201, 178);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(269, 20);
this.label2.TabIndex = 2;
@ -85,7 +85,6 @@ namespace PersistentWindows.SystrayShell
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "SplashForm";
this.Icon = Program.IdleIcon;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Load += new System.EventHandler(this.SplashForm_Load);
this.ResumeLayout(false);

View file

@ -52,6 +52,9 @@
</PropertyGroup>
<PropertyGroup />
<PropertyGroup />
<PropertyGroup>
<ApplicationManifest>app.manifest</ApplicationManifest>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.IO.Compression.FileSystem" />
@ -91,6 +94,7 @@
<SubType>Designer</SubType>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
<None Include="app.manifest" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>

View file

@ -0,0 +1,77 @@
<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
<assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<!-- UAC Manifest Options
If you want to change the Windows User Account Control level replace the
requestedExecutionLevel node with one of the following.
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
<requestedExecutionLevel level="highestAvailable" uiAccess="false" />
Specifying requestedExecutionLevel element will disable file and registry virtualization.
Remove this element if your application requires this virtualization for backwards
compatibility.
-->
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
</requestedPrivileges>
</security>
</trustInfo>
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
<!-- A list of the Windows versions that this application has been tested on
and is designed to work with. Uncomment the appropriate elements
and Windows will automatically select the most compatible environment. -->
<!-- Windows Vista -->
<!--<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}" />-->
<!-- Windows 7 -->
<!--<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}" />-->
<!-- Windows 8 -->
<!--<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}" />-->
<!-- Windows 8.1 -->
<!--<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}" />-->
<!-- Windows 10 -->
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
</application>
</compatibility>
<!-- Indicates that the application is DPI-aware and will not be automatically scaled by Windows at higher
DPIs. Windows Presentation Foundation (WPF) applications are automatically DPI-aware and do not need
to opt in. Windows Forms applications targeting .NET Framework 4.6 that opt into this setting, should
also set the 'EnableWindowsFormsHighDpiAutoResizing' setting to 'true' in their app.config.
Makes the application long-path aware. See https://docs.microsoft.com/windows/win32/fileio/maximum-file-path-limitation -->
<application xmlns="urn:schemas-microsoft-com:asm.v3">
<windowsSettings>
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
<longPathAware xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">true</longPathAware>
</windowsSettings>
</application>
<!-- Enable themes for Windows common controls and dialogs (Windows XP and later) -->
<!--
<dependency>
<dependentAssembly>
<assemblyIdentity
type="win32"
name="Microsoft.Windows.Common-Controls"
version="6.0.0.0"
processorArchitecture="*"
publicKeyToken="6595b64144ccf1df"
language="*"
/>
</dependentAssembly>
</dependency>
-->
</assembly>