1 | using System.Windows.Forms;
|
---|
2 | using System.Drawing;
|
---|
3 | using System.ComponentModel;
|
---|
4 | using Microsoft.Win32;
|
---|
5 |
|
---|
6 | namespace Common
|
---|
7 | {
|
---|
8 | 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)
|
---|
18 | {
|
---|
19 | foreach (Control child in (component as Control).Controls)
|
---|
20 | {
|
---|
21 | ApplyToComponent(child);
|
---|
22 | }
|
---|
23 | }
|
---|
24 | if (component is ToolStrip)
|
---|
25 | {
|
---|
26 | //(component as ToolStrip).AutoSize = false;
|
---|
27 | Size newSize = new Size((int)((component as ToolStrip).ImageScalingSize.Width * CustomDpi / 96F),
|
---|
28 | (int)((component as 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 | */ (component as 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 | if (component is MenuStrip)
|
---|
55 | {
|
---|
56 | (component as MenuStrip).Font = new Font((component as MenuStrip).Font.FontFamily, 8.25F * CustomDpi / 96F,
|
---|
57 | (component as MenuStrip).Font.Style, GraphicsUnit.Point, (component as MenuStrip).Font.GdiCharSet);
|
---|
58 | }
|
---|
59 | if (component is TabControl)
|
---|
60 | {
|
---|
61 | foreach (TabPage tabPage in (component as TabControl).TabPages)
|
---|
62 | {
|
---|
63 | ApplyToComponent(tabPage);
|
---|
64 | }
|
---|
65 | }
|
---|
66 | if (component is ToolStripItem)
|
---|
67 | {
|
---|
68 | (component as ToolStripItem).Font = new Font((component as ToolStripItem).Font.FontFamily, 8.25F * CustomDpi / 96F,
|
---|
69 | (component as ToolStripItem).Font.Style, GraphicsUnit.Point, (component as ToolStripItem).Font.GdiCharSet);
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | public static void Apply(Form form)
|
---|
74 | {
|
---|
75 | if (UseCustomDpi) _changed = true;
|
---|
76 | if (SystemDpi == 0)
|
---|
77 | {
|
---|
78 | Graphics graphics = form.CreateGraphics();
|
---|
79 | SystemDpi = graphics.DpiX;
|
---|
80 | }
|
---|
81 | if (UseCustomDpi || _changed)
|
---|
82 | {
|
---|
83 | form.Font = new Font(form.Font.FontFamily, 8.25F * CustomDpi / 96F,
|
---|
84 | form.Font.Style, GraphicsUnit.Point, form.Font.GdiCharSet);
|
---|
85 | ApplyToComponent(form);
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | public static float Scale(float size, float baseDpi = 96F)
|
---|
90 | {
|
---|
91 | if (UseCustomDpi)
|
---|
92 | {
|
---|
93 | size = size * CustomDpi / baseDpi;
|
---|
94 | }
|
---|
95 | else
|
---|
96 | {
|
---|
97 | size = size * SystemDpi / baseDpi;
|
---|
98 | }
|
---|
99 |
|
---|
100 | return size;
|
---|
101 | }
|
---|
102 |
|
---|
103 | public static void LoadFromRegistry(string regSubKey = "")
|
---|
104 | {
|
---|
105 | if (Application.UserAppDataRegistry == null) return;
|
---|
106 | RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(regSubKey);
|
---|
107 | if (regKey == null) return;
|
---|
108 |
|
---|
109 | CustomDpi = regKey.GetValueInt("CustomDpiEnabled", (int)CustomDpi);
|
---|
110 | UseCustomDpi = regKey.GetValueBool("StackTrace", UseCustomDpi);
|
---|
111 | }
|
---|
112 |
|
---|
113 | public static void SaveToRegistry(string regSubKey = "")
|
---|
114 | {
|
---|
115 | if (Application.UserAppDataRegistry == null) return;
|
---|
116 | RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(regSubKey, true);
|
---|
117 | if (regKey == null) regKey = Application.UserAppDataRegistry.CreateSubKey(regSubKey);
|
---|
118 |
|
---|
119 | regKey.SetValueInt("CustomDpiEnabled", (int)CustomDpi);
|
---|
120 | regKey.SetValueBool("StackTrace", UseCustomDpi);
|
---|
121 | }
|
---|
122 | }
|
---|
123 | } |
---|