Csharp/C Sharp/GUI Windows Form
Версия от 15:31, 26 мая 2010; (обсуждение)
- Agent Help
- Anchor
- Bitmap
- Border
- Button
- Calendar
- CheckBox
- CheckBox List
- Clipboard
- Clock
- Color Dialog
- ComboBox
- Console
- ContentAlignment
- Control
- Control Anchor
- Cursor
- Data Binding
- Data Validation
- DataGrid
- DataGrid Data Binding
- DateTimePicker
- Dialog
- Docking
- Drag Drop
- Editor
- Focus
- Form Event
- Form Frame Window
- Form Style
- Full Screen
- GroupBox
- Help System
- ImageList
- Irregularly Shaped Forms
- Label
- Layout
- LinkLabel
- ListBox
- ListBox Data Binding
- ListView
- MDI
- MaskedTextBox
- Menu
- MenuItem
- MessageBox
- Mouse
- Mouse On Form
- NativeWindow
- Open File Dialog
- PageSetupDialog
- Paint
- Panel
- Password
- PictureBox
- Popup Menu
- Print Dialog
- PrinterSettings
- ProgressBar
- Property Grid
- RadioButton
- ResXResourceWriter
- ResourceManager
- ResourceReader
- ResourceWriter
- RichTextBox
- SaveFileDialog
- ScrollBar
- Scrollable
- Splitter
- StatusBar
- System Tray Icon
- Tab
- TextBox
- Thread and UI
- Time
- ToolBar
- ToolStripMenuItem
- Tooltips
- Track Bar
- Tray Icon
- TreeView
- Unicode Display
- UpDown
- User Control
- Validation
- Web Browser
Содержание
- 1 All font properties
- 2 Color Dialog and Font Dialog
- 3 Font: GetHeight
- 4 Font list
- 5 Font: SizeInPoints
- 6 Font with different size
- 7 Get all system installed font
- 8 Get the font in a FontDialog
- 9 Load all system installed fonts
- 10 new Font(fontRegular, FontStyle.Italic);
- 11 new Font(strFont, fSize)
- 12 new Font("Times New Roman", 10, FontStyle.Italic)
- 13 System Fonts: Icon Title Font
- 14 Text on Baseline
- 15 Timer based animation for font
All font properties
using System;
using System.Drawing;
using System.Windows.Forms;
class AllAboutFont: Form
{
public static void Main()
{
Application.Run(new AllAboutFont());
}
public AllAboutFont()
{
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)
{
grfx.DrawString(
"Name: " + Font.Name + "\n" +
"FontFamily: " + Font.FontFamily + "\n" +
"FontStyle: " + Font.Style + "\n" +
"Bold: " + Font.Bold + "\n" +
"Italic: " + Font.Italic + "\n" +
"Underline: " + Font.Underline + "\n" +
"Strikeout: " + Font.Strikeout + "\n" +
"Size: " + Font.Size + "\n" +
"GraphicsUnit: " + Font.Unit + "\n" +
"SizeInPoints: " + Font.SizeInPoints + "\n" +
"Height: " + Font.Height + "\n" +
"GdiCharSet: " + Font.GdiCharSet + "\n" +
"GdiVerticalFont : " + Font.GdiVerticalFont + "\n" +
"GetHeight(): " + Font.GetHeight() + "\n" +
"GetHeight(grfx): " + Font.GetHeight(grfx) + "\n" +
"GetHeight(100 DPI): " + Font.GetHeight(100),
Font, new SolidBrush(clr), Point.Empty);
}
}
Color Dialog and Font Dialog
/*
Professional Windows GUI Programming Using C#
by Jay Glynn, Csaba Torok, Richard Conway, Wahid Choudhury,
Zach Greenvoss, Shripad Kulkarni, Neil Whitlow
Publisher: Peer Information
ISBN: 1861007663
*/
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
namespace ColorFontDialog
{
/// <summary>
/// Summary description for ColorFontDialog.
/// </summary>
public class ColorFontDialog : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Label label1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ruponentModel.Container components = null;
public ColorFontDialog()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
this.Text = "Font and Color Dialogs";
this.button1.Text = "&Font";
this.button2.Text = "&Color";
this.label1.Text = "Change my FONT and COLOR!";
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(24, 8);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click_1);
//
// button2
//
this.button2.Location = new System.Drawing.Point(120, 8);
this.button2.Name = "button2";
this.button2.TabIndex = 1;
this.button2.Text = "button2";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 48);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(280, 48);
this.label1.TabIndex = 2;
this.label1.Text = "label1";
//
// ColorFontDialog
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 101);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.label1,
this.button2,
this.button1});
this.Name = "ColorFontDialog";
this.Text = "ColorFontDialog";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new ColorFontDialog());
}
private void button2_Click(object sender, System.EventArgs e)
{
ColorDialog cd = new ColorDialog();
cd.AllowFullOpen = true; // allow custom colors
//cd.FullOpen = true; // shows custom colors automatically
cd.Color = Color.DarkBlue; // sets the custom color
//cd.Color = Color.Blue; // set the basic color
if(cd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
this.label1.ForeColor = cd.Color;
}
private void button1_Click_1(object sender, System.EventArgs e)
{
FontDialog fd = new FontDialog();
fd.ShowColor = true;
fd.Color = Color.Blue;
fd.ShowApply = true; // ColorDialog does not provide this option!!!
fd.Apply += new EventHandler(ApplyFont);
if(fd.ShowDialog() != System.Windows.Forms.DialogResult.Cancel)
ChangeFont(fd);
}
private void ApplyFont(object o, EventArgs ea)
{
ChangeFont((FontDialog)o);
}
private void ChangeFont(FontDialog fd)
{
this.label1.Font = fd.Font;
this.label1.ForeColor = fd.Color;
}
}
}
Font: GetHeight
using System;
using System.Drawing;
using System.Windows.Forms;
class FontMenuForm: Form
{
protected string strText = "Sample Text";
protected Font font = new Font("Times New Roman", 24, FontStyle.Italic);
public static void Main()
{
Application.Run(new FontMenuForm());
}
public FontMenuForm()
{
ResizeRedraw = true;
Menu = new MainMenu();
Menu.MenuItems.Add("&Font!", new EventHandler(MenuFontOnClick));
}
void MenuFontOnClick(object obj, EventArgs ea)
{
FontDialog dlg = new FontDialog();
dlg.Font = font;
if (dlg.ShowDialog() == DialogResult.OK)
{
font = dlg.Font;
Invalidate();
}
}
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)
{
SizeF sizef = grfx.MeasureString(strText, font);
Brush brush = new SolidBrush(clr);
grfx.DrawString(strText, font, brush, (cx - sizef.Width) / 2,
(cy - sizef.Height) / 2);
}
public float GetAscent(Graphics grfx, Font font)
{
return font.GetHeight(grfx) *
font.FontFamily.GetCellAscent(font.Style) /
font.FontFamily.GetLineSpacing(font.Style);
}
public float GetDescent(Graphics grfx, Font font)
{
return font.GetHeight(grfx) *
font.FontFamily.GetCellDescent(font.Style) /
font.FontFamily.GetLineSpacing(font.Style);
}
public float PointsToPageUnits(Graphics grfx, Font font)
{
float fFontSize;
if (grfx.PageUnit == GraphicsUnit.Display)
fFontSize = 100 * font.SizeInPoints / 72;
else
fFontSize = grfx.DpiX * font.SizeInPoints / 72;
return fFontSize;
}
}
Font list
/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa
Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
namespace FontList
{
/// <summary>
/// Summary description for FontListForm.
/// </summary>
public class FontListForm : System.Windows.Forms.Form
{
private System.Windows.Forms.ListBox listBox1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ruponentModel.Container components = null;
public FontListForm()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
InitForm ();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
FontFamily [] fonts;
protected void InitForm ()
{
fonts = FontFamily.Families;
foreach (FontFamily font in fonts)
listBox1.Items.Add (font.Name);
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.listBox1 = new System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// listBox1
//
this.listBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable;
this.listBox1.Location = new System.Drawing.Point(22, 21);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(226, 190);
this.listBox1.TabIndex = 0;
this.listBox1.DoubleClick += new System.EventHandler(this.listBox1_OnDoubleClick);
this.listBox1.MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(this.listBox1_OnMeasureItem);
this.listBox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.listBox1_OnDrawItem);
this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_OnSelChanged);
//
// FontListForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(266, 243);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.listBox1});
this.Name = "FontListForm";
this.Text = "Font Families";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new FontListForm());
}
private void listBox1_OnDrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
Rectangle rc = e.Bounds;
string strSample = "The quick red fox jumps over the lazy brown dog";
Font fntText;
Font fntSample;
int cy = 3 * (rc.Bottom - rc.Top) / 4;
try
{
fntText = new Font ("Times New Roman", 8, GraphicsUnit.Point);
}
catch (ArgumentException)
{
MessageBox.Show ("Cannot create Times New Roman font", "Pied font",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
try
{
fntText = new Font ("Times New Roman", 8, GraphicsUnit.Point);
fntSample = new Font (fonts[e.Index], 8, GraphicsUnit.Point);
}
catch (ArgumentException)
{
DialogResult mbResult = MessageBox.Show("Cannot open font " + fonts[e.Index].Name +"\nDo you want to delete this item from the list","Pied Font Error", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
switch (mbResult)
{
case DialogResult.Yes:
listBox1.Items.RemoveAt (e.Index);
return;
case DialogResult.No:
break;
}
strSample = "Cannot open font " + fonts[e.Index].Name;
fntSample = fntText;
}
Brush brush;
string str = fonts[e.Index].Name + " -- ";
if ((e.State & DrawItemState.Selected) != 0)
{
e.Graphics.FillRectangle (Brushes.DarkBlue, rc);
e.DrawFocusRectangle ();
brush = Brushes.White;
}
else
{
e.Graphics.FillRectangle (Brushes.White, rc);
brush = Brushes.Black;
}
e.Graphics.DrawString (str, fntText, brush, rc);
StringFormat format = new StringFormat ();
format.LineAlignment = StringAlignment.Center;
format.FormatFlags = StringFormatFlags.LineLimit;
SizeF size = e.Graphics.MeasureString (str, fntText);
rc = new Rectangle (rc.Left + (int) (size.Width + .5),
rc.Top, rc.Width, rc.Height);
e.Graphics.DrawString (strSample, fntSample, brush, rc, format);
fntText.Dispose ();
fntSample.Dispose();
}
private void listBox1_OnSelChanged(object sender, System.EventArgs e)
{
listBox1.Invalidate ();
}
private void listBox1_OnMeasureItem(object sender, System.Windows.Forms.MeasureItemEventArgs e)
{
MeasureItemEventArgs args = e;
Font plain = new Font ("Times New Roman", 10, GraphicsUnit.Point);
SizeF size = e.Graphics.MeasureString ("Test", plain);
args.ItemHeight = (int) (size.Height + .5);
plain.Dispose ();
}
private void listBox1_OnDoubleClick(object sender, System.EventArgs e)
{
if (e is ListChangedEventArgs)
{
frmSampleText stuff = new frmSampleText ();
}
}
}
/// <summary>
/// Summary description for frmSampleText.
/// </summary>
public class frmSampleText : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ruponentModel.Container components = null;
public frmSampleText()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(8, 8);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(272, 136);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "textBox1";
//
// frmSampleText
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 16);
this.ClientSize = new System.Drawing.Size(296, 157);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.textBox1});
this.Name = "frmSampleText";
this.Text = "Sample Text";
this.ResumeLayout(false);
}
#endregion
}
}
Font: SizeInPoints
using System;
using System.Drawing;
using System.Windows.Forms;
class HowdyWorld: Form
{
public static void Main()
{
Application.Run(new HowdyWorld());
}
public HowdyWorld()
{
ResizeRedraw = true;
MinimumSize = SystemInformation.MinimumWindowSize + new Size(0,1);
}
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)
{
Font font = new Font("Times New Roman", 10, FontStyle.Italic);
SizeF sizef = grfx.MeasureString(Text, font);
float fScale = Math.Min(cx / sizef.Width, cy / sizef.Height);
font = new Font(font.Name, fScale * font.SizeInPoints, font.Style);
sizef = grfx.MeasureString(Text, font);
grfx.DrawString(Text, font, new SolidBrush(clr),
(cx - sizef.Width ) / 2, (cy - sizef.Height) / 2);
}
}
Font with different size
using System;
using System.Drawing;
using System.Windows.Forms;
class TwentyFourPointPrinterFonts: Form
{
public static void Main()
{
Application.Run(new TwentyFourPointPrinterFonts());
}
public TwentyFourPointPrinterFonts()
{
Text = "Twenty-Four Point Printer Fonts";
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);
float y = 0;
Font font;
string strFamily = "Times New Roman";
font = new Font(strFamily, 24);
grfx.DrawString("No GraphicsUnit, 24 points", font, brush, 0, y);
y += font.GetHeight(grfx);
font = new Font(strFamily, 24, GraphicsUnit.Point);
grfx.DrawString("GraphicsUnit.Point, 24 units", font, brush, 0, y);
y += font.GetHeight(grfx);
font = new Font(strFamily, 1/ 3f, GraphicsUnit.Inch);
grfx.DrawString("GraphicsUnit.Inch, 1/3 units", font, brush, 0, y);
y += font.GetHeight(grfx);
font = new Font(strFamily, 25.4f / 3, GraphicsUnit.Millimeter);
grfx.DrawString("GraphicsUnit.Millimeter, 25.4/3 units",
font, brush, 0, y);
y += font.GetHeight(grfx);
font = new Font(strFamily, 100, GraphicsUnit.Document);
grfx.DrawString("GraphicsUnit.Document, 100 units",
font, brush, 0, y);
y += font.GetHeight(grfx);
font = new Font(strFamily, 100f / 3, GraphicsUnit.Pixel);
grfx.DrawString("GraphicsUnit.Pixel, " + 100f / 3 + " units",
font, brush, 0, y);
y += font.GetHeight(grfx);
font = new Font(strFamily, 100f / 3, GraphicsUnit.World);
grfx.DrawString("GraphicsUnit.World, " + 100f / 3 + " units",
font, brush, 0, y);
}
}
Get all system installed font
using System;
using System.Drawing;
using System.Drawing.Text;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class Test{
static void Main()
{
InstalledFontCollection fonts = new InstalledFontCollection();
for(int i = 0; i < fonts.Families.Length; i++)
{
Console.WriteLine(fonts.Families[i].Name);
}
}
}
Get the font in a FontDialog
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class MainClass {
private FontDialog fontDlg = new FontDialog();
private Font currFont = new Font("Times New Roman", 12);
public static void Main() {
FontDialog fontDlg = new FontDialog();
if (fontDlg.ShowDialog() != DialogResult.Cancel) {
Console.WriteLine(fontDlg.Font);
}
}
}
Load all system installed fonts
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
{
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.ruboBox lstFonts;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.StatusStrip statusBar;
private System.Windows.Forms.ToolStripStatusLabel statusLabel;
public Form1() {
InitializeComponent();
System.Drawing.Text.InstalledFontCollection fonts = new System.Drawing.Text.InstalledFontCollection();
foreach (FontFamily family in fonts.Families)
{
lstFonts.Items.Add(family.Name);
}
}
private void lstFonts_SelectedIndexChanged(object sender, EventArgs e)
{
this.Invalidate();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
if (lstFonts.SelectedIndex != -1)
{
e.Graphics.DrawString(lstFonts.Text, new Font(lstFonts.Text, 50), Brushes.Black, 10, 50);
statusBar.Items[0].Text = lstFonts.Text;
}
}
private void InitializeComponent()
{
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.lstFonts = new System.Windows.Forms.ruboBox();
this.label1 = new System.Windows.Forms.Label();
this.statusBar = new System.Windows.Forms.StatusStrip();
this.statusLabel = new System.Windows.Forms.ToolStripStatusLabel();
this.groupBox1.SuspendLayout();
this.statusBar.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.groupBox1.Controls.Add(this.lstFonts);
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Location = new System.Drawing.Point(7, 0);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(497, 40);
this.groupBox1.TabIndex = 1;
this.groupBox1.TabStop = false;
//
// lstFonts
//
this.lstFonts.DropDownStyle = System.Windows.Forms.ruboBoxStyle.DropDownList;
this.lstFonts.DropDownWidth = 340;
this.lstFonts.FormattingEnabled = true;
this.lstFonts.Location = new System.Drawing.Point(100, 12);
this.lstFonts.Name = "lstFonts";
this.lstFonts.Size = new System.Drawing.Size(340, 21);
this.lstFonts.TabIndex = 1;
this.lstFonts.SelectedIndexChanged += new System.EventHandler(this.lstFonts_SelectedIndexChanged);
//
// label1
//
this.label1.Location = new System.Drawing.Point(12, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(80, 12);
this.label1.TabIndex = 0;
this.label1.Text = "Choose Font:";
//
// statusBar
//
this.statusBar.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.statusLabel});
this.statusBar.LayoutStyle = System.Windows.Forms.ToolStripLayoutStyle.Table;
this.statusBar.Location = new System.Drawing.Point(0, 155);
this.statusBar.Name = "statusBar";
this.statusBar.Size = new System.Drawing.Size(516, 22);
this.statusBar.TabIndex = 2;
this.statusBar.Text = "statusStrip1";
//
// statusLabel
//
this.statusLabel.Name = "statusLabel";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(516, 177);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.statusBar);
this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.Text = "Font Viewer";
this.groupBox1.ResumeLayout(false);
this.statusBar.ResumeLayout(false);
this.ResumeLayout(false);
this.PerformLayout();
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}
}
new Font(fontRegular, FontStyle.Italic);
using System;
using System.Drawing;
using System.Windows.Forms;
class BoldAndItalic : Form {
public static void Main() {
Application.Run(new BoldAndItalic());
}
public BoldAndItalic() {
Text = "Bold and Italic Text";
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) {
float x = 0;
float y = 0;
grfx.DrawString("text", new Font(this.Font, FontStyle.Bold), new SolidBrush(Color.AliceBlue), x, y);
}
}
new Font(strFont, fSize)
using System;
using System.Drawing;
using System.Windows.Forms;
class FontSizes: Form
{
public static void Main()
{
Application.Run(new FontSizes());
}
public FontSizes()
{
Text = "Font Sizes";
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 strFont = "Times New Roman";
Brush brush = new SolidBrush(clr);
float y = 0;
for (float fSize = 6; fSize <= 12; fSize += 0.25f)
{
Font font = new Font(strFont, fSize);
grfx.DrawString(strFont + " in " + fSize + " points",
font, brush, 0, y);
}
}
}
new Font("Times New Roman", 10, FontStyle.Italic)
using System;
using System.Drawing;
using System.Windows.Forms;
class HowdyWorld: Form
{
public static void Main()
{
Application.Run(new HowdyWorld());
}
public HowdyWorld()
{
ResizeRedraw = true;
MinimumSize = SystemInformation.MinimumWindowSize + new Size(0,1);
}
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)
{
Font font = new Font("Times New Roman", 10, FontStyle.Italic);
SizeF sizef = grfx.MeasureString(Text, font);
float fScale = Math.Min(cx / sizef.Width, cy / sizef.Height);
grfx.DrawString(Text, font, new SolidBrush(clr),
(cx - sizef.Width ) / 2, (cy - sizef.Height) / 2);
}
}
System Fonts: Icon Title Font
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
public class Form1 : Form
{
private System.Windows.Forms.Label label2;
public Form1() {
this.Font = SystemFonts.IconTitleFont;
this.AutoScaleDimensions = new System.Drawing.SizeF(6.0F, 13.0F);
InitializeComponent();
SystemEvents.UserPreferenceChanged += new UserPreferenceChangedEventHandler(SystemEvents_UserPreferenceChanged);
}
private void SystemEvents_UserPreferenceChanged(object sender, UserPreferenceChangedEventArgs e)
{
if (e.Category == UserPreferenceCategory.Window)
{
this.Font = SystemFonts.IconTitleFont;
}
}
private void InitializeComponent()
{
this.label2 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label2
//
this.label2.Location = new System.Drawing.Point(12, 9);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(220, 55);
this.label2.TabIndex = 2;
this.label2.Text = "Try changing the Small Fonts/Large Fonts setting for th" +
"e computer.";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(244, 138);
this.Controls.Add(this.label2);
this.Text = "Form1";
this.ResumeLayout(false);
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}
}
Text on Baseline
using System;
using System.Drawing;
using System.Windows.Forms;
class TextOnBaseline: Form{
public static void Main() {
Application.Run(new TextOnBaseline());
}
public TextOnBaseline() {
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) {
float yBaseline = cy / 2;
Pen pen = new Pen(clr);
grfx.DrawLine(pen, 0, yBaseline, cx, yBaseline);
Font font = new Font("Times New Roman", 144);
float cyLineSpace = font.GetHeight(grfx);
int iCellSpace = font.FontFamily.GetLineSpacing(font.Style);
int iCellAscent = font.FontFamily.GetCellAscent(font.Style);
float cyAscent = cyLineSpace * iCellAscent / iCellSpace;
grfx.DrawString("Baseline", font, new SolidBrush(clr),
0, yBaseline - cyAscent);
}
}
Timer based animation for font
using System;
using System.Drawing;
using System.Drawing.Text;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class FontForm : System.Windows.Forms.Form
{
private Timer timer;
private int swellValue;
private string fontFace = "WingDings";
public FontForm()
{
InitializeComponent();
timer = new Timer();
Text = "Font App";
Width = 425;
Height = 150;
BackColor = Color.Honeydew;
CenterToScreen();
timer.Enabled = true;
timer.Interval = 100;
timer.Tick += new EventHandler(FontForm_OnTimer);
}
private void InitializeComponent()
{
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 253);
this.Text = "Form1";
this.Resize += new System.EventHandler(this.FontForm_Resize);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.FontForm_Paint);
}
static void Main()
{
Application.Run(new FontForm());
}
private void FontForm_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics;
Font theFont = new Font(fontFace, 12 + swellValue);
string message = "www.nfex.ru";
float windowCenter = this.DisplayRectangle.Width / 2;
SizeF stringSize = e.Graphics.MeasureString(message, theFont);
float startPos = windowCenter - (stringSize.Width / 2);
g.DrawString(message, theFont,
new SolidBrush(Color.Blue), startPos, 10);
}
private void FontForm_Resize(object sender, System.EventArgs e)
{
Rectangle myRect = new Rectangle(0, 100,
ClientRectangle.Width, ClientRectangle.Height);
Invalidate(myRect);
}
private void FontForm_OnTimer(object sender, EventArgs e)
{
swellValue += 5;
if(swellValue >= 50)
swellValue = 0;
Invalidate(new Rectangle(0, 0, ClientRectangle.Width, 100));
}
}