字体选择器

来源:互联网 发布:有哪些高仿的淘宝店 编辑:程序博客网 时间:2024/04/28 01:30

 

先看一下效果:

第一部分:Form1.cs

 

view plaincopy to clipboardprint?
  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.ComponentModel;   
  4. using System.Data;   
  5. using System.Drawing;   
  6. using System.Linq;   
  7. using System.Text;   
  8. using System.Windows.Forms;   
  9.   
  10. namespace WindowsFormsApplication1   
  11. {   
  12.     public partial class Form1 : Form   
  13.     {   
  14.         public Form1()   
  15.         {   
  16.             InitializeComponent();   
  17.         }   
  18.   
  19.         private void Form1_Load(object sender, EventArgs e)   
  20.         {   
  21.   
  22.             comboBox1.DrawItem += new DrawItemEventHandler(comboBox1_DrawItem);   
  23.             System.Drawing.Text.InstalledFontCollection ifc = new System.Drawing.Text.InstalledFontCollection();   
  24.             FontFamily[] ffs = ifc.Families;   
  25.             foreach (FontFamily ff in ffs)   
  26.                 //在这里的样式里表示Regular可能使用的字体   
  27.                 if (ff.IsStyleAvailable(FontStyle.Regular))   
  28.                     comboBox1.Items.Add(ff.Name);   
  29.                
  30.   
  31.   
  32.   
  33.   
  34.         }   
  35.   
  36.         private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)   
  37.         {   
  38.             e.DrawBackground();   
  39.             ComboBox cmb = (ComboBox)sender;   
  40.             string txt = e.Index > -1 ? cmb.Items[e.Index].ToString() : cmb.Text;   
  41.             Font f = new Font(txt, cmb.Font.Size);   
  42.             //使用格式刷   
  43.             Brush b = new SolidBrush(e.ForeColor);   
  44.             //字符串描绘   
  45.             float ym =   
  46.                 (e.Bounds.Height - e.Graphics.MeasureString(txt, f).Height) / 2;   
  47.             e.Graphics.DrawString(txt, f, b, e.Bounds.X, e.Bounds.Y + ym);   
  48.   
  49.             f.Dispose();   
  50.             b.Dispose();   
  51.   
  52.             //描绘四角表示焦点的形状   
  53.             e.DrawFocusRectangle();   
  54.   
  55.   
  56.         }   
  57. //combobox改变选择事件   
  58.         private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)   
  59.         {   
  60. //            if (comboBox1.SelectedIndex == -1)   
  61. //                fontname = comboBox1.Items[32].ToString();//未被选择时设置默认,如果下面fontname设置了默认值,这里的if,else就没有必要要了。   
  62. //            else   
  63.                 fontname = comboBox1.Text;   
  64.             textBox1.Font = new Font(fontname,fontsize);   
  65.         }   
  66. //listbox选择事件   
  67.         private void listBox1_SelectedIndexChanged(object sender, EventArgs e)   
  68.         {   
  69.   
  70.  //           if (listBox1.SelectedIndex == -1)   
  71.  //               fontsize = float.Parse(listBox1.Items[1].ToString());//未被选择时默认为第一个,如果下面fontsize设置了默认值,这里的if,else就没有必要要了。   
  72.  //           else   
  73.                 fontsize = float.Parse(listBox1.SelectedItem.ToString());   
  74.             textBox1.Font = new Font(fontname, fontsize);   
  75.         }   
  76. //设定fontname,fontsize的默认值   
  77.         public string fontname = "宋体";   
  78.         public float fontsize = 9;   
  79. //按钮1复制_事件   
  80.         private void button1_Click(object sender, EventArgs e)   
  81.         {   
  82.             if (textBox1.SelectionLength > 0)   
  83.                 textBox1.Copy();   
  84.         }   
  85. //按钮2粘贴_事件   
  86.         private void button2_Click(object sender, EventArgs e)   
  87.         {   
  88.             textBox1.Paste();   
  89.         }   
  90. //linklabel的点击事件   
  91.         private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)   
  92.         {   
  93.             linkLabel1.Links[linkLabel1.Links.IndexOf(e.Link)].Visited = true;   
  94.             string targetUrl = e.Link.LinkData as string;   
  95.             if (string.IsNullOrEmpty(targetUrl))   
  96.                 MessageBox.Show("没有链接URL!");   
  97.             else  
  98.                 Help.ShowHelp(this, targetUrl);   
  99.   
  100.         }   
  101.     }   
  102. }  

 

第二部分:Form1.designer.cs

 

view plaincopy to clipboardprint?
  1. namespace WindowsFormsApplication1   
  2. {   
  3.     partial class Form1   
  4.     {   
  5.         /// <summary>   
  6.         /// Required designer variable.   
  7.         /// </summary>   
  8.         private System.ComponentModel.IContainer components = null;   
  9.   
  10.         /// <summary>   
  11.         /// Clean up any resources being used.   
  12.         /// </summary>   
  13.         /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>   
  14.         protected override void Dispose(bool disposing)   
  15.         {   
  16.             if (disposing && (components != null))   
  17.             {   
  18.                 components.Dispose();   
  19.             }   
  20.             base.Dispose(disposing);   
  21.         }  
  22.  
  23.         #region Windows Form Designer generated code   
  24.   
  25.         /// <summary>   
  26.         /// Required method for Designer support - do not modify   
  27.         /// the contents of this method with the code editor.   
  28.         /// </summary>   
  29.         private void InitializeComponent()   
  30.         {   
  31.             this.label1 = new System.Windows.Forms.Label();   
  32.             this.label2 = new System.Windows.Forms.Label();   
  33.             this.listBox1 = new System.Windows.Forms.ListBox();   
  34.             this.comboBox1 = new System.Windows.Forms.ComboBox();   
  35.             this.button1 = new System.Windows.Forms.Button();   
  36.             this.button2 = new System.Windows.Forms.Button();   
  37.             this.textBox1 = new System.Windows.Forms.TextBox();   
  38.             this.linkLabel1 = new System.Windows.Forms.LinkLabel();   
  39.             this.SuspendLayout();   
  40.             //    
  41.             // label1   
  42.             //    
  43.             this.label1.AutoSize = true;   
  44.             this.label1.Location = new System.Drawing.Point(24, 16);   
  45.             this.label1.Name = "label1";   
  46.             this.label1.Size = new System.Drawing.Size(29, 12);   
  47.             this.label1.TabIndex = 0;   
  48.             this.label1.Text = "字体";   
  49.             //    
  50.             // label2   
  51.             //    
  52.             this.label2.AutoSize = true;   
  53.             this.label2.Location = new System.Drawing.Point(508, 67);   
  54.             this.label2.Name = "label2";   
  55.             this.label2.Size = new System.Drawing.Size(29, 12);   
  56.             this.label2.TabIndex = 2;   
  57.             this.label2.Text = "字号";   
  58.             //    
  59.             // listBox1   
  60.             //    
  61.             this.listBox1.FormattingEnabled = true;   
  62.             this.listBox1.ItemHeight = 12;   
  63.             this.listBox1.Items.AddRange(new object[] {   
  64.             "9",   
  65.             "10",   
  66.             "12",   
  67.             "16",   
  68.             "20",   
  69.             "28"});   
  70.             this.listBox1.Location = new System.Drawing.Point(507, 95);   
  71.             this.listBox1.Name = "listBox1";   
  72.             this.listBox1.Size = new System.Drawing.Size(54, 100);   
  73.             this.listBox1.TabIndex = 3;   
  74.             this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);   
  75.             //    
  76.             // comboBox1   
  77.             //    
  78.             this.comboBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable;   
  79.             this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;   
  80.             this.comboBox1.FormattingEnabled = true;   
  81.             this.comboBox1.Location = new System.Drawing.Point(63, 9);   
  82.             this.comboBox1.Name = "comboBox1";   
  83.             this.comboBox1.Size = new System.Drawing.Size(133, 22);   
  84.             this.comboBox1.TabIndex = 4;   
  85.             this.comboBox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.comboBox1_DrawItem);   
  86.             this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);   
  87.             //    
  88.             // button1   
  89.             //    
  90.             this.button1.Location = new System.Drawing.Point(26, 54);   
  91.             this.button1.Name = "button1";   
  92.             this.button1.Size = new System.Drawing.Size(66, 25);   
  93.             this.button1.TabIndex = 5;   
  94.             this.button1.Text = "复制";   
  95.             this.button1.UseVisualStyleBackColor = true;   
  96.             this.button1.Click += new System.EventHandler(this.button1_Click);   
  97.             //    
  98.             // button2   
  99.             //    
  100.             this.button2.Location = new System.Drawing.Point(130, 54);   
  101.             this.button2.Name = "button2";   
  102.             this.button2.Size = new System.Drawing.Size(66, 25);   
  103.             this.button2.TabIndex = 6;   
  104.             this.button2.Text = "粘贴";   
  105.             this.button2.UseVisualStyleBackColor = true;   
  106.             this.button2.Click += new System.EventHandler(this.button2_Click);   
  107.             //    
  108.             // textBox1   
  109.             //    
  110.             this.textBox1.Location = new System.Drawing.Point(26, 95);   
  111.             this.textBox1.Multiline = true;   
  112.             this.textBox1.Name = "textBox1";   
  113.             this.textBox1.Size = new System.Drawing.Size(442, 201);   
  114.             this.textBox1.TabIndex = 7;   
  115.             //    
  116.             // linkLabel1   
  117.             //    
  118.             this.linkLabel1.AutoSize = true;   
  119.   
  120.             this.linkLabel1.LinkBehavior = System.Windows.Forms.LinkBehavior.HoverUnderline;   
  121.             this.linkLabel1.Location = new System.Drawing.Point(53, 307);   
  122.             this.linkLabel1.Name = "linkLabel1";   
  123.             this.linkLabel1.Size = new System.Drawing.Size(79, 19);   
  124.             this.linkLabel1.TabIndex = 8;   
  125.             this.linkLabel1.TabStop = true;   
  126.             this.linkLabel1.Text = "一路向东 新浪网 百度 谷歌 YAHOO";   
  127.             this.linkLabel1.UseCompatibleTextRendering = true;   
  128.             this.linkLabel1.Links.Add(0, 4, "http://blog.csdn.net/imbiz/");   
  129.             this.linkLabel1.Links.Add(9, 2, "http://www.baidu.com/");   
  130.             this.linkLabel1.Links.Add(12, 2, "http://www.google.cn/");   
  131.             this.linkLabel1.Links.Add(15, 5, "");   
  132. //以上代码使用links.add方法对一个label添加多个链接,第一,二个参数分别是Text的起始,终点位置   
  133.                
  134.             this.linkLabel1.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel1_LinkClicked);   
  135.             //    
  136.             // Form1   
  137.             //    
  138.             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);   
  139.             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;   
  140.             this.ClientSize = new System.Drawing.Size(639, 340);   
  141.             this.Controls.Add(this.linkLabel1);   
  142.             this.Controls.Add(this.textBox1);   
  143.             this.Controls.Add(this.button2);   
  144.             this.Controls.Add(this.button1);   
  145.             this.Controls.Add(this.comboBox1);   
  146.             this.Controls.Add(this.listBox1);   
  147.             this.Controls.Add(this.label2);   
  148.             this.Controls.Add(this.label1);   
  149.             this.Name = "Form1";   
  150.             this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;   
  151.             this.Text = "Form1";   
  152.             this.Load += new System.EventHandler(this.Form1_Load);   
  153.             this.ResumeLayout(false);   
  154.             this.PerformLayout();   
  155.   
  156.         }  
  157.  
  158.           
  159.  
  160.         #endregion   
  161.   
  162.         private System.Windows.Forms.Label label1;   
  163.         private System.Windows.Forms.Label label2;   
  164.         private System.Windows.Forms.ListBox listBox1;   
  165.         private System.Windows.Forms.ComboBox comboBox1;   
  166.         private System.Windows.Forms.Button button1;   
  167.         private System.Windows.Forms.Button button2;   
  168.         private System.Windows.Forms.TextBox textBox1;   
  169.         private System.Windows.Forms.LinkLabel linkLabel1;   
  170.   
  171.   
  172.   
  173.            
  174.   
  175.           
  176.     }   
  177. }  

 

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

 

 

 

 

 

原创粉丝点击