Skip to content

Commit

Permalink
Fixed an issue with MiniPicker (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
Leo-Peyronnet committed Sep 15, 2021
1 parent 519be7f commit c41cffc
Showing 1 changed file with 38 additions and 2 deletions.
40 changes: 38 additions & 2 deletions ColorPicker/Pages/PickerPage.xaml.cs
Expand Up @@ -76,8 +76,26 @@ public PickerPage()
BlueSlider.Value = pixel.B; // Set value
// MiniPicker
double factor = PresentationSource.FromVisual(this).CompositionTarget.TransformToDevice.M11; // Get factor for DPI
float dpiX, dpiY;
double scaling = 100; // Default scaling = 100%
using (Graphics graphics = Graphics.FromHwnd(IntPtr.Zero))
{
dpiX = graphics.DpiX; // Get the DPI
dpiY = graphics.DpiY; // Get the DPI
scaling = dpiX switch
{
96 => 100, // Get the %
120 => 125, // Get the %
144 => 150, // Get the %
168 => 175, // Get the %
192 => 200, // Get the %
_ => 100
};
}
double factor = scaling / 100d; // Calculate factor
miniPicker.Left = Env.GetMouseCursorPositionWPF().X / factor; // Define position
miniPicker.Top = Env.GetMouseCursorPositionWPF().Y / factor + 5; // Define position
};
Expand Down Expand Up @@ -186,8 +204,26 @@ private void SelectColorBtn_Click(object sender, RoutedEventArgs e)
SelectColorBtn.Content = Properties.Resources.Stop; // Set text
isRunning = true;

double factor = PresentationSource.FromVisual(this).CompositionTarget.TransformToDevice.M11; // Get factor for DPI
float dpiX, dpiY;
double scaling = 100; // Default scaling = 100%

using (Graphics graphics = Graphics.FromHwnd(IntPtr.Zero))
{
dpiX = graphics.DpiX; // Get the DPI
dpiY = graphics.DpiY; // Get the DPI

scaling = dpiX switch
{
96 => 100, // Get the %
120 => 125, // Get the %
144 => 150, // Get the %
168 => 175, // Get the %
192 => 200, // Get the %
_ => 100
};
}

double factor = scaling / 100d; // Calculate factor
miniPicker.Left = Env.GetMouseCursorPositionWPF().X / factor; // Define position
miniPicker.Top = Env.GetMouseCursorPositionWPF().Y / factor + 5; // Define position
miniPicker.Show(); // Show
Expand Down

0 comments on commit c41cffc

Please sign in to comment.