using System.Windows.Forms; using System.Drawing; using System.ComponentModel; using Microsoft.Win32; namespace Common { 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) { foreach (Control child in (component as Control).Controls) { ApplyToComponent(child); } } if (component is ToolStrip) { //(component as ToolStrip).AutoSize = false; Size newSize = new Size((int)((component as ToolStrip).ImageScalingSize.Width * CustomDpi / 96F), (int)((component as 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; } } */ (component as 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); } */ } if (component is MenuStrip) { (component as MenuStrip).Font = new Font((component as MenuStrip).Font.FontFamily, 8.25F * CustomDpi / 96F, (component as MenuStrip).Font.Style, GraphicsUnit.Point, (component as MenuStrip).Font.GdiCharSet); } if (component is TabControl) { foreach (TabPage tabPage in (component as TabControl).TabPages) { ApplyToComponent(tabPage); } } if (component is ToolStripItem) { (component as ToolStripItem).Font = new Font((component as ToolStripItem).Font.FontFamily, 8.25F * CustomDpi / 96F, (component as ToolStripItem).Font.Style, GraphicsUnit.Point, (component as ToolStripItem).Font.GdiCharSet); } } 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); if (regKey == null) regKey = Application.UserAppDataRegistry.CreateSubKey(regSubKey); regKey.SetValueInt("CustomDpiEnabled", (int)CustomDpi); regKey.SetValueBool("StackTrace", UseCustomDpi); } } }