Skip to content

Commit

Permalink
Merge pull request #269 from pstranak-sw/feature/hiDpiSupport
Browse files Browse the repository at this point in the history
Support for HiDPI
  • Loading branch information
tdanner committed Mar 5, 2021
2 parents d48c42c + d5d5f6f commit 0728b8e
Show file tree
Hide file tree
Showing 16 changed files with 95 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -8,6 +8,7 @@ packages
paket.dependencies
paket.dependencies.locked
.vs
_ReSharper.Caches
UpgradeLog.htm

Samples/Java/swis-client/\.settings/
Expand Down

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

Expand Up @@ -30,6 +30,7 @@ public partial class FindReplaceDialog : Form

public FindReplaceDialog()
{
this.Font = new Font("Tahoma", 8.25F, FontStyle.Regular, GraphicsUnit.Point, 0);
InitializeComponent();

_autoPosition = true;
Expand Down
16 changes: 8 additions & 8 deletions Src/SwqlStudio/About.Designer.cs

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

4 changes: 3 additions & 1 deletion Src/SwqlStudio/About.cs
@@ -1,4 +1,5 @@
using System.Linq;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;

Expand All @@ -8,6 +9,7 @@ public partial class About : Form
{
public About()
{
this.Font = new Font("Segoe UI", 9F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0)));
InitializeComponent();

var assembly = Assembly.GetEntryAssembly();
Expand Down
2 changes: 2 additions & 0 deletions Src/SwqlStudio/CrudTab.cs
Expand Up @@ -3,6 +3,7 @@
using System.Windows.Forms;
using SolarWinds.InformationService.Contract2;
using SwqlStudio.Metadata;
using SwqlStudio.Utils;

namespace SwqlStudio
{
Expand All @@ -18,6 +19,7 @@ public CrudTab(CrudOperation operation)
{
_operation = operation;
InitializeComponent();
DpiHelper.FixRowHeight(propertiesDataGridView);
}

public Entity Entity
Expand Down
13 changes: 7 additions & 6 deletions Src/SwqlStudio/DocumentationContent.Designer.cs

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

2 changes: 2 additions & 0 deletions Src/SwqlStudio/DocumentationContent.cs
@@ -1,4 +1,5 @@
using System.Windows.Forms;
using SwqlStudio.Utils;
using WeifenLuo.WinFormsUI.Docking;

namespace SwqlStudio
Expand All @@ -7,6 +8,7 @@ public partial class DocumentationContent : DockContent
{
public DocumentationContent()
{
DpiHelper.FixFont(this);
InitializeComponent();
}

Expand Down
2 changes: 2 additions & 0 deletions Src/SwqlStudio/EntityClassGraphForm.cs
@@ -1,13 +1,15 @@
using System.Collections.Generic;
using System.Windows.Forms;
using SolarWinds.InformationService.InformationServiceClient;
using SwqlStudio.Utils;

namespace SwqlStudio
{
public partial class EntityClassGraphForm : Form
{
public EntityClassGraphForm(EntityClassGraph entityClassGraph)
{
DpiHelper.FixFont(this);
InitializeComponent();

LoadTree(entityClassGraph);
Expand Down
1 change: 1 addition & 0 deletions Src/SwqlStudio/MainForm.cs
Expand Up @@ -39,6 +39,7 @@ public ConnectionInfo SelectedConnection

public MainForm()
{
DpiHelper.FixFont(this);
InitializeComponent();

InitializeDockPanel();
Expand Down
2 changes: 2 additions & 0 deletions Src/SwqlStudio/NewConnection.cs
@@ -1,12 +1,14 @@
using System;
using System.Windows.Forms;
using SwqlStudio.Utils;

namespace SwqlStudio
{
internal partial class NewConnection : Form
{
public NewConnection()
{
DpiHelper.FixFont(this);
InitializeComponent();

cmbServer.Items.AddRange(ConnectionHistory.PreviousServers);
Expand Down
2 changes: 1 addition & 1 deletion Src/SwqlStudio/ObjectExplorer/ObjectExplorer.cs
Expand Up @@ -38,7 +38,7 @@ internal class ObjectExplorer : Control
private ImageList objectExplorerImageList;
private System.ComponentModel.IContainer components;
private TreeNode _dragNode;
private readonly TreeNodesBuilder treeNodesBuilder = new TreeNodesBuilder(DefaultFont);
private readonly TreeNodesBuilder treeNodesBuilder = new TreeNodesBuilder(DpiHelper.DefaultFont);
public event TreeViewEventHandler SelectionChanged;
public ITabsFactory TabsFactory { get; set; }

Expand Down
3 changes: 3 additions & 0 deletions Src/SwqlStudio/QueryParameters.cs
Expand Up @@ -3,6 +3,7 @@
using System.Drawing;
using System.Linq;
using SolarWinds.InformationService.Contract2;
using SwqlStudio.Utils;
using WeifenLuo.WinFormsUI.Docking;

namespace SwqlStudio
Expand All @@ -27,7 +28,9 @@ public bool AllowSetParameters

public QueryParameters()
{
DpiHelper.FixFont(this);
InitializeComponent();
DpiHelper.FixRowHeight(parametersGrid);
parametersGrid.DataSource = new BindingList<QueryVariable>();
AllowSetParameters = true;
}
Expand Down
7 changes: 5 additions & 2 deletions Src/SwqlStudio/QueryTab.cs
Expand Up @@ -15,6 +15,7 @@
using SwqlStudio.Playback;
using SwqlStudio.Properties;
using SwqlStudio.Subscriptions;
using SwqlStudio.Utils;

namespace SwqlStudio
{
Expand Down Expand Up @@ -62,6 +63,8 @@ public QueryTab()
{
InitializeComponent();
nullFont = new Font(dataGridView1.DefaultCellStyle.Font, dataGridView1.DefaultCellStyle.Font.Style | FontStyle.Italic);
DpiHelper.FixRowHeight(dataGridView1);
DpiHelper.FixRowHeight(dataGridView2);
ShowTabs(Tabs.Results);
Disposed += QueryTabDisposed;
AddRunContextMenu();
Expand Down Expand Up @@ -476,9 +479,9 @@ private void queryWorker_RunWorkerCompleted(object sender, System.ComponentModel
}
}

private static void AutoResizeColumns(DataGridView grid)
private void AutoResizeColumns(DataGridView grid)
{
const int maxSize = 200;
int maxSize = base.LogicalToDeviceUnits(200);
const int widthFudgeFactor = 25;

int[] preferredSizes = new int[grid.ColumnCount];
Expand Down
6 changes: 6 additions & 0 deletions Src/SwqlStudio/TabTemplate.cs
@@ -1,4 +1,5 @@
using System.Windows.Forms;
using SwqlStudio.Utils;

namespace SwqlStudio
{
Expand All @@ -20,5 +21,10 @@ public virtual bool AllowsChangeConnection
{
get { return true; }
}

public TabTemplate()
{
DpiHelper.FixFont(this);
}
}
}
49 changes: 49 additions & 0 deletions Src/SwqlStudio/Utils/DpiHelper.cs
@@ -0,0 +1,49 @@
using System.Drawing;
using System.Windows.Forms;

namespace SwqlStudio.Utils
{
/// <summary>
/// A helper class for supporting font and DPI scaling
/// </summary>
/// <remarks>
/// <para>This dimensions of the default font are used to calculate a proper DPI scaling ratio of elements. .Net Framework tries to use "MS Sans Serif"
/// from Windows 3.1, which isn't available anymore. So the system automatically replaces it with "Microsoft Sans Serif", or sometimes with "Tahoma".
/// But those are too old and don't match with Windows UI fonts. And if there are elements using also other fonts, like "Segoe UI", it messes up the calculation.
/// That can happen especially when a different font is used for rendering than for the DPI scaling calculation.</para>
/// <para>This should not be needed anymore in .Net Core 3.1 and .Net 5: https://github.com/dotnet/winforms/pull/656 </para>
/// </remarks>
internal static class DpiHelper
{
/// <summary>
/// The default font used in SWQL Studio dialogs
/// </summary>
/// <remarks>Returns the default .Net font.</remarks>
/// <value>Microsoft Sans Serif, 8.25F</value>
public static readonly Font DefaultFont = new Font("Microsoft Sans Serif", 8.25F, FontStyle.Regular, GraphicsUnit.Point, 0);

/// <summary>
/// Sets the <see cref="DefaultFont"/> as the default font for the <paramref name="control"/>
/// </summary>
/// <param name="control">Usually a <see cref="Form"/></param>
/// <remarks>This is needed to correctly calculate a proper DPI scaling ratio of the <paramref name="control"/> and its child elements.
/// It forces the <paramref name="control"/> to sync the fonts used for its rendering and DPI scaling calculation.
/// <para>This should be called in the <paramref name="control"/>'s constructor.</para></remarks>
public static void FixFont(Control control)
{
control.Font = DefaultFont;
}

/// <summary>
/// Adjusts the default row height in a <see cref="DataGridView"/>
/// </summary>
/// <remarks>The default row height is defined as the height of the default font + 9 pixels.
/// This method, when called after <see cref="FixFont"/>, forces the recalculation of the height.</remarks>
public static void FixRowHeight(DataGridView dataGridView)
{
// Instead of "dataGridView.DefaultCellStyle.Font.Height" we could just hard-code "DefaultFont". That way, this method would not depend
// on a previous call to "FixFont". But this is a bit more universal solution.
dataGridView.RowTemplate.Height = dataGridView.DefaultCellStyle.Font.Height + 9;
}
}
}

0 comments on commit 0728b8e

Please sign in to comment.