Changeset 14 for Common/DpiScaling.cs
- Timestamp:
- Aug 2, 2022, 11:46:25 AM (22 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Common/DpiScaling.cs
r1 r14 1 using System;2 using System.Collections.Generic;3 using System.Linq;4 using System.Text;5 using System.Threading.Tasks;6 1 using System.Windows.Forms; 7 2 using System.Drawing; 8 3 using System.ComponentModel; 4 using Microsoft.Win32; 9 5 10 6 namespace Common … … 12 8 class DpiScaling 13 9 { 14 public static float systemDpi = 0;15 public static bool useCustomDpi = false;16 public static float customDpi = 96;17 private static bool changed = false;10 public static float SystemDpi; 11 public static bool UseCustomDpi; 12 public static float CustomDpi = 96; 13 private static bool _changed; 18 14 19 15 public static void ApplyToComponent(Component component) … … 29 25 { 30 26 //(component as ToolStrip).AutoSize = false; 31 Size newSize = new Size((int)((component as ToolStrip).ImageScalingSize.Width * customDpi / 96F),32 (int)((component as ToolStrip).ImageScalingSize.Height * customDpi / 96F));27 Size newSize = new Size((int)((component as ToolStrip).ImageScalingSize.Width * CustomDpi / 96F), 28 (int)((component as ToolStrip).ImageScalingSize.Height * CustomDpi / 96F)); 33 29 /* foreach (ToolStripItem item in (component as ToolStrip).Items) 34 30 { … … 58 54 if (component is MenuStrip) 59 55 { 60 (component as MenuStrip).Font = new System.Drawing.Font((component as MenuStrip).Font.FontFamily, 8.25F * customDpi / 96F,61 (component as MenuStrip).Font.Style, System.Drawing.GraphicsUnit.Point, (component as MenuStrip).Font.GdiCharSet);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); 62 58 } 63 59 if (component is TabControl) … … 70 66 if (component is ToolStripItem) 71 67 { 72 (component as ToolStripItem).Font = new System.Drawing.Font((component as ToolStripItem).Font.FontFamily, 8.25F * customDpi / 96F,73 (component as ToolStripItem).Font.Style, System.Drawing.GraphicsUnit.Point, (component as ToolStripItem).Font.GdiCharSet);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); 74 70 } 75 71 } … … 77 73 public static void Apply(Form form) 78 74 { 79 if ( useCustomDpi)changed = true;80 if ( systemDpi == 0)75 if (UseCustomDpi) _changed = true; 76 if (SystemDpi == 0) 81 77 { 82 78 Graphics graphics = form.CreateGraphics(); 83 systemDpi = graphics.DpiX;79 SystemDpi = graphics.DpiX; 84 80 } 85 if ( useCustomDpi ||changed)81 if (UseCustomDpi || _changed) 86 82 { 87 form.Font = new System.Drawing.Font(form.Font.FontFamily, 8.25F * customDpi / 96F,88 form.Font.Style, System.Drawing.GraphicsUnit.Point, form.Font.GdiCharSet);83 form.Font = new Font(form.Font.FontFamily, 8.25F * CustomDpi / 96F, 84 form.Font.Style, GraphicsUnit.Point, form.Font.GdiCharSet); 89 85 ApplyToComponent(form); 90 86 } 91 87 } 92 88 93 public static float Scale(float size )89 public static float Scale(float size, float baseDpi = 96F) 94 90 { 95 if (useCustomDpi) size = size * customDpi / 96F; 91 if (UseCustomDpi) 92 { 93 size = size * CustomDpi / baseDpi; 94 } 95 else 96 { 97 size = size * SystemDpi / baseDpi; 98 } 99 96 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); 97 121 } 98 122 }
Note:
See TracChangeset
for help on using the changeset viewer.