zoukankan      html  css  js  c++  java
  • FontCombobox 和FontSizeCombobox

    附件:http://files.cnblogs.com/xe2011/WindowsFormsFontCombox.rar

    1. 自定义组件字体组合框
    2. 自定义组件字体组合框如何使用
    3. 自定义组件字体大小组合框
    4. 自定义组件字体大小组合框如何使用
    5. 如何设置richTextBox1选中的字体名称
    6. 如何获得richTextBox1选中的字体名称
    7. 如何设置richTextBox1选中的字体大小
    8. 如何获得richTextBox1选中的字体大小
    9. 如何在toolStrip中添加这2个控件

    自定义组件的做法

    1 新个新的工程,先做一个想要达到效果的样子来。

    2 然后转到 InitializeComponent(); 把相关代码复制过来

    3 选中工程添加一个类然后继承一个组件的类 如 class FontComboBox : ComboBox{}

    4 小修改一下 基本完成了

    自定义组件字体组合框类

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Drawing;
    
    namespace System.Windows.Forms
    {
        class FontComboBox : ComboBox
        {
            public FontComboBox()
            {
                this.comboBox1 = this;
                this.comboBox1.FormattingEnabled = true;
                this.comboBox1.Location = new System.Drawing.Point(12, 12);
                this.comboBox1.Name = "comboBox1";
                this.comboBox1.Size = new System.Drawing.Size(121, 20);
                this.comboBox1.TabIndex = 1;
    
                //OwnerDrawVariable
                this.comboBox1.DrawMode = DrawMode.OwnerDrawVariable;
                this.comboBox1.MaxDropDownItems = 20;
                this.comboBox1.DropDownWidth = 200;
    
    
    
    
                this.comboBox1.Text = "Times New Roman";
                this.comboBox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.comboBox1_DrawItem);
                this.comboBox1.MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(this.comboBox1_MeasureItem);
            }
    
            //这么写原因
            //1 comboBox1.Items初始化在Form1.Designer.cs产生了大量的代码 多出165行代码 我的系统上有165种字体
            //        
            //private void Form1_Load(object sender, EventArgs e)
            //{
            //    fontComboBox1.Initialize();
            //}
            public void Initialize()
            {
                this.comboBox1.Items.Clear();
                foreach (FontFamily f in FontFamily.Families)
                {
                    comboBox1.Items.Add(f.Name);
                }
            }
    
    
            private System.Windows.Forms.ComboBox comboBox1;
    
            private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
            {
                e.DrawBackground();
                //e.DrawFocusRectangle();
                string s = comboBox1.Items[e.Index].ToString();
    
                string fontName = comboBox1.Items[e.Index].ToString();
                Font font = new Font(fontName, 12);
    
                e.Graphics.DrawString(s, font, Brushes.Black, e.Bounds);
            }
    
            private void comboBox1_MeasureItem(object sender, MeasureItemEventArgs e)
            {
                e.ItemHeight = 20;
            }
        }
    }
    FontComboBox.cs

    初始化下就可以使用了

       private void Form1_Load(object sender, EventArgs e)
            {
                fontComboBox1.Initialize();
            }

    自定义组件字体大小组合框类

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Drawing;
    
    namespace System.Windows.Forms
    {
        class FontSizeComboBox : ComboBox
        {
            public FontSizeComboBox()
            {
                this.comboBox1 = this;
                // 
                // comboBox1
                // 
                this.comboBox1.FormattingEnabled = true;
                this.comboBox1.Location = new System.Drawing.Point(12, 12);
                this.comboBox1.Name = "comboBox1";
                this.comboBox1.Size = new System.Drawing.Size(121, 20);
                this.comboBox1.TabIndex = 1;
                //this.comboBox1.Sorted = true;
    
                //OwnerDrawVariable
                this.comboBox1.DrawMode = DrawMode.OwnerDrawVariable;
                this.comboBox1.MaxDropDownItems = 15;
                this.comboBox1.DropDownWidth = 200;
                this.comboBox1.Text = "9";                   
                this.comboBox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.comboBox1_DrawItem);
                this.comboBox1.MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(this.comboBox1_MeasureItem);
            }
    
            //这么写原因有2
            //1 comboBox1.Items的赋值了2次
            //2 comboBox1.Items初始化在Form1.Designer.cs产生了大量的代码
            //private void Form1_Load(object sender, EventArgs e)
            //{
            //    fontSizeComboBox1.Initialize();
            //}
            public void Initialize()
            {
                this.comboBox1.Items.Clear();
                this.comboBox1.Items.AddRange(new string[] {
                "8",
                "9",
                "10",
                "11",
                "12",
                "14",
                "16",
                "18",
                "20",
                "22",
                "24",
                "26",
                "28",
                "36",
                "48",
                "72"});
            }
    
            private System.Windows.Forms.ComboBox comboBox1;
    
            private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
            {
                e.DrawBackground();
                //e.DrawFocusRectangle();
    
                string s = comboBox1.Items[e.Index].ToString();
                int fontSize = Convert.ToInt32(comboBox1.Items[e.Index].ToString());
                Font font = new Font("Times New Roman", fontSize, FontStyle.Bold);
    
                e.Graphics.DrawString(s, font, Brushes.Black, e.Bounds);
            }
    
            private void comboBox1_MeasureItem(object sender, MeasureItemEventArgs e)
            {
                e.ItemHeight = Convert.ToInt32(comboBox1.Items[e.Index].ToString()) + 12;
            }
    
        }
    }
    FontSizeComboBox.cs

    初始化下就可以使用了

       private void Form1_Load(object sender, EventArgs e)
            {
                fontSizeComboBox1.Initialize();
            }

    设置richTextBox1选中的字体名称

            private void fontComboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                float fontSize;
                try
                {
                    fontSize = richTextBox1.SelectionFont.Size;
                }
                catch
                {
                    fontSize = richTextBox1.Font.Size;
                }
                richTextBox1.SelectionFont = new Font(fontComboBox1.Text, fontSize);
            }
    View Code

    获得richTextBox1选中的字体名称

     private void richTextBox1_SelectionChanged(object sender, EventArgs e)
            {
                if (richTextBox1.SelectionFont == null)
                    return;
    
                fontComboBox1.Text = richTextBox1.SelectionFont.Name.ToString();
            }
    View Code

    如何设置richTextBox1选中的字体大小

            private void fontSizeComboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                string fontName;
                try
                {
                    fontName = richTextBox1.SelectionFont.Name;
                }
                catch
                {
                    fontName = richTextBox1.Font.Name;
                }
    
                float fontSize = Convert.ToSingle(fontSizeComboBox1.Text);
                richTextBox1.SelectionFont = new Font(fontName, fontSize);
            }
    View Code

    获得richTextBox1选中的字体大小

            private void richTextBox1_SelectionChanged(object sender, EventArgs e)
            {
                if (richTextBox1.SelectionFont == null)
                    return;
                fontSizeComboBox1.Text = richTextBox1.SelectionFont.Size.ToString();
            }
    View Code

     在toolStrip中添加这2个控件

    1 选中TOOL STRIP 置于底层,选中这2个控件置于顶层

    2 选中这2个控件按键盘的↑键把控件移上去

  • 相关阅读:
    java多线程为什么要用while而不是if
    爆竹声响,新人入场
    java多线程状态转换
    java接口的方法默认都是public abstract类型
    java中的静态static关键字
    我的第一个python爬虫程序
    python爬取某些网站出错的解决办法
    spring
    hibernate中文乱码问题
    eclipse(Version: Neon Release (4.6.0))安装hibernate tools
  • 原文地址:https://www.cnblogs.com/xe2011/p/3466506.html
Copyright © 2011-2022 走看看