Skip to content

Commit e7b3bf0

Browse files
committed
Fixed several high DPI issues
1 parent d76ac09 commit e7b3bf0

19 files changed

+223
-168
lines changed

src/GuiLibrary/Controls/ListBoxEx.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
#endregion
3030

31+
using Com.Couchcoding.GuiLibrary.Helper;
3132
using System;
3233
using System.ComponentModel;
3334
using System.Drawing;
@@ -135,7 +136,7 @@ public override string ToString()
135136
}
136137
set
137138
{
138-
base.ItemHeight = value;
139+
base.ItemHeight = DpiHelper.RescaleByDpiY(value);
139140
}
140141
}
141142

@@ -155,9 +156,11 @@ public int SeperatorHeight
155156
}
156157
set
157158
{
158-
if (value > 0 && mSeperatorHeight != value)
159+
int newValue = DpiHelper.RescaleByDpiY(value);
160+
161+
if (newValue > 0 && mSeperatorHeight != newValue)
159162
{
160-
mSeperatorHeight = value;
163+
mSeperatorHeight = newValue;
161164
}
162165
}
163166
}

src/GuiLibrary/Dialogs/DialogForm.cs

Lines changed: 11 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
#endregion
3030

31+
using Com.Couchcoding.GuiLibrary.Helper;
3132
using System.ComponentModel;
3233
using System.Drawing;
3334
using System.Windows.Forms;
@@ -41,16 +42,6 @@ public class DialogForm : Form
4142
{
4243
#region Private Consts
4344

44-
/// <summary>
45-
/// Defines the default horizontal resolution.
46-
/// </summary>
47-
protected const float DEFAULT_DPI_Y = 96.0f;
48-
49-
/// <summary>
50-
/// Defines the defualt vertical resolution.
51-
/// </summary>
52-
protected const float DEFAULT_DPI_X = 96.0f;
53-
5445
/// <summary>
5546
/// Defines the height of the button area within the dialog.
5647
/// </summary>
@@ -65,8 +56,6 @@ public class DialogForm : Form
6556

6657
#region Private Fields
6758

68-
private static SizeF mDpiSize = new SizeF(96.0f, 96.0f);
69-
7059
private bool mShowHeaderArea = true;
7160
private bool mShowFooterArea = true;
7261
private Color mContentColor = SystemColors.Control;
@@ -341,46 +330,11 @@ protected void RecalculatePadding()
341330
{
342331
Padding = new Padding(
343332
5
344-
, mShowHeaderArea ? RescaleByDpiY(HEADER_AREA_HEIGHT) + 5 : 5
333+
, mShowHeaderArea ? HEADER_AREA_HEIGHT + 5 : 5
345334
, 5
346335
, 5);
347336
}
348337

349-
/// <summary>
350-
/// Reloads the current DPI-Settings of the system.
351-
/// </summary>
352-
/// <param name="ctrl">The <see cref="Control"/> to use the <see cref="Graphics"/> object from.</param>
353-
protected static void ReloadDpiSettings(Control ctrl)
354-
{
355-
if (ctrl != null)
356-
{
357-
using (Graphics grfx = ctrl.CreateGraphics())
358-
{
359-
mDpiSize = new SizeF(grfx.DpiX, grfx.DpiY);
360-
}
361-
}
362-
}
363-
364-
/// <summary>
365-
/// Rescales the given value by the currently used DPI-Settings.
366-
/// </summary>
367-
/// <param name="value">The value to rescale.</param>
368-
/// <returns>The rescaled value.</returns>
369-
protected static int RescaleByDpiY(int value)
370-
{
371-
return (int)((value / DEFAULT_DPI_Y) * mDpiSize.Height);
372-
}
373-
374-
/// <summary>
375-
/// Rescales the given value by the currently used DPI-Settings.
376-
/// </summary>
377-
/// <param name="value">The value to rescale.</param>
378-
/// <returns>The rescaled value.</returns>
379-
protected static int RescaleByDpiX(int value)
380-
{
381-
return (int)((value / DEFAULT_DPI_X) * mDpiSize.Width);
382-
}
383-
384338
/// <summary>
385339
/// Draws the dialog as a default MSR dialog.
386340
/// </summary>
@@ -389,7 +343,7 @@ protected virtual void DrawDialogBackground(Graphics grfx)
389343
{
390344
if (mShowHeaderArea && Width > 0 && Height > 0)
391345
{
392-
int headerHeight = RescaleByDpiY(HEADER_AREA_HEIGHT);
346+
int headerHeight = DpiHelper.RescaleByDpiY(HEADER_AREA_HEIGHT);
393347

394348
if (mHeaderColor != Color.Transparent && mHeaderColor != mContentColor)
395349
{
@@ -439,8 +393,8 @@ protected virtual void DrawDialogBackground(Graphics grfx)
439393
, captionFont
440394
, captionBrush
441395
, new Point(
442-
RescaleByDpiY(12)
443-
, RescaleByDpiY(8)));
396+
DpiHelper.RescaleByDpiY(12)
397+
, DpiHelper.RescaleByDpiY(8)));
444398
}
445399
}
446400
}
@@ -458,10 +412,10 @@ protected virtual void DrawDialogBackground(Graphics grfx)
458412
, Font
459413
, textBrush
460414
, new RectangleF(
461-
RescaleByDpiY(24)
462-
, RescaleByDpiY(30)
463-
, ClientRectangle.Width - imgSpaceLeft - RescaleByDpiX(30)
464-
, headerHeight - RescaleByDpiY(22))
415+
DpiHelper.RescaleByDpiY(24)
416+
, DpiHelper.RescaleByDpiY(30)
417+
, ClientRectangle.Width - imgSpaceLeft - DpiHelper.RescaleByDpiX(30)
418+
, headerHeight - DpiHelper.RescaleByDpiY(22))
465419
, sFormat);
466420
}
467421
}
@@ -477,7 +431,7 @@ protected override void OnPaint(PaintEventArgs e)
477431
{
478432
base.OnPaint(e);
479433

480-
int buttonHeight = mShowFooterArea ? RescaleByDpiY(BUTTON_AREA_HEIGHT) : 0;
434+
int buttonHeight = mShowFooterArea ? DpiHelper.RescaleByDpiY(BUTTON_AREA_HEIGHT) : 0;
481435

482436
using (Brush contentBrush = new SolidBrush(ContentColor))
483437
{
@@ -524,9 +478,7 @@ protected DialogForm()
524478
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
525479
SetStyle(ControlStyles.ResizeRedraw, true);
526480

527-
AutoScaleMode = AutoScaleMode.Dpi;
528-
Font = SystemFonts.MessageBoxFont;
529-
481+
Font = SystemFonts.MessageBoxFont;
530482
ContentColor = SystemColors.Control;
531483
HeaderColor = Color.White;
532484
HeaderDescriptionColor = Color.DimGray;
@@ -537,7 +489,6 @@ protected DialogForm()
537489
ShowInTaskbar = false;
538490
StartPosition = FormStartPosition.CenterParent;
539491

540-
ReloadDpiSettings(this);
541492
RecalculatePadding();
542493
}
543494

src/GuiLibrary/GuiLibrary.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
<Compile Include="Dialogs\DialogForm.cs">
100100
<SubType>Form</SubType>
101101
</Compile>
102+
<Compile Include="Helper\DpiHelper.cs" />
102103
<Compile Include="Interop\Win32.cs" />
103104
<Compile Include="Properties\AssemblyInfo.cs" />
104105
</ItemGroup>

src/GuiLibrary/Helper/DpiHelper.cs

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#region Copyright © 2015 Couchcoding
2+
3+
// File: DialogForm.cs
4+
// Package: GuiLibrary
5+
// Project: Logbert
6+
//
7+
// The MIT License (MIT)
8+
//
9+
// Copyright (c) 2017 Couchcoding
10+
//
11+
// Permission is hereby granted, free of charge, to any person obtaining a copy
12+
// of this software and associated documentation files (the "Software"), to deal
13+
// in the Software without restriction, including without limitation the rights
14+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15+
// copies of the Software, and to permit persons to whom the Software is
16+
// furnished to do so, subject to the following conditions:
17+
//
18+
// The above copyright notice and this permission notice shall be included in
19+
// all copies or substantial portions of the Software.
20+
//
21+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27+
// THE SOFTWARE.
28+
29+
#endregion
30+
31+
using System.Drawing;
32+
using System.Windows.Forms;
33+
34+
namespace Com.Couchcoding.GuiLibrary.Helper
35+
{
36+
/// <summary>
37+
/// Implements a helper class for DPI scaling.
38+
/// </summary>
39+
public sealed class DpiHelper
40+
{
41+
#region Private Consts
42+
43+
/// <summary>
44+
/// Defines the default horizontal resolution.
45+
/// </summary>
46+
private const float DEFAULT_DPI_Y = 96.0f;
47+
48+
/// <summary>
49+
/// Defines the default vertical resolution.
50+
/// </summary>
51+
private const float DEFAULT_DPI_X = 96.0f;
52+
53+
#endregion
54+
55+
#region Private Fields
56+
57+
/// <summary>
58+
/// Holds the calculated DPI scaling.
59+
/// </summary>
60+
private static SizeF mDpiSize = new SizeF(96.0f, 96.0f);
61+
62+
#endregion
63+
64+
#region Public Methods
65+
66+
/// <summary>
67+
/// Reloads the current DPI-Settings of the system.
68+
/// </summary>
69+
/// <param name="ctrl">The <see cref="Control"/> to use the <see cref="Graphics"/> object from.</param>
70+
public static void ReloadDpiSettings(Control ctrl)
71+
{
72+
if (ctrl != null)
73+
{
74+
using (Graphics grfx = ctrl.CreateGraphics())
75+
{
76+
mDpiSize = new SizeF(grfx.DpiX, grfx.DpiY);
77+
}
78+
}
79+
}
80+
81+
/// <summary>
82+
/// Rescales the given value by the currently used DPI-Settings.
83+
/// </summary>
84+
/// <param name="value">The value to rescale.</param>
85+
/// <returns>The rescaled value.</returns>
86+
public static int RescaleByDpiY(int value)
87+
{
88+
return (int)((value / DEFAULT_DPI_Y) * mDpiSize.Height);
89+
}
90+
91+
/// <summary>
92+
/// Rescales the given value by the currently used DPI-Settings.
93+
/// </summary>
94+
/// <param name="value">The value to rescale.</param>
95+
/// <returns>The rescaled value.</returns>
96+
public static int RescaleByDpiX(int value)
97+
{
98+
return (int)((value / DEFAULT_DPI_X) * mDpiSize.Width);
99+
}
100+
101+
#endregion
102+
}
103+
}

src/GuiLibrary/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@
3131
// You can specify all the values or you can default the Build and Revision Numbers
3232
// by using the '*' as shown below:
3333
// [assembly: AssemblyVersion("1.0.*")]
34-
[assembly: AssemblyVersion("1.3.1.0")]
35-
[assembly: AssemblyFileVersion("1.3.1.0")]
34+
[assembly: AssemblyVersion("1.3.2.0")]
35+
[assembly: AssemblyFileVersion("1.3.2.0")]

src/Logbert/Dialogs/Docking/FrmLogDocument.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
using Com.Couchcoding.Logbert.Properties;
4444

4545
using WeifenLuo.WinFormsUI.Docking;
46+
using Com.Couchcoding.GuiLibrary.Helper;
4647

4748
namespace Com.Couchcoding.Logbert.Dialogs.Docking
4849
{

0 commit comments

Comments
 (0)