您的位置:首页 > 编程语言 > C#

C# 训练场(三)WinForm 练习:字体选择器,文本框,按钮,超链接

2010-06-03 01:15 435 查看
先看一下效果:



第一部分:Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

comboBox1.DrawItem += new DrawItemEventHandler(comboBox1_DrawItem);
System.Drawing.Text.InstalledFontCollection ifc = new System.Drawing.Text.InstalledFontCollection();
FontFamily[] ffs = ifc.Families;
foreach (FontFamily ff in ffs)
//在这里的样式里表示Regular可能使用的字体
if (ff.IsStyleAvailable(FontStyle.Regular))
comboBox1.Items.Add(ff.Name);

}

private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
ComboBox cmb = (ComboBox)sender;
string txt = e.Index > -1 ? cmb.Items[e.Index].ToString() : cmb.Text;
Font f = new Font(txt, cmb.Font.Size);
//使用格式刷
Brush b = new SolidBrush(e.ForeColor);
//字符串描绘
float ym =
(e.Bounds.Height - e.Graphics.MeasureString(txt, f).Height) / 2;
e.Graphics.DrawString(txt, f, b, e.Bounds.X, e.Bounds.Y + ym);

f.Dispose();
b.Dispose();

//描绘四角表示焦点的形状
e.DrawFocusRectangle();

}
//combobox改变选择事件
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
//            if (comboBox1.SelectedIndex == -1)
//                fontname = comboBox1.Items[32].ToString();//未被选择时设置默认,如果下面fontname设置了默认值,这里的if,else就没有必要要了。
//            else
fontname = comboBox1.Text;
textBox1.Font = new Font(fontname,fontsize);
}
//listbox选择事件
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{

//           if (listBox1.SelectedIndex == -1)
//               fontsize = float.Parse(listBox1.Items[1].ToString());//未被选择时默认为第一个,如果下面fontsize设置了默认值,这里的if,else就没有必要要了。
//           else
fontsize = float.Parse(listBox1.SelectedItem.ToString());
textBox1.Font = new Font(fontname, fontsize);
}
//设定fontname,fontsize的默认值
public string fontname = "宋体";
public float fontsize = 9;
//按钮1复制_事件
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.SelectionLength > 0)
textBox1.Copy();
}
//按钮2粘贴_事件
private void button2_Click(object sender, EventArgs e)
{
textBox1.Paste();
}
//linklabel的点击事件
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
linkLabel1.Links[linkLabel1.Links.IndexOf(e.Link)].Visited = true;
string targetUrl = e.Link.LinkData as string;
if (string.IsNullOrEmpty(targetUrl))
MessageBox.Show("没有链接URL!");
else
Help.ShowHelp(this, targetUrl);

}
}
}


第二部分:Form1.designer.cs

namespace WindowsFormsApplication1
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (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.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.listBox1 = new System.Windows.Forms.ListBox();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.linkLabel1 = new System.Windows.Forms.LinkLabel();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(24, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(29, 12);
this.label1.TabIndex = 0;
this.label1.Text = "字体";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(508, 67);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(29, 12);
this.label2.TabIndex = 2;
this.label2.Text = "字号";
//
// listBox1
//
this.listBox1.FormattingEnabled = true;
this.listBox1.ItemHeight = 12;
this.listBox1.Items.AddRange(new object[] {
"9",
"10",
"12",
"16",
"20",
"28"});
this.listBox1.Location = new System.Drawing.Point(507, 95);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(54, 100);
this.listBox1.TabIndex = 3;
this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
//
// comboBox1
//
this.comboBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable;
this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBox1.FormattingEnabled = true;
this.comboBox1.Location = new System.Drawing.Point(63, 9);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(133, 22);
this.comboBox1.TabIndex = 4;
this.comboBox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.comboBox1_DrawItem);
this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
//
// button1
//
this.button1.Location = new System.Drawing.Point(26, 54);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(66, 25);
this.button1.TabIndex = 5;
this.button1.Text = "复制";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(130, 54);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(66, 25);
this.button2.TabIndex = 6;
this.button2.Text = "粘贴";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(26, 95);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(442, 201);
this.textBox1.TabIndex = 7;
//
// linkLabel1
//
this.linkLabel1.AutoSize = true;

this.linkLabel1.LinkBehavior = System.Windows.Forms.LinkBehavior.HoverUnderline;
this.linkLabel1.Location = new System.Drawing.Point(53, 307);
this.linkLabel1.Name = "linkLabel1";
this.linkLabel1.Size = new System.Drawing.Size(79, 19);
this.linkLabel1.TabIndex = 8;
this.linkLabel1.TabStop = true;
this.linkLabel1.Text = "一路向东 新浪网 百度 谷歌 YAHOO";
this.linkLabel1.UseCompatibleTextRendering = true;
this.linkLabel1.Links.Add(0, 4, "http://blog.csdn.net/imbiz/");
this.linkLabel1.Links.Add(9, 2, "http://www.baidu.com/");
this.linkLabel1.Links.Add(12, 2, "http://www.google.cn/");
this.linkLabel1.Links.Add(15, 5, "");
//以上代码使用links.add方法对一个label添加多个链接,第一,二个参数分别是Text的起始,终点位置

this.linkLabel1.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel1_LinkClicked);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(639, 340);
this.Controls.Add(this.linkLabel1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.comboBox1);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.LinkLabel linkLabel1;

}
}


程序入口:program.cs (系统自动)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐