Skip to content

Fixed icon and UI on HiDPi-enabled devices. #255

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions main/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
<System.Windows.Forms.ApplicationConfigurationSection>
<add key="DpiAwareness" value="PerMonitorV2" />
</System.Windows.Forms.ApplicationConfigurationSection>
</configuration>
46 changes: 45 additions & 1 deletion main/Forms/frmClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Windows.Forms;
using Windows.Data.Json;
using System.Net.Http;
using System.Runtime.InteropServices;

namespace client.Forms
{
Expand All @@ -17,16 +18,32 @@ public frmClient()
{
System.Runtime.ProfileOptimization.StartProfile("frmClient.Profile");
InitializeComponent();
eDpi = Display(DpiType.Effective);
this.MaximumSize = new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
this.MinimumSize = new Size(Size.Width + 1, Size.Height); // +1 seems to fix the bottomscroll bar randomly appearing.
Reload();

currentVersion.Text = "v" + System.Reflection.Assembly.GetEntryAssembly().GetName().Version.ToString();

githubVersion.Text = Task.Run(() => getVersionData()).Result;
}

public static uint eDpi { get; set; } // Effective DPI

public uint Display(DpiType type)
{
foreach (var screen in System.Windows.Forms.Screen.AllScreens)
{
screen.GetDpi(DpiType.Effective, out uint x, out _);
eDpi = x;
return (x);
}
return (eDpi);
}
public void Reload()
{
// flush and reload existing groups
pnlVersionInfo.Location = new Point((int)(19 * eDpi / 96), (int)(615 * eDpi / 96)); // eDpi position ajustments
pnlExistingGroups.Controls.Clear();
pnlExistingGroups.Height = 0;

Expand Down Expand Up @@ -54,7 +71,7 @@ public void Reload()
lblHelpTitle.Text = "Press on \"Add Taskbar group\" to get started";
pnlHelp.Visible = false;
}
pnlBottomMain.Top = pnlExistingGroups.Bottom + 20; // spacing between existing groups and add new group btn
pnlBottomMain.Top = pnlExistingGroups.Bottom + (int)(20 * eDpi / 96); // spacing between existing groups and add new group btn

Reset();
}
Expand Down Expand Up @@ -133,4 +150,31 @@ private void frmClient_Resize(object sender, EventArgs e)
Reset();
}
}

// Get DPI function below
public static class ScreenExtensions
{
public static void GetDpi(this System.Windows.Forms.Screen screen, DpiType dpiType, out uint dpiX, out uint dpiY)
{
var pnt = new System.Drawing.Point(screen.Bounds.Left + 1, screen.Bounds.Top + 1);
var mon = MonitorFromPoint(pnt, 2/*MONITOR_DEFAULTTONEAREST*/);
GetDpiForMonitor(mon, dpiType, out dpiX, out dpiY);
}

//https://msdn.microsoft.com/en-us/library/windows/desktop/dd145062(v=vs.85).aspx
[DllImport("User32.dll")]
private static extern IntPtr MonitorFromPoint([In] System.Drawing.Point pt, [In] uint dwFlags);

//https://msdn.microsoft.com/en-us/library/windows/desktop/dn280510(v=vs.85).aspx
[DllImport("Shcore.dll")]
private static extern IntPtr GetDpiForMonitor([In] IntPtr hmonitor, [In] DpiType dpiType, [Out] out uint dpiX, [Out] out uint dpiY);
}

//https://msdn.microsoft.com/en-us/library/windows/desktop/dn280511(v=vs.85).aspx
public enum DpiType
{
Effective = 0,
Angular = 1,
Raw = 2,
}
}
16 changes: 8 additions & 8 deletions main/Forms/frmGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public frmGroup(frmClient client, Category category)
private void frmGroup_Load(object sender, EventArgs e)
{
// Scaling form (WORK IN PROGRESS)
this.MaximumSize = new Size(605, Screen.PrimaryScreen.WorkingArea.Height);
this.MaximumSize = new Size(Size.Width, Screen.PrimaryScreen.WorkingArea.Height);
}

//--------------------------------------
Expand All @@ -130,10 +130,10 @@ public void LoadShortcut(ProgramShortcut psc, int position)

if (pnlShortcuts.Controls.Count < 6)
{
pnlShortcuts.Height += 50;
pnlAddShortcut.Top += 50;
pnlShortcuts.Height += 50 * (int)(frmClient.eDpi / 96);
pnlAddShortcut.Top += 50 * (int)(frmClient.eDpi / 96);
}
ucPsc.Location = new Point(25, (pnlShortcuts.Controls.Count * 50)-50);
ucPsc.Location = new Point(25 * (int)(frmClient.eDpi / 96), (pnlShortcuts.Controls.Count * 50 * (int)(frmClient.eDpi / 96)) - 50 * (int)(frmClient.eDpi / 96));
pnlShortcuts.AutoScroll = true;

}
Expand Down Expand Up @@ -239,7 +239,7 @@ public void DeleteShortcut(ProgramShortcut psc)
{
if (before)
{
ucPsc.Top -= 50;
ucPsc.Top -= 50 * (int)(frmClient.eDpi / 96);
ucPsc.Position -= 1;
}
if (ucPsc.Shortcut == psc)
Expand Down Expand Up @@ -270,8 +270,8 @@ public void DeleteShortcut(ProgramShortcut psc)

if (pnlShortcuts.Controls.Count < 5)
{
pnlShortcuts.Height -= 50;
pnlAddShortcut.Top -= 50;
pnlShortcuts.Height -= 50 * (int)(frmClient.eDpi / 96);
pnlAddShortcut.Top -= 50 * (int)(frmClient.eDpi / 96);
}
}

Expand All @@ -286,7 +286,7 @@ public void Swap<T>(IList<T> list, int indexA, int indexB)
// Clears and reloads all shortcuts with new positions
pnlShortcuts.Controls.Clear();
pnlShortcuts.Height = 0;
pnlAddShortcut.Top = 220;
pnlAddShortcut.Top = 220 * (int)(frmClient.eDpi / 96);

selectedShortcut = null;

Expand Down
50 changes: 38 additions & 12 deletions main/Forms/frmMain.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using client.Classes;
using client.Forms;
using client.User_controls;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -30,19 +31,25 @@ protected override CreateParams CreateParams
public Category ThisCategory;
public List<ucShortcut> ControlList;
public Color HoverColor;
public static uint eDpi { get; set; } // Effective DPI
public static decimal xDpi { get; set; } // eDpi ratio for HDPi conversions

private string passedDirec;
public Point mouseClick;

public int ucShortcutHeight;
public int ucShortcutWidth;

//------------------------------------------------------------------------------------
// CTOR AND LOAD
//
public frmMain(string passedDirectory, int cursorPosX, int cursorPosY)
{
InitializeComponent();

eDpi = Display(DpiType.Effective); // Get Dpi from clicked screen
xDpi = eDpi / 96; // Get eDpi conversion ratio
System.Runtime.ProfileOptimization.StartProfile("frmMain.Profile");
mouseClick = new Point(cursorPosX, cursorPosY); // Consstruct point p based on passed x y mouse values
mouseClick = new Point(Cursor.Position.X, Cursor.Position.Y); // Construct point p based on passed x y mouse values
passedDirec = passedDirectory;
FormBorderStyle = FormBorderStyle.None;

Expand Down Expand Up @@ -71,6 +78,21 @@ public frmMain(string passedDirectory, int cursorPosX, int cursorPosY)
}
}

// eDpi Calculations Below -----------------
public uint Display(DpiType type)
{
foreach (var screen in System.Windows.Forms.Screen.AllScreens)
{
if (screen.Bounds.Contains(mouseClick)) // Get what screen to target eDpi
{
screen.GetDpi(DpiType.Effective, out uint x, out _);
eDpi = x;
return (x);
}
}
return (eDpi);
}

private void frmMain_Load(object sender, EventArgs e)
{
LoadCategory();
Expand Down Expand Up @@ -263,11 +285,14 @@ private void LoadCategory()
{
//System.Diagnostics.Debugger.Launch();

this.Width = 0;
this.Height = 45;
this.Width = (int)(55 * xDpi); // Adds a bug where if the (number of icons) < (width in group_panel) show an additional blank space
this.Height = (int)(45 * xDpi);
ucShortcutHeight = this.Height;
ucShortcutWidth = this.Width;

int x = 0;
int y = 0;
int width = ThisCategory.Width;
int width = ThisCategory.Width;
int columns = 1;

// Check if icon caches exist for the category being loaded
Expand All @@ -283,17 +308,17 @@ private void LoadCategory()
if (columns > width) // creating new row if there are more psc than max width
{
x = 0;
y += 45;
this.Height += 45;
y += (int)(45 * xDpi);
this.Height += (int)(45*xDpi);
columns = 1;
}

if (this.Width < ((width * 55)))
this.Width += (55);
if (this.Width < ((width * (int)(55 * xDpi))))
this.Width += ((int)(55 * (xDpi)));

// OLD
//BuildShortcutPanel(x, y, psc);

// Building shortcut controls
ucShortcut pscPanel = new ucShortcut()
{
Expand All @@ -308,11 +333,12 @@ private void LoadCategory()
pscPanel.BringToFront();

// Reset values
x += 55;
x += (int)(55 * xDpi);
columns++;
}

this.Width -= 2; // For some reason the width is 2 pixels larger than the shortcuts. Temporary fix
// this.Width -= 2; // For some reason the width is 2 pixels larger than the shortcuts. Temporary fix
// Fixed by setting the width of the ucPanel to the real value up here
}

// OLD (Having some issues with the uc build, so keeping the old code below)
Expand Down
14 changes: 7 additions & 7 deletions main/User controls/ucCategoryPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public ucCategoryPanel(frmClient client, Category category)
picGroupIcon.BackgroundImage = Category.LoadIconImage();

// starting values for position of shortcuts
int x = 90;
int y = 55;
int x = (int)(90 * frmClient.eDpi / 96);
int y = (int)(56 * frmClient.eDpi / 96);
int columns = 1;

if (!Directory.Exists((@"config\" + category.Name) + "\\Icons\\"))
Expand All @@ -34,13 +34,13 @@ public ucCategoryPanel(frmClient client, Category category)
{
if (columns == 8)
{
x = 90; // resetting x
y += 40; // adding new row
this.Height += 40;
x = (int)(90 * frmClient.eDpi / 96); // resetting x
y += (int)(40 * frmClient.eDpi / 96); // adding new row
this.Height += (int)(40 * frmClient.eDpi / 96);
columns = 1;
}
CreateShortcut(x, y, psc);
x += 50;
x += (int)(50 * frmClient.eDpi / 96);
columns++;
}
}
Expand All @@ -52,7 +52,7 @@ private void CreateShortcut(int x, int y, ProgramShortcut programShortcut)
{
BackColor = System.Drawing.Color.Transparent,
Location = new System.Drawing.Point(x, y),
Size = new System.Drawing.Size(30, 30),
Size = new System.Drawing.Size((int)(30 * frmClient.eDpi / 96), (int)(30 * frmClient.eDpi / 96)),
BackgroundImageLayout = ImageLayout.Stretch,
TabStop = false
};
Expand Down
1 change: 1 addition & 0 deletions main/User controls/ucShortcut.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions main/User controls/ucShortcut.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public ucShortcut()

private void ucShortcut_Load(object sender, EventArgs e)
{
this.Size = new Size(MotherForm.ucShortcutWidth, MotherForm.ucShortcutHeight);
this.Show();
this.BringToFront();
this.BackColor = MotherForm.BackColor;
Expand Down
2 changes: 2 additions & 0 deletions main/client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,11 @@
</Compile>
<EmbeddedResource Include="Forms\frmClient.resx">
<DependentUpon>frmClient.cs</DependentUpon>
<SubType>Designer</SubType>
</EmbeddedResource>
<EmbeddedResource Include="Forms\frmGroup.resx">
<DependentUpon>frmGroup.cs</DependentUpon>
<SubType>Designer</SubType>
</EmbeddedResource>
<EmbeddedResource Include="Forms\frmMain.resx">
<DependentUpon>frmMain.cs</DependentUpon>
Expand Down