Skip to content

Commit

Permalink
Merge pull request #79 from damienleroy/fix/dpi
Browse files Browse the repository at this point in the history
fix dpi (fix from PowerToys, need explanation)
  • Loading branch information
damienleroy authored Nov 8, 2022
2 parents 5c0b214 + 55286c1 commit 10ca7f8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
22 changes: 11 additions & 11 deletions PowerAccent.Core/PowerAccent.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>disable</Nullable>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>net6.0-windows</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>disable</Nullable>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Configuration.ConfigurationManager" Version="6.0.0" />
<PackageReference Include="Vanara.PInvoke.User32" Version="3.3.15" />
<PackageReference Include="Vanara.PInvoke.Shell32" Version="3.3.15" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="System.Configuration.ConfigurationManager" Version="6.0.0" />
<PackageReference Include="Vanara.PInvoke.User32" Version="3.3.15" />
<PackageReference Include="Vanara.PInvoke.Shell32" Version="3.3.15" />
</ItemGroup>

</Project>
9 changes: 4 additions & 5 deletions PowerAccent.Core/PowerAccent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,13 @@ private bool PowerAccent_KeyUp(object sender, KeyboardListener.RawKeyEventArgs a
return true;
}

public Point GetDisplayCoordinates(Size window)
public Point GetDisplayCoordinates(Size window, double primaryDpi)
{
var activeDisplay = WindowsFunctions.GetActiveDisplay();
Rect screen = new Rect(activeDisplay.Location, activeDisplay.Size) / activeDisplay.Dpi;
Rect screen = new Rect(activeDisplay.Location, activeDisplay.Size) / primaryDpi;
Position position = _settingService.Position;

Debug.WriteLine("Dpi: " + activeDisplay.Dpi);
Debug.WriteLine($"Primary Dpi: {primaryDpi} - Screen Dpi: {activeDisplay.Dpi}");

if (!_settingService.UseCaretPosition)
{
Expand All @@ -162,8 +162,7 @@ public Point GetDisplayCoordinates(Size window)
return Calculation.GetRawCoordinatesFromPosition(position, screen, window);
}

Point dpi = new Point(activeDisplay.Dpi, activeDisplay.Dpi);
Point caret = new Point(carretPixel.X / dpi.X, carretPixel.Y / dpi.Y);
Point caret = new Point(carretPixel.X, carretPixel.Y) / primaryDpi;
return Calculation.GetRawCoordinatesFromCaret(caret, screen, window);
}

Expand Down
5 changes: 4 additions & 1 deletion PowerAccent.UI/Selector.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using Point = PowerAccent.Core.Point;
using Size = PowerAccent.Core.Size;
using Application = System.Windows.Application;

namespace PowerAccent.UI;

Expand Down Expand Up @@ -62,7 +64,8 @@ private void CenterWindow()
{
UpdateLayout();
Size window = new Size(((System.Windows.Controls.Panel)Application.Current.MainWindow.Content).ActualWidth, ((System.Windows.Controls.Panel)Application.Current.MainWindow.Content).ActualHeight);
Point position = _powerAccent.GetDisplayCoordinates(window);
double primaryDPI = Screen.PrimaryScreen.Bounds.Width / SystemParameters.PrimaryScreenWidth;
Point position = _powerAccent.GetDisplayCoordinates(window, primaryDPI);
this.Left = position.X;
this.Top = position.Y;
}
Expand Down

0 comments on commit 10ca7f8

Please sign in to comment.