Microsoft Communities

Welcome to WindowsClient.net | Sign in | Join

Here are some frequently asked questions about Windows Forms and their answers.

Windows Forms FAQs

How can I sync system font changes?

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 form's 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";
}
}
}



Page view counter