Changeset 14 for Common/DpiScaling.cs


Ignore:
Timestamp:
Aug 2, 2022, 11:46:25 AM (22 months ago)
Author:
chronos
Message:
  • Modified: Various improvements.
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;
    61using System.Windows.Forms;
    72using System.Drawing;
    83using System.ComponentModel;
     4using Microsoft.Win32;
    95
    106namespace Common
     
    128    class DpiScaling
    139    {
    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;       
    1814
    1915        public static void ApplyToComponent(Component component)
     
    2925            {
    3026                //(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));
    3329/*                foreach (ToolStripItem item in (component as ToolStrip).Items)
    3430                {
     
    5854            if (component is MenuStrip)
    5955            {
    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);
    6258            }
    6359            if (component is TabControl)
     
    7066            if (component is ToolStripItem)
    7167            {
    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);
    7470            }
    7571        }
     
    7773        public static void Apply(Form form)
    7874        {
    79             if (useCustomDpi) changed = true;
    80             if (systemDpi == 0)
     75            if (UseCustomDpi) _changed = true;
     76            if (SystemDpi == 0)
    8177            {
    8278                Graphics graphics = form.CreateGraphics();
    83                 systemDpi = graphics.DpiX;
     79                SystemDpi = graphics.DpiX;
    8480            }
    85             if (useCustomDpi || changed)
     81            if (UseCustomDpi || _changed)
    8682            {
    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);
    8985                ApplyToComponent(form);
    9086            }           
    9187        }
    9288
    93         public static float Scale(float size)
     89        public static float Scale(float size, float baseDpi = 96F)
    9490        {
    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
    96100            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);
    97121        }
    98122    }
Note: See TracChangeset for help on using the changeset viewer.