using System.Windows.Forms; using System.Drawing; using System.ComponentModel; using Microsoft.Win32; namespace Common { public class DpiScaling { public static float SystemDpi; public static bool UseCustomDpi; public static float CustomDpi = 96; private static bool _changed; public static void ApplyToComponent(Component component) { if (component is Control control) { foreach (Control child in control.Controls) { ApplyToComponent(child); } } if (component is ToolStrip toolStrip) { //(component as ToolStrip).AutoSize = false; Size newSize = new Size((int)(toolStrip.ImageScalingSize.Width * CustomDpi / 96F), (int)(toolStrip.ImageScalingSize.Height * CustomDpi / 96F)); /* foreach (ToolStripItem item in (component as ToolStrip).Items) { item.AutoSize = false; if (item.Image != null) { Bitmap b = new Bitmap(newSize.Width, newSize.Height); Graphics g = Graphics.FromImage((Image)b); g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.DrawImage(item.Image, 0, 0, newSize.Width, newSize.Height); g.Dispose(); Image myResizedImg = (Image)b; item.Image = myResizedImg; } } */ toolStrip.ImageScalingSize = newSize; /*(component as ToolStrip).Font = new System.Drawing.Font((component as ToolStrip).Font.FontFamily, 8.25F * customDpi / 96F, (component as ToolStrip).Font.Style, System.Drawing.GraphicsUnit.Point, (component as ToolStrip).Font.GdiCharSet); foreach (ToolStripItem item in (component as ToolStrip).Items) { ApplyToComponent(item); } */ } switch (component) { case MenuStrip menuStrip: menuStrip.Font = new Font(menuStrip.Font.FontFamily, 8.25F * CustomDpi / 96F, menuStrip.Font.Style, GraphicsUnit.Point, menuStrip.Font.GdiCharSet); break; case TabControl tabControl: { foreach (TabPage tabPage in tabControl.TabPages) { ApplyToComponent(tabPage); } break; } case ToolStripItem toolStripItem: toolStripItem.Font = new Font(toolStripItem.Font.FontFamily, 8.25F * CustomDpi / 96F, toolStripItem.Font.Style, GraphicsUnit.Point, toolStripItem.Font.GdiCharSet); break; } } public static void Apply(Form form) { if (UseCustomDpi) _changed = true; if (SystemDpi == 0) { Graphics graphics = form.CreateGraphics(); SystemDpi = graphics.DpiX; } if (UseCustomDpi || _changed) { form.Font = new Font(form.Font.FontFamily, 8.25F * CustomDpi / 96F, form.Font.Style, GraphicsUnit.Point, form.Font.GdiCharSet); ApplyToComponent(form); } } public static float Scale(float size, float baseDpi = 96F) { if (UseCustomDpi) { size = size * CustomDpi / baseDpi; } else { size = size * SystemDpi / baseDpi; } return size; } public static void LoadFromRegistry(string regSubKey = "") { if (Application.UserAppDataRegistry == null) return; RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(regSubKey); if (regKey == null) return; CustomDpi = regKey.GetValueInt("CustomDpiEnabled", (int)CustomDpi); UseCustomDpi = regKey.GetValueBool("StackTrace", UseCustomDpi); } public static void SaveToRegistry(string regSubKey = "") { if (Application.UserAppDataRegistry == null) return; RegistryKey regKey = Application.UserAppDataRegistry.OpenSubKey(regSubKey, true) ?? Application.UserAppDataRegistry.CreateSubKey(regSubKey); regKey.SetValueInt("CustomDpiEnabled", (int)CustomDpi); regKey.SetValueBool("StackTrace", UseCustomDpi); } } }