Thursday, March 3, 2011

scale fonts together with controls

in WinForms I can use Control.Scale to scale control larger. when I do that, all child controls are repositioned and scaled correctly, but font size remains the same.

is there an easy way to force font to scale up/down as well? or is the only way for me to manually update font for all controls when control is being scaled?

background: I'm working on a program in which I need to support zoom in/out to make labels, textboxs, etc. more readable for users with poor eyesight.

From stackoverflow
  • Do it the other way around. Change the font size, the controls will automatically scale to accommodate the larger font. For example:

      public partial class Form1 : Form {
        float mDesignSize;
        int mIncrement;
        public Form1() {
          InitializeComponent();
          mDesignSize = this.Font.SizeInPoints;
        }
        private void adjustFont() {
          float size = mDesignSize * (1 + mIncrement / 7f);
          this.Font = new Font(this.Font.FontFamily, size);
        }
        private void btnIncreaseFontSize_Click(object sender, EventArgs e) {
          mIncrement += 1;
          adjustFont();
        }
        private void btnDecreateFontSize_Click(object sender, EventArgs e) {
          mIncrement -= 1;
          adjustFont();
        }
      }
    
    Ornus : this won't scale controls location. I can use table layout, but it has it's own drawbacks.
  • I couldn't find solution so I ended up scaling font by hand.

    I'm using Krypton Toolkit *highly recommended, great controls library) which supports themes. I simply used reflection to find all font properties and scale them up.

0 comments:

Post a Comment