1 | using System.Windows.Forms;
|
---|
2 | using System.Drawing;
|
---|
3 | using System.ComponentModel;
|
---|
4 | using Microsoft.Win32;
|
---|
5 |
|
---|
6 | namespace Common
|
---|
7 | {
|
---|
8 | public class DpiScaling
|
---|
9 | {
|
---|
10 | public static float SystemDpi;
|
---|
11 | public static bool UseCustomDpi;
|
---|
12 | public static float CustomDpi = 96;
|
---|
13 | private static bool _changed;
|
---|
14 |
|
---|
15 | public static void ApplyToComponent(Component component)
|
---|
16 | {
|
---|
17 | if (component is Control control)
|
---|
18 | {
|
---|
19 | foreach (Control child in control.Controls)
|
---|
20 | {
|
---|
21 | ApplyToComponent(child);
|
---|
22 | }
|
---|
23 | }
|
---|
24 | if (component is ToolStrip toolStrip)
|
---|
25 | {
|
---|
26 | //(component as ToolStrip).AutoSize = false;
|
---|
27 | Size newSize = new Size((int)(toolStrip.ImageScalingSize.Width * CustomDpi / 96F),
|
---|
28 | (int)(toolStrip.ImageScalingSize.Height * CustomDpi / 96F));
|
---|
29 | /* foreach (ToolStripItem item in (component as ToolStrip).Items)
|
---|
30 | {
|
---|
31 | item.AutoSize = false;
|
---|
32 |
|
---|
33 | if (item.Image != null)
|
---|
34 | {
|
---|
35 | Bitmap b = new Bitmap(newSize.Width, newSize.Height);
|
---|
36 | Graphics g = Graphics.FromImage((Image)b);
|
---|
37 | g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
|
---|
38 | g.DrawImage(item.Image, 0, 0, newSize.Width, newSize.Height);
|
---|
39 | g.Dispose();
|
---|
40 | Image myResizedImg = (Image)b;
|
---|
41 | item.Image = myResizedImg;
|
---|
42 | }
|
---|
43 | }
|
---|
44 | */ toolStrip.ImageScalingSize = newSize;
|
---|
45 |
|
---|
46 | /*(component as ToolStrip).Font = new System.Drawing.Font((component as ToolStrip).Font.FontFamily, 8.25F * customDpi / 96F,
|
---|
47 | (component as ToolStrip).Font.Style, System.Drawing.GraphicsUnit.Point, (component as ToolStrip).Font.GdiCharSet);
|
---|
48 | foreach (ToolStripItem item in (component as ToolStrip).Items)
|
---|
49 | {
|
---|
50 | ApplyToComponent(item);
|
---|
51 | }
|
---|
52 | */
|
---|
53 | }
|
---|
54 | switch (component)
|
---|
55 | {
|
---|
56 | case MenuStrip menuStrip:
|
---|
57 | menuStrip.Font = new Font(menuStrip.Font.FontFamily, 8.25F * CustomDpi / 96F,
|
---|
58 | menuStrip.Font.Style, GraphicsUnit.Point, menuStrip.Font.GdiCharSet);
|
---|
59 | break;
|
---|
60 | case TabControl tabControl:
|
---|
61 | {
|
---|
62 | foreach (TabPage tabPage in tabControl.TabPages)
|
---|
63 | {
|
---|
64 | ApplyToComponent(tabPage);
|
---|
65 | }
|
---|
66 |
|
---|
67 | break;
|
---|
68 | }
|
---|
69 | case ToolStripItem toolStripItem:
|
---|
70 | toolStripItem.Font = new Font(toolStripItem.Font.FontFamily, 8.25F * CustomDpi / 96F,
|
---|
71 | toolStripItem.Font.Style, GraphicsUnit.Point, toolStripItem.Font.GdiCharSet);
|
---|
72 | break;
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | public static void Apply(Form form)
|
---|
77 | {
|
---|
78 | if (UseCustomDpi) _changed = true;
|
---|
79 | if (SystemDpi == 0)
|
---|
80 | {
|
---|
81 | Graphics graphics = form.CreateGraphics();
|
---|
82 | SystemDpi = graphics.DpiX;
|
---|
83 | }
|
---|
84 | if (UseCustomDpi || _changed)
|
---|
85 | {
|
---|
86 | form.Font = new Font(form.Font.FontFamily, 8.25F * CustomDpi / 96F,
|
---|
87 | form.Font.Style, GraphicsUnit.Point, form.Font.GdiCharSet);
|
---|
88 | ApplyToComponent(form);
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | public static float Scale(float size, float baseDpi = 96F)
|
---|
93 | {
|
---|
94 | if (UseCustomDpi)
|
---|
95 | {
|
---|
96 | size = size * CustomDpi / baseDpi;
|
---|
97 | }
|
---|
98 | else
|
---|
99 | {
|
---|
100 | size = size * SystemDpi / baseDpi;
|
---|
101 | }
|
---|
102 |
|
---|
103 | return size;
|
---|
104 | }
|
---|
105 |
|
---|
106 | public static void LoadFromRegistry(string regSubKey = "")
|
---|
107 | {
|
---|
108 | if (Application.UserAppDataRegistry == null) return;
|
---|
109 | RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(regSubKey);
|
---|
110 | if (regKey == null) return;
|
---|
111 |
|
---|
112 | CustomDpi = regKey.GetValueInt("CustomDpiEnabled", (int)CustomDpi);
|
---|
113 | UseCustomDpi = regKey.GetValueBool("StackTrace", UseCustomDpi);
|
---|
114 | }
|
---|
115 |
|
---|
116 | public static void SaveToRegistry(string regSubKey = "")
|
---|
117 | {
|
---|
118 | if (Application.UserAppDataRegistry == null) return;
|
---|
119 | RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(regSubKey, true) ??
|
---|
120 | Application.UserAppDataRegistry.CreateSubKey(regSubKey);
|
---|
121 |
|
---|
122 | regKey.SetValueInt("CustomDpiEnabled", (int)CustomDpi);
|
---|
123 | regKey.SetValueBool("StackTrace", UseCustomDpi);
|
---|
124 | }
|
---|
125 | }
|
---|
126 | }
|
---|