dotnetmonitor.com

 
Index
Previous
Next

 

The following example explains how to correctly sync system font changes and make the dynamic change in your applciation.

 

  1. Hook UserPreferenceChanged
  2. Set the Form font again when this event is raised
  3. Unhook from UserPreferenceChanged in forms Dispose

 

NOTE: Make sure you unhook the UserPreferenceChanged in Dispose - otherwise you will cause a GC leak due to the reference to SystemEvents.

 

using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.Win32;
namespace WindowsApplication1 {
    public class Form1 : Form {
        public Form1() {
            this.Font = SystemFonts.IconTitleFont;
            InitializeComponent();
            SystemEvents.UserPreferenceChanged += new UserPreferenceChangedEventHandler(SystemEvents_UserPreferenceChanged);
        }

        void SystemEvents_UserPreferenceChanged(object sender, UserPreferenceChangedEventArgs e) {
            if (e.Category == UserPreferenceCategory.Window) {
                this.Font = SystemFonts.IconTitleFont;
            }
        }

        protected override void Dispose(bool disposing) {
            if (disposing) {
                SystemEvents.UserPreferenceChanged -= new UserPreferenceChangedEventHandler(SystemEvents_UserPreferenceChanged);
                if (components != null) {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent() {
            this.AutoScaleMode = AutoScaleMode.Font;
            this.AutoScaleDimensions = new System.Drawing.SizeF(5, 13);
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Name = "Form1";
            this.Text = "Form1";
        }

    }
}

Terms
Docking Anchor Layout
What happens when I have Dock and Anchors that conflict?
Dock Scenarios
Padding Margin
You have a perf problem with Layout - how to diagnose?