Configurable font height/width
-
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!
-
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!
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 theGraphics
object. To perform the transformation, you can use a myriad of methods on theGraphics
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] -
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 theGraphics
object. To perform the transformation, you can use a myriad of methods on theGraphics
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] -
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 theGraphics
object. To perform the transformation, you can use a myriad of methods on theGraphics
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]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