forked from fdorg/flashdevelop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScaleHelper.cs
More file actions
133 lines (120 loc) · 3.72 KB
/
Copy pathScaleHelper.cs
File metadata and controls
133 lines (120 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using PluginCore.Utilities;
namespace PluginCore.Helpers
{
public class ScaleHelper
{
/// <summary>
/// Private properties
/// </summary>
private static double curScale = double.MinValue;
private static HashSet<Control> adjustedItems = new HashSet<Control>();
/// <summary>
/// Gets the display scale. Ideally would probably keep separate scales for X and Y.
/// </summary>
public static double GetScale()
{
if (curScale != double.MinValue) return curScale;
using (var g = Graphics.FromHwnd(PluginBase.MainForm.Handle))
{
curScale = g.DpiX / 96f;
}
return curScale;
}
/// <summary>
/// Resizes based on display scale.
/// </summary>
public static int Scale(int value)
{
return (int)(value * GetScale());
}
/// <summary>
/// Resizes based on display scale.
/// </summary>
public static long Scale(long value)
{
return (long)(value * GetScale());
}
/// <summary>
/// Resizes based on display scale.
/// </summary>
public static float Scale(float value)
{
return (float)(value * GetScale());
}
/// <summary>
/// Resizes based on display scale.
/// </summary>
public static double Scale(double value)
{
return value * GetScale();
}
/// <summary>
/// Resizes based on display scale.
/// </summary>
public static Size Scale(Size value)
{
return new Size(Scale(value.Width), Scale(value.Height));
}
/// <summary>
/// Resizes based on display scale.
/// </summary>
public static SizeF Scale(SizeF value)
{
return new SizeF(Scale(value.Width), Scale(value.Height));
}
/// <summary>
/// Resizes the image based on the display scale. Uses high quality settings.
/// </summary>
public static Bitmap Scale(Bitmap image)
{
if (GetScale() == 1) return image;
int width = Scale(image.Width);
int height = Scale(image.Height);
return (Bitmap)ImageKonverter.ImageResize(image, width, height);
}
/// <summary>
/// Adjusts the specified control for better high dpi look
/// </summary>
public static void AdjustForHighDPI(Control control, double multi)
{
if (IsAdjusted(control)) return;
double scale = GetScale();
foreach (Control ctrl in control.Controls)
{
if (ctrl is Button)
{
if (scale >= 1.5)
{
double noPad = ctrl.Height * multi;
ctrl.Height = (Int32)noPad;
}
}
AdjustForHighDPI(ctrl, multi);
}
}
public static void AdjustForHighDPI(Control control)
{
AdjustForHighDPI(control, 0.92);
}
/// <summary>
/// Keep track and adjust forms only once
/// </summary>
private static Boolean IsAdjusted(Control control)
{
if (control is Form)
{
if (adjustedItems.Contains(control)) return true;
else
{
adjustedItems.Add(control);
return false;
}
}
else return false;
}
}
}