Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Configurable font height/width

Configurable font height/width

Scheduled Pinned Locked Moved C#
tutorial
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    Yaron K
    wrote on last edited by
    #1

    hi I need to print text but the font height has to be configurable. I know I can change the font size at run time, but changing the font size changes both height and width proportioally. For example I need to increase the witdh X2 ,and the height X 4. Thanks!

    H 1 Reply Last reply
    0
    • Y Yaron K

      hi I need to print text but the font height has to be configurable. I know I can change the font size at run time, but changing the font size changes both height and width proportioally. For example I need to increase the witdh X2 ,and the height X 4. Thanks!

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      Fonts are graphical representation of characters and are designed to resize proportionally. If you need to resize them disproportionately, then you'll need to transform them. One way would be to either create a new Graphics device or to adjust your math - if possible - to account for the transformation you'll be performing to the Graphics object. To perform the transformation, you can use a myriad of methods on the Graphics class - all documented in the .NET Framework SDK. One such method follows:

      using System;
      using System.ComponentModel;
      using System.Drawing;
      using System.Drawing.Drawing2D;
      using System.Windows.Forms;
       
      class Test : Form
      {
      static void Main()
      {
      Application.Run(new Test());
      }
       
      Test()
      {
      StretchLabel lbl = new StretchLabel();
      Controls.Add(lbl);
      lbl.Dock = DockStyle.Fill;
      lbl.ScaleHeight = 4f;
      lbl.Font = new Font(Font.FontFamily, 20f);
      lbl.ScaleWidth = 2f;
      lbl.Text = "Sample";
       
      Text = "Sample";
      }
      }
       
      class StretchLabel : Label
      {
      SizeF size = SizeF.Empty;
      float scaleWidth = 1f;
      float scaleHeight = 1f;
       
      [DefaultValue(1f)]
      public float ScaleWidth
      {
      get { return scaleWidth; }
      set { scaleWidth = value; }
      }
       
      [DefaultValue(1f)]
      public float ScaleHeight
      {
      get { return scaleHeight; }
      set { scaleHeight = value; }
      }
       
      protected override void OnTextChanged(EventArgs e)
      {
      using (Graphics g = CreateGraphics())
      size = g.MeasureString(Text, Font, (SizeF)Size);
       
      base.OnTextChanged(e);
      }
       
      protected override void OnPaint(PaintEventArgs e)
      {
      Graphics g = e.Graphics; // DO NOT dispose this Graphics object.
      g.ScaleTransform(scaleWidth, scaleHeight, MatrixOrder.Append);
      g.DrawString(Text, Font, new SolidBrush(ForeColor), PointF.Empty);
       
      base.OnPaint(e);
      }
      }

      Pay close attention to the Graphics.ScaleTransform, which I recommend reading about in the .NET Framework SDK. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles]

      Y 2 Replies Last reply
      0
      • H Heath Stewart

        Fonts are graphical representation of characters and are designed to resize proportionally. If you need to resize them disproportionately, then you'll need to transform them. One way would be to either create a new Graphics device or to adjust your math - if possible - to account for the transformation you'll be performing to the Graphics object. To perform the transformation, you can use a myriad of methods on the Graphics class - all documented in the .NET Framework SDK. One such method follows:

        using System;
        using System.ComponentModel;
        using System.Drawing;
        using System.Drawing.Drawing2D;
        using System.Windows.Forms;
         
        class Test : Form
        {
        static void Main()
        {
        Application.Run(new Test());
        }
         
        Test()
        {
        StretchLabel lbl = new StretchLabel();
        Controls.Add(lbl);
        lbl.Dock = DockStyle.Fill;
        lbl.ScaleHeight = 4f;
        lbl.Font = new Font(Font.FontFamily, 20f);
        lbl.ScaleWidth = 2f;
        lbl.Text = "Sample";
         
        Text = "Sample";
        }
        }
         
        class StretchLabel : Label
        {
        SizeF size = SizeF.Empty;
        float scaleWidth = 1f;
        float scaleHeight = 1f;
         
        [DefaultValue(1f)]
        public float ScaleWidth
        {
        get { return scaleWidth; }
        set { scaleWidth = value; }
        }
         
        [DefaultValue(1f)]
        public float ScaleHeight
        {
        get { return scaleHeight; }
        set { scaleHeight = value; }
        }
         
        protected override void OnTextChanged(EventArgs e)
        {
        using (Graphics g = CreateGraphics())
        size = g.MeasureString(Text, Font, (SizeF)Size);
         
        base.OnTextChanged(e);
        }
         
        protected override void OnPaint(PaintEventArgs e)
        {
        Graphics g = e.Graphics; // DO NOT dispose this Graphics object.
        g.ScaleTransform(scaleWidth, scaleHeight, MatrixOrder.Append);
        g.DrawString(Text, Font, new SolidBrush(ForeColor), PointF.Empty);
         
        base.OnPaint(e);
        }
        }

        Pay close attention to the Graphics.ScaleTransform, which I recommend reading about in the .NET Framework SDK. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles]

        Y Offline
        Y Offline
        Yaron K
        wrote on last edited by
        #3

        Thanks! will try that.

        1 Reply Last reply
        0
        • H Heath Stewart

          Fonts are graphical representation of characters and are designed to resize proportionally. If you need to resize them disproportionately, then you'll need to transform them. One way would be to either create a new Graphics device or to adjust your math - if possible - to account for the transformation you'll be performing to the Graphics object. To perform the transformation, you can use a myriad of methods on the Graphics class - all documented in the .NET Framework SDK. One such method follows:

          using System;
          using System.ComponentModel;
          using System.Drawing;
          using System.Drawing.Drawing2D;
          using System.Windows.Forms;
           
          class Test : Form
          {
          static void Main()
          {
          Application.Run(new Test());
          }
           
          Test()
          {
          StretchLabel lbl = new StretchLabel();
          Controls.Add(lbl);
          lbl.Dock = DockStyle.Fill;
          lbl.ScaleHeight = 4f;
          lbl.Font = new Font(Font.FontFamily, 20f);
          lbl.ScaleWidth = 2f;
          lbl.Text = "Sample";
           
          Text = "Sample";
          }
          }
           
          class StretchLabel : Label
          {
          SizeF size = SizeF.Empty;
          float scaleWidth = 1f;
          float scaleHeight = 1f;
           
          [DefaultValue(1f)]
          public float ScaleWidth
          {
          get { return scaleWidth; }
          set { scaleWidth = value; }
          }
           
          [DefaultValue(1f)]
          public float ScaleHeight
          {
          get { return scaleHeight; }
          set { scaleHeight = value; }
          }
           
          protected override void OnTextChanged(EventArgs e)
          {
          using (Graphics g = CreateGraphics())
          size = g.MeasureString(Text, Font, (SizeF)Size);
           
          base.OnTextChanged(e);
          }
           
          protected override void OnPaint(PaintEventArgs e)
          {
          Graphics g = e.Graphics; // DO NOT dispose this Graphics object.
          g.ScaleTransform(scaleWidth, scaleHeight, MatrixOrder.Append);
          g.DrawString(Text, Font, new SolidBrush(ForeColor), PointF.Empty);
           
          base.OnPaint(e);
          }
          }

          Pay close attention to the Graphics.ScaleTransform, which I recommend reading about in the .NET Framework SDK. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles]

          Y Offline
          Y Offline
          Yaron K
          wrote on last edited by
          #4

          I think I got it to do what I want. Here is the code using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Drawing.Drawing2D; namespace FontSizeTest { /// /// Summary description for Form1. /// public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.NotifyIcon notifyIcon1; private System.Windows.Forms.Panel panel2; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label1; private System.Windows.Forms.TextBox txtH; private System.Windows.Forms.TextBox txtW; private System.Windows.Forms.TextBox txtStr; private System.Windows.Forms.Button btnGo; private System.Windows.Forms.PictureBox pictureBox1; private System.ComponentModel.IContainer components; public Form1() { InitializeComponent(); } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components); this.panel2 = new System.Windows.Forms.Panel(); this.label2 = new System.Windows.Forms.Label(); this.label1 = new System.Windows.Forms.Label(); this.txtH = new System.Windows.Forms.TextBox(); this.txtW = new System.Windows.Forms.TextBox(); this.txtStr = new System.Windows.Forms.TextBox(); this.btnGo = new System.Windows.Forms.Button(); this.pictureBox1 = new System.Windows.Forms.PictureBox(); this.panel2.SuspendLayout(); this.SuspendLayout(); // // notifyIcon1 // this.notifyIcon1.Text = "notifyIcon1"; this.notifyIcon1.Visible = true; // // panel2 // this.panel2.Controls.Add(this.label2); this.panel2.Controls.Add(this.label1); this.panel2.Controls.Add(this.txtH); this.panel2.Controls.Add(this.txtW); this.panel2.Controls.Add(this.txtStr); this.panel2.Controls.Add(this.btnGo); this.panel2.Dock = System.Windows.Forms.DockStyle.Bottom; this.panel2.Loca

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups