using System; using System.Drawing; using System.Windows.Forms; class HuckleberryFinnHalfHeight: Form { public static void Main() { Application.Run(new HuckleberryFinnHalfHeight()); } public HuckleberryFinnHalfHeight() { ResizeRedraw = true; } protected override void OnPaint(PaintEventArgs pea) { Graphics grfx = pea.Graphics; int cx = ClientSize.Width; int cy = ClientSize.Height; Pen pen = new Pen(ForeColor); Rectangle rect = new Rectangle(0, 0, cx / 2, cy / 2); grfx.DrawString("some", Font, new SolidBrush(ForeColor), rect); grfx.DrawLine(pen, 0, cy / 2, cx / 2, cy / 2); grfx.DrawLine(pen, cx / 2, 0, cx / 2, cy / 2); } }
DrawString with solid brush, string format
using System; using System.Drawing; using System.Drawing.Text; using System.Windows.Forms; class BoldAndItalicTighter: Form { public static void Main() { Application.Run(new BoldAndItalicTighter()); } public BoldAndItalicTighter() { Text = "Bold and Italic (Tighter)"; Font = new Font("Times New Roman", 24); ResizeRedraw = true; } protected override void OnPaint(PaintEventArgs pea) { DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height); } protected void DoPage(Graphics grfx, Color clr, int cx, int cy) { string str = "text."; Brush brush = new SolidBrush(clr); Font fontRegular = Font; Font fontBold = new Font(fontRegular, FontStyle.Bold); Font fontItalic = new Font(fontRegular, FontStyle.Italic); PointF ptf = new PointF(0, 0); StringFormat strfmt = StringFormat.GenericTypographic; strfmt.FormatFlags |= StringFormatFlags.MeasureTrailingSpaces; grfx.DrawString(str, fontRegular, brush, ptf, strfmt); } }
TextRenderingHint: Anti-Aliased Text
using System;
using System.Drawing;
using System.Drawing.Text;
using System.Windows.Forms;
class AntiAliasedText: Form
{
public static void Main()
{
Application.Run(new AntiAliasedText());
}
public AntiAliasedText()
{
Font = new Font(“Times New Roman”, 12);
ResizeRedraw = true;
}
protected override void OnPaint(PaintEventArgs pea)
{
DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
}
protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
{
Brush brush = new SolidBrush(clr);
string str = “A “;
int cxText = (int) grfx.MeasureString(str, Font).Width;
for (int i = 0; i < 6; i++) { grfx.TextRenderingHint = (TextRenderingHint)i; grfx.DrawString(str, Font, brush, i * cxText, 0); } } } [/csharp]
Use TexturedBrush to draw string
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; public class MainForm : Form { private Brush texturedTextBrush; private Brush texturedBGroundBrush; public MainForm() { Image bGroundBrushImage = new Bitmap("Clouds.bmp"); texturedBGroundBrush = new TextureBrush(bGroundBrushImage); Image textBrushImage = new Bitmap("Soap Bubbles.bmp"); texturedTextBrush = new TextureBrush(textBrushImage); } protected void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; Rectangle r = ClientRectangle; g.FillRectangle(texturedBGroundBrush, r); g.DrawString("Bitmaps as brushes", new Font("Arial", 30, FontStyle.Bold | FontStyle.Italic), texturedTextBrush, r); } public static void Main(){ Application.Run(new MainForm()); } }
Hotkey Prefix
using System; using System.Drawing; using System.Drawing.Text; using System.Windows.Forms; class UnderlinedText: Form { public static void Main() { Application.Run(new UnderlinedText()); } public UnderlinedText() { Text = "Underlined Text Using HotkeyPrefix"; Font = new Font("Times New Roman", 14); ResizeRedraw = true; } protected override void OnPaint(PaintEventArgs pea) { DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height); } protected void DoPage(Graphics grfx, Color clr, int cx, int cy) { string str = "This is some &u&n&d&e&r&l&i&n&e&d text!"; StringFormat strfmt = new StringFormat(); strfmt.HotkeyPrefix = HotkeyPrefix.Show; grfx.DrawString(str, Font, new SolidBrush(clr), 0, 0, strfmt); } }
Draw Rectangle surrounding the Text
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Drawing2D; using System.Text; using System.Windows.Forms; public class Form1 : Form { protected override void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; g.PageUnit = GraphicsUnit.Inch; Pen p = new Pen(Color.Black, 1 / 96f); Font f = new Font("Times New Roman", 16); String s = "Abc"; SizeF sf = g.MeasureString(s, f); g.DrawRectangle(p, 1, 1, sf.Width, sf.Height); g.DrawString(s, f, Brushes.Black, 1, 1); f.Dispose(); p.Dispose(); } public static void Main() { Application.Run(new Form1()); } }
Rotate the text 45 degrees clockwise and translate the text 150 pixels horizontally
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Drawing2D; 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); Font f = new Font("Times New Roman", 24); g.DrawString("Rotate then Translate", f, Brushes.Black, 0, 0); g.RotateTransform(45); g.TranslateTransform(150, 0); g.DrawString("Rotate then Translate ", f, Brushes.Black, 0, 0); } public static void Main() { Application.Run(new Form1()); } }