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

- 自定义组件字体组合框
- 自定义组件字体组合框如何使用
- 自定义组件字体大小组合框
- 自定义组件字体大小组合框如何使用
- 如何设置richTextBox1选中的字体名称
- 如何获得richTextBox1选中的字体名称
- 如何设置richTextBox1选中的字体大小
- 如何获得richTextBox1选中的字体大小
- 如何在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(, );
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(, );
this.comboBox1.TabIndex = ; //OwnerDrawVariable
this.comboBox1.DrawMode = DrawMode.OwnerDrawVariable;
this.comboBox1.MaxDropDownItems = ;
this.comboBox1.DropDownWidth = ; 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, ); e.Graphics.DrawString(s, font, Brushes.Black, e.Bounds);
} private void comboBox1_MeasureItem(object sender, MeasureItemEventArgs e)
{
e.ItemHeight = ;
}
}
}
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(, );
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(, );
this.comboBox1.TabIndex = ;
//this.comboBox1.Sorted = true; //OwnerDrawVariable
this.comboBox1.DrawMode = DrawMode.OwnerDrawVariable;
this.comboBox1.MaxDropDownItems = ;
this.comboBox1.DropDownWidth = ;
this.comboBox1.Text = "";
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[] {
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
""});
} 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()) + ;
} }
}
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);
}
获得richTextBox1选中的字体名称
private void richTextBox1_SelectionChanged(object sender, EventArgs e)
{
if (richTextBox1.SelectionFont == null)
return; fontComboBox1.Text = richTextBox1.SelectionFont.Name.ToString();
}
如何设置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);
}
获得richTextBox1选中的字体大小
private void richTextBox1_SelectionChanged(object sender, EventArgs e)
{
if (richTextBox1.SelectionFont == null)
return;
fontSizeComboBox1.Text = richTextBox1.SelectionFont.Size.ToString();
}
在toolStrip中添加这2个控件
1 选中TOOL STRIP 置于底层,选中这2个控件置于顶层
2 选中这2个控件按键盘的↑键把控件移上去


FontCombobox 和FontSizeCombobox的更多相关文章
- qt_文本编辑器实现_附带详细注释和源码下载
源码下载: 链接: http://pan.baidu.com/s/1c21EVRy 密码: qub8 实现主要的功能有:新建,打开,保存,另存为,查找(查找的时候需要先将光标放到最下面位置才能查全,不 ...
- Qt 控件
一.布局管理器 QHBoxLayout 水平布局 QVBoxLayout 垂直布局 QGridLayout 格点布局 QFormLayout 关联布局 QSplitter 分裂器 Spacers 间隔 ...
- QT_文本编辑器_源码下载
源码下载: 链接: http://pan.baidu.com/s/1c21EVRy 密码: qub8 实现主要的功能有:新建,打开,保存,另存为,查找(查找的时候需要先将光标放到最下面位置才能查全,不 ...
- 【PyQt5-Qt Designer】文本框读写操作
主要内容: 1.读.写 输入控件(Input Widgets)中的内容(str) 2.保存数据到txt文件 3.从txt文件中读内容,与输入控件中内容比较 将上述各种输入控件(Input Widget ...
- Winform自定义控件实例
本文转自http://www.cnblogs.com/hahacjh/archive/2010/04/29/1724125.html 写在前面: .Net已经成为许多软件公司的选择,而.Net自定义W ...
- pyqt 多窗口跳转
今天在做pyqt5的多页面跳转时遇到问题,一点击button按钮,程序会崩溃.在网上查了下,应该是当窗口A调用窗口B的时候,两个窗口不能是同一类型.我写的时候把A.B同时写成了QWidget.把窗口B ...
- [Qt Creator 快速入门] 第4章 布局管理
第3章讲述了一些窗口部件,当时往界面上拖放部件时都是随意放置的,这对于学习部件的使用没有太大的影响,但是,对于一个完善的软件,布局管理却是必不可少的. 无论是想要界面中部件有一个很整齐的排列,还是想要 ...
- 30.QT IDE编写
mainwindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QTe ...
- 29.QT主窗口加widget
运行效果 widget布局showwidget.h #ifndef SHOWWIDGET_H #define SHOWWIDGET_H #include <QWidget> #includ ...
随机推荐
- warning: no newline at end of file
编译错误:warning: no newline at end of file原因:程序结尾需要有一个空行解决办法:在程序末尾多打个回车就行了
- 【转】成为Java顶尖程序员 ,看这11本书就够了
成为Java顶尖程序员 ,看这11本书就够了 转自:http://developer.51cto.com/art/201512/503095.htm 以下是我推荐给Java开发者们的一些值得一看的好书 ...
- Java 之 网络编程
1.OSI模型 a.全称:开放系统互联 b.七层模型:应用层.表示层.会话层.传输层.网络层.数据链路层.物理层 c.注意:OSI模型 是所谓的 理论标准 2.TCP/IP模型 a.四层模型:应用层. ...
- iPhone不同机型适配 6/6plus --备用
机型变化 坐标:表示屏幕物理尺寸大小,坐标变大了,表示机器屏幕尺寸变大了: 像素:表示屏幕图片的大小,跟坐标之间有个对应关系,比如1:1或1:2等: ppi:代表屏幕物理大小到图片大小的比例值,如果 ...
- aspx、ashx以及cs的关系,viewState
aspx和ashx关系:aspx就是一种特殊的ashx,aspx对应的类是page,它是实现了IHttpHandler接口,所以说aspx是高级的HttpHandler.aspx中帮我们封装了很多操作 ...
- 【POJ 3623】 Best Cow Line, Gold (后缀数组)
[题意] [分析] 后缀数组水题,嗯,不认真看输出像我一样就会被坑.. #include<cstdio> #include<cstdlib> #include<cstri ...
- http://jinnianshilongnian.iteye.com/blog/1996071
http://jinnianshilongnian.iteye.com/blog/1996071 http://my.oschina.net/jkcui/blog/388400 http://tian ...
- 李洪强iOS开发Swift篇---12_NSThread线程相关简单说明
李洪强iOS开发Swift篇---12_NSThread线程相关简单说明 一 说明 1)关于多线程部分的理论知识和OC实现,在之前的博文中已经写明,所以这里不再说明. 2)该文仅仅简单讲解NSThre ...
- zabbix 通过key 获取
zabbix:/root# zabbix_get -s 192.168.2.224 -k "perf_counter[\Processor(_Total)\% Processor Time] ...
- WordPress WooCommerce ‘hide-wc-extensions-message’参数跨站脚本漏洞
漏洞名称: WordPress WooCommerce ‘hide-wc-extensions-message’参数跨站脚本漏洞 CNNVD编号: CNNVD-201310-501 发布时间: 201 ...