Csharp/CSharp Tutorial/GUI Windows Forms
Версия от 15:31, 26 мая 2010; (обсуждение)
- ActiveX
- Anchoring
- Binder
- Button
- CheckBox
- CheckedListBox
- Clipboard
- ColorDialog
- ComboBox
- ContextMenu Popup Menu
- Control
- Control Event
- Control Relation
- Cursors
- Custom Control
- DataBinding DataGrid
- DataBinding Label
- DataBinding ListBox
- DataBinding TextBox
- DataGridView
- DateTimePicker
- Dialog
- Dock
- DomainUpDown
- Double Buffering
- Drag Drop
- Drag Move
- ErrorProvider
- Event System
- EventArgs
- File Explore
- FloatLayoutPanel
- Form
- Form Event
- Form Paint
- Form Properties
- Generic Event
- HelpProvider
- ImageList
- Internationalization
- Introduction
- KeyEvent
- Label
- LinkLabel
- ListBox
- ListView
- ListViewItem
- MDI
- MaskedTextBox
- Menu
- MenuItem
- MessageBox
- MonthCalendar
- Mouse Event
- NotifyIcon
- NumericUpDown
- OpenFileDialog
- Password Field
- PictureBox
- Print Preview Dialog
- Print PrintDocument
- PrintDialog
- ProgressBar
- RadioButton
- Resource File
- Resx
- RichTextBox
- SaveFileDialog
- Screen capture
- ScrollBar
- Splitter
- StatusBar
- TabControl
- TableLayoutPanel
- TextBox
- Thread UI
- Timer
- ToolStripMenuItem
- Toolbar
- TrackBar
- TreeView
- Validation
- WebBrowser
- event
Содержание
- 1 Create a font from FontFamily
- 2 Create a font from font name and size
- 3 Font Constructor: derive a font
- 4 FontDialog Apply event
- 5 Font Dialog: Display and get selected font
- 6 FontDialog: set color, Apply event
- 7 Font list form
- 8 Font Properties
- 9 List all Font Families
- 10 RichTextBox: Multiline
- 11 Set the label font to a font selected from a FontDialog
- 12 Show Font Dialog Help
- 13 Use GraphicsUnit to create font
Create a font from FontFamily
using System;
using System.Drawing;
using System.Windows.Forms;
public class FontConstructor3 : Form
{
public FontConstructor3()
{
Text = "Font Constructor";
Size = new Size(350,200);
FontFamily ff = new FontFamily("Times New Roman");
Font fnt = new Font(ff, 12, FontStyle.Bold | FontStyle.Italic);
Font = fnt;
RichTextBox rtxt = new RichTextBox();
rtxt.Text = "first line.\n" +
"This is a second line of text.";
rtxt.Text += "\nFont Name:\t" + Font.Name;
rtxt.Text += "\nFont Family:\t" + Font.FontFamily;
rtxt.Text += "\nFont Styles:\t" + Font.Style;
rtxt.Text += "\nFont Size:\t" + Font.Size;
rtxt.Text += "\nFont Height:\t" + Font.Height;
rtxt.Text += "\nFont Units:\t" + Font.Unit;
rtxt.Multiline = true;
rtxt.Dock = DockStyle.Fill;
rtxt.Parent = this;
}
static void Main()
{
Application.Run(new FontConstructor3());
}
}
Create a font from font name and size
using System;
using System.Drawing;
using System.Windows.Forms;
public class FontConstructor2 : Form
{
public FontConstructor2()
{
Text = "Font Constructor";
Size = new Size(350,200);
Font fnt = new Font("Times New Roman", 10);
fnt = new Font(fnt, FontStyle.Bold | FontStyle.Italic);
Font = fnt;
RichTextBox rtxt = new RichTextBox();
rtxt.Text = "first line.\n" +
"This is a second line of text.";
rtxt.Text += "\nFont Name:\t" + Font.Name;
rtxt.Text += "\nFont Family:\t" + Font.FontFamily;
rtxt.Text += "\nFont Styles:\t" + Font.Style;
rtxt.Text += "\nFont Size:\t" + Font.Size;
rtxt.Text += "\nFont Height:\t" + Font.Height;
rtxt.Text += "\nFont Units:\t" + Font.Unit;
rtxt.Multiline = true;
rtxt.Dock = DockStyle.Fill;
rtxt.Parent = this;
}
static void Main()
{
Application.Run(new FontConstructor2());
}
}
Font Constructor: derive a font
using System;
using System.Drawing;
using System.Windows.Forms;
public class FontConstructor1 : Form
{
public FontConstructor1()
{
Size = new Size(350,200);
Font fnt = Font;
fnt = new Font(fnt, FontStyle.Bold | FontStyle.Italic);
Font = fnt;
RichTextBox rtxt = new RichTextBox();
rtxt.Text = "first line.\n" +
"This is a second line of text.";
rtxt.Text += "\nFont Name:\t" + Font.Name;
rtxt.Text += "\nFont Family:\t" + Font.FontFamily;
rtxt.Text += "\nFont Styles:\t" + Font.Style;
rtxt.Text += "\nFont Size:\t" + Font.Size;
rtxt.Text += "\nFont Height:\t" + Font.Height;
rtxt.Text += "\nFont Units:\t" + Font.Unit;
rtxt.Multiline = true;
rtxt.Dock = DockStyle.Fill;
rtxt.Parent = this;
}
static void Main()
{
Application.Run(new FontConstructor1());
}
}
FontDialog Apply event
using System;
using System.Drawing;
using System.Windows.Forms;
public class FontDialogFontApplyEvent : Form
{
private Button btnChange;
private Label lbl;
private FontDialog fd;
public FontDialogFontApplyEvent()
{
Size = new Size(350,200);
btnChange = new Button();
btnChange.Location = new Point(200,50);
btnChange.Size = new Size(100,23);
btnChange.Text = "Change";
btnChange.Click += new System.EventHandler(btnChange_Click);
btnChange.Parent = this;
lbl = new Label();
lbl.Text = "test";
lbl.AutoSize = true;
lbl.Parent = this;
}
static void Main()
{
Application.Run(new FontDialogFontApplyEvent());
}
private void btnChange_Click(object sender, EventArgs e)
{
fd = new FontDialog();
fd.ShowHelp = false;
fd.ShowApply = true;
fd.Apply += new System.EventHandler(this.fd_Apply);
if (fd.ShowDialog() == DialogResult.OK)
lbl.Font = fd.Font;
}
private void fd_Apply(object sender, System.EventArgs e)
{
lbl.Font = fd.Font;
}
}
Font Dialog: Display and get selected font
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class FontDialogDisplay : System.Windows.Forms.Form
{
private System.ruponentModel.Container components = null;
private System.Windows.Forms.FontDialog fontDlg = new System.Windows.Forms.FontDialog();
private Font currFont = new Font("Times New Roman", 12);
public FontDialogDisplay()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
private void InitializeComponent()
{
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.FontDialogDisplay_MouseUp);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.FontDialogDisplay_Paint);
}
[STAThread]
static void Main()
{
Application.Run(new FontDialogDisplay());
}
private void FontDialogDisplay_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawString("Testing...", currFont, new SolidBrush(Color.Black), 0, 0);
}
private void FontDialogDisplay_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (fontDlg.ShowDialog() != DialogResult.Cancel)
{
currFont = fontDlg.Font;
Invalidate();
}
}
}
FontDialog: set color, Apply event
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class Form1 : System.Windows.Forms.Form {
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Label label1;
public Form1() {
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";
//
// Form1
//
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.ResumeLayout(false);
this.Text = "Font and Color Dialogs";
this.button1.Text = "&Font";
this.button2.Text = "&Color";
this.label1.Text = "Change my FONT and COLOR!";
}
[STAThread]
static void Main() {
Application.Run(new Form1());
}
private void button2_Click(object sender, System.EventArgs e) {
ColorDialog cd = new ColorDialog();
cd.AllowFullOpen = true;
cd.FullOpen = true;
cd.Color = Color.DarkBlue;
//cd.Color = Color.Blue;
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;
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 list form
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Text;
using System.Collections.Generic;
public class FontListForm : Form
{
public FontListForm()
{
InitializeComponent();
}
private void FontListForm_Load(object sender, EventArgs e)
{
using (InstalledFontCollection fontFamilies = new InstalledFontCollection())
{
int offset = 10;
foreach (FontFamily family in fontFamilies.Families)
{
try
{
Label fontLabel = new Label();
fontLabel.Text = family.Name;
fontLabel.Font = new Font(family, 14);
fontLabel.Left = 10;
fontLabel.Width = pnlFonts.Width;
fontLabel.Top = offset;
pnlFonts.Controls.Add(fontLabel);
offset += 30;
}catch{}
}
}
}
private System.ruponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
this.pnlFonts = new System.Windows.Forms.Panel();
this.SuspendLayout();
this.pnlFonts.AutoScroll = true;
this.pnlFonts.Dock = System.Windows.Forms.DockStyle.Fill;
this.pnlFonts.Location = new System.Drawing.Point(0, 0);
this.pnlFonts.Name = "pnlFonts";
this.pnlFonts.Size = new System.Drawing.Size(299, 276);
this.pnlFonts.TabIndex = 0;
this.ClientSize = new System.Drawing.Size(299, 276);
this.Controls.Add(this.pnlFonts);
this.Name = "FontListForm";
this.Text = "List of Installed Fonts";
this.Load += new System.EventHandler(this.FontListForm_Load);
this.ResumeLayout(false);
}
private System.Windows.Forms.Panel pnlFonts;
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new FontListForm());
}
}
Font Properties
using System;
using System.Drawing;
using System.Windows.Forms;
public class FontProperties : Form
{
private Button btnChange;
private Label lbl;
private FontDialog fd;
public FontProperties()
{
Size = new Size(350,200);
btnChange = new Button();
btnChange.Location = new Point(200,50);
btnChange.Size = new Size(100,23);
btnChange.Text = "Change";
btnChange.Click += new System.EventHandler(btnChange_Click);
btnChange.Parent = this;
lbl = new Label();
lbl.Text = "test";
lbl.AutoSize = true;
lbl.Parent = this;
}
static void Main()
{
Application.Run(new FontProperties());
}
private void btnChange_Click(object sender, EventArgs e)
{
fd = new FontDialog();
fd.ShowHelp = false;
fd.ShowApply = true;
fd.Apply += new System.EventHandler(this.fd_Apply);
if (fd.ShowDialog() == DialogResult.OK)
lbl.Font = fd.Font;
MessageBox.Show("Label Font:\t" + lbl.Font.ToString() + "\n" +
"Label Font Family:\t" + lbl.Font.FontFamily.ToString() + "\n" +
"Label Font Style:\t" + lbl.Font.Style.ToString() + "\n" +
"Label Font Unit:\t" + lbl.Font.Unit.ToString() + "\n" +
"Label Font Height:\t" + lbl.Font.Height.ToString() + "\n",
"Font Properties");
}
private void fd_Apply(object sender, System.EventArgs e)
{
lbl.Font = fd.Font;
}
}
List all Font Families
using System;
using System.Drawing;
using System.Windows.Forms;
public class FontFamilies : Form
{
public FontFamilies()
{
Size = new Size(350,200);
RichTextBox rtxt = new RichTextBox();
rtxt.Multiline = true;
rtxt.Dock = DockStyle.Fill;
rtxt.Parent = this;
FontFamily[] ffArray = FontFamily.Families;
foreach( FontFamily ff in ffArray )
{
if (ff.IsStyleAvailable(FontStyle.Regular))
{
rtxt.Text += ff.Name + "\n";
}
}
}
static void Main()
{
Application.Run(new FontFamilies());
}
}
RichTextBox: Multiline
using System;
using System.Drawing;
using System.Windows.Forms;
public class FontConstructor1 : Form
{
public FontConstructor1()
{
Size = new Size(350,200);
Font fnt = Font;
fnt = new Font(fnt, FontStyle.Bold | FontStyle.Italic);
Font = fnt;
RichTextBox rtxt = new RichTextBox();
rtxt.Text = "first line.\n" +
"This is a second line of text.";
rtxt.Text += "\nFont Name:\t" + Font.Name;
rtxt.Text += "\nFont Family:\t" + Font.FontFamily;
rtxt.Text += "\nFont Styles:\t" + Font.Style;
rtxt.Text += "\nFont Size:\t" + Font.Size;
rtxt.Text += "\nFont Height:\t" + Font.Height;
rtxt.Text += "\nFont Units:\t" + Font.Unit;
rtxt.Multiline = true;
rtxt.Dock = DockStyle.Fill;
rtxt.Parent = this;
}
static void Main()
{
Application.Run(new FontConstructor1());
}
}
Set the label font to a font selected from a FontDialog
using System;
using System.ruponentModel;
using System.Drawing;
using System.Windows.Forms;
public class FontPickerDemo : Form
{
Button b;
Label l;
void OnApply(Object sender,System.EventArgs e)
{
FontDialog dlg = (FontDialog)sender;
l.Font=dlg.Font;
}
void OnClickedb(Object sender,EventArgs e)
{
FontDialog dlg=new FontDialog();
dlg.Font = l.Font;
dlg.ShowApply = true;
dlg.Apply += new EventHandler(OnApply);
if(dlg.ShowDialog() != DialogResult.Cancel)
{
l.Font = dlg.Font;
}
}
public FontPickerDemo()
{
this.Size=new Size(416,320);
l=new Label();
l.Location = new Point(8,8);
l.Size = new Size(400,200);
l.Text = "nabcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ";
l.Font = new Font("Microsoft Sans Serif", 18f);
this.Controls.Add(l);
b=new Button();
b.Text = "Choose Font";
b.Click += new EventHandler(OnClickedb);
b.Location = new Point(8,250);
b.Size = new Size(400,32);
this.Controls.Add(b);
}
static void Main()
{
Application.Run(new FontPickerDemo());
}
}
Show Font Dialog Help
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class FontDialogDisplayHelp : System.Windows.Forms.Form
{
private System.ruponentModel.Container components = null;
private System.Windows.Forms.FontDialog fontDlg = new System.Windows.Forms.FontDialog();
private Font currFont = new Font("Times New Roman", 12);
public FontDialogDisplayHelp()
{
InitializeComponent();
fontDlg.ShowHelp = true;
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
private void InitializeComponent()
{
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.FontDialogDisplayHelp_MouseUp);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.FontDialogDisplayHelp_Paint);
}
[STAThread]
static void Main()
{
Application.Run(new FontDialogDisplayHelp());
}
private void FontDialogDisplayHelp_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawString("Testing...", currFont, new SolidBrush(Color.Black), 0, 0);
}
private void FontDialogDisplayHelp_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (fontDlg.ShowDialog() != DialogResult.Cancel)
{
currFont = fontDlg.Font;
Invalidate();
}
}
}
Use GraphicsUnit to create font
using System;
using System.Drawing;
using System.Windows.Forms;
public class FontConstructor4 : Form
{
public FontConstructor4()
{
Size = new Size(350,200);
FontFamily ff = new FontFamily("Times New Roman");
Font fnt = new Font(ff, .25f, GraphicsUnit.Inch);
Font = fnt;
RichTextBox rtxt = new RichTextBox();
rtxt.Text = "first line.\n" +
"This is a second line of text.";
rtxt.Text += "\nFont Name:\t" + Font.Name;
rtxt.Text += "\nFont Family:\t" + Font.FontFamily;
rtxt.Text += "\nFont Styles:\t" + Font.Style;
rtxt.Text += "\nFont Size:\t" + Font.Size;
rtxt.Text += "\nFont Height:\t" + Font.Height;
rtxt.Text += "\nFont Units:\t" + Font.Unit;
rtxt.Multiline = true;
rtxt.Dock = DockStyle.Fill;
rtxt.Parent = this;
}
static void Main()
{
Application.Run(new FontConstructor4());
}
}