Csharp/C Sharp by API/System.Drawing/StringFormat
Содержание
new StringFormat(StringFormatFlags.DirectionVertical)
<source lang="csharp">
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; using System.Drawing.Imaging; public class Form1 : System.Windows.Forms.Form { public Form1() { InitializeComponent(); } private void InitializeComponent() { this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 273); this.Text = ""; this.Resize += new System.EventHandler(this.Form1_Resize); this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint); } static void Main() { Application.Run(new Form1()); } private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { Graphics g = e.Graphics; g.FillRectangle(Brushes.White, this.ClientRectangle); String s = "Accrington Stanley"; StringFormat sf = new StringFormat(StringFormatFlags.DirectionVertical); Font f = new Font("Times New Roman", 14); SizeF sizef = g.MeasureString(s, f, Int32.MaxValue, sf); RectangleF rf = new RectangleF(20, 20, sizef.Width, sizef.Height); g.DrawRectangle(Pens.Black, rf.Left, rf.Top, rf.Width, rf.Height); g.DrawString(s, f, Brushes.Black, rf, sf); f.Dispose(); } private void Form1_Resize(object sender, System.EventArgs e) { Invalidate(); } } </source>
StringFormat.Alignment
<source lang="csharp"> using System; using System.Drawing; using System.Windows.Forms;
class HelloCenteredRectangle: Form {
public static void Main() { Application.Run(new HelloCenteredRectangle()); } public HelloCenteredRectangle() { BackColor = SystemColors.Window; ForeColor = SystemColors.WindowText; ResizeRedraw = true; } protected override void OnPaint(PaintEventArgs pea) { Graphics grfx = pea.Graphics; StringFormat strfmt = new StringFormat(); strfmt.Alignment = StringAlignment.Center; strfmt.LineAlignment = StringAlignment.Center; grfx.DrawString("Hello, world!", Font, new SolidBrush(ForeColor), ClientRectangle, strfmt); }
}
</source>
StringFormat.GenericTypographic
<source lang="csharp"> using System; using System.Collections.Generic; using System.ruponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; public class Form1 : Form {
protected override void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; g.FillRectangle(Brushes.White, this.ClientRectangle); FontFamily ff = new FontFamily("Times New Roman"); Font f = new Font(ff, 12); String s = "Height: " + f.Height; SizeF sf = g.MeasureString(s, f, Int32.MaxValue,StringFormat.GenericTypographic); RectangleF r = new RectangleF(0, 0, sf.Width, f.Height); g.DrawRectangle(Pens.Black, r.Left, r.Top, r.Width, r.Height); g.DrawString(s, f, Brushes.Black, r, StringFormat.GenericTypographic); f.Dispose(); } public static void Main() { Application.Run(new Form1()); }
}
</source>
StringFormat.HotkeyPrefix
<source lang="csharp"> using System; using System.Drawing; using System.Windows.Forms; using System.Drawing.Text; // necessary for HotkeyPrefix public class DrawStringHotkey : Form {
public DrawStringHotkey() { ResizeRedraw = true; } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); Graphics g = e.Graphics; StringFormat fmt = new StringFormat(); fmt.Alignment = StringAlignment.Center; fmt.HotkeyPrefix = HotkeyPrefix.Show; Brush b = new SolidBrush(ForeColor); g.DrawString("&Do It!", Font, b, ClientSize.Width / 2, 50, fmt); } static void Main() { Application.Run(new DrawStringHotkey()); }
}
</source>
StringFormat.LineAlignment
<source lang="csharp">
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; using System.Drawing.Imaging; public class Form1 : System.Windows.Forms.Form { public Form1() { InitializeComponent(); } private void InitializeComponent() { this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 273); this.Text = ""; this.Resize += new System.EventHandler(this.Form1_Resize); this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint); } static void Main() { Application.Run(new Form1()); } private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { Graphics g = e.Graphics; g.FillRectangle(Brushes.White, this.ClientRectangle); String s = "This is a long string that will wrap. "; s += "It will be centered both vertically and horizontally."; Font f = new Font("Arial", 12); StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Center; // horizontal alignment sf.LineAlignment = StringAlignment.Center; // vertical alignment Rectangle r = new Rectangle(10, 10, 300, f.Height * 4); g.DrawRectangle(Pens.Black, r); g.DrawString(s, f, Brushes.Black, r, sf); f.Dispose(); } private void Form1_Resize(object sender, System.EventArgs e) { Invalidate(); } } </source>
StringFormat.SetTabStops
<source lang="csharp">
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; using System.Drawing.Imaging; public class Form1 : System.Windows.Forms.Form { public Form1() { InitializeComponent(); } private void InitializeComponent() { this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 273); this.Text = ""; this.Resize += new System.EventHandler(this.Form1_Resize); this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint); } static void Main() { Application.Run(new Form1()); } private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { Graphics g = e.Graphics; g.FillRectangle(Brushes.White, this.ClientRectangle); Font f = new Font("Times New Roman", 12); Font bf = new Font(f, FontStyle.Bold); StringFormat sf = new StringFormat(); float[] ts = { 10.0f, 70.0f, 100.0f, 90.0f }; sf.SetTabStops(0.0f, ts); // The \t escape-sequence in these lines specifies the tab string s1 = "\tName\tEye Color\tHeight"; string s2 = "\tBob\tBrown\t175cm"; string s3 = "\tMary\tBlond\t161cm\n\tBill\tBlack\t168cm"; g.DrawString(s1, bf, Brushes.Black, 20, 20, sf); g.DrawString(s2, f, Brushes.Blue, 20, 20 + bf.Height, sf); g.DrawString(s3, f, Brushes.Blue, 20, 20 + bf.Height + f.Height, sf); f.Dispose(); bf.Dispose(); } private void Form1_Resize(object sender, System.EventArgs e) { Invalidate(); } } </source>