第一步 面板区设置
新建一个控件项目 取名叫blog_cartoonFont
新建三个label 分别修改其text 属性 学生 教师 管理员
再修改textAlign属性 为居中 Dock属性为left autosize属性为false
调整后如下
然后我们再将每个label宽度调整到一致
将每一个label 的Font属性 调整为 微软雅黑 大小为三号
第二步 代码区设置
在控件类 里面 添加三个属性 一个事件
private Color _isSeleColor = Color.FromArgb(46, 204, 113); //绿色
[Description("控件被选中时的颜色")]
public Color IsSeleColor //用于获取 字体没选中的时候的 颜色
{
get { return _isSeleColor; }
set{ _isSeleColor = value; }
}
private int _selectedStatus = 1;
[Description("哪个标签被选中")]
public int SelectStatus //用于标志哪一个 label被选中 1代表第一个 2第二个。。。
{
get { return _selectedStatus; }
set { _selectedStatus = value; }
}
private Color _noSeleColor = Color.FromArgb(223, 223, 223);
[Description("控件未被选中时的颜色")]
public Color NoSeleColor //用于获取 字体未被选中时候的颜色
{
get { return _noSeleColor; }
set{ _noSeleColor = value; }
}
public event EventHandler button_userControl_click; //用于开发人员使用该控件的时候 能够调用该控件的 点击事件
添加控件窗口的load事件 用于初始化每一个label 因为默认是第一个label被选中 所以第一个label 颜色改为绿色 字号变为22 其他的label颜色为灰色
private void button_userControl_Load(object sender, EventArgs e)
{
label1.ForeColor = _isSeleColor;
label1.Font = new Font("微软雅黑", 22);
label2.ForeColor = _noSeleColor;
label3.ForeColor = _noSeleColor;
}
效果如下
添加 文字缩小 扩大 函数 的定义
//
//文本的缓慢扩大和缩小开始
//
private static Control reduce_control = null; //用于获取需要缩小的label
private static Control expand_control = null;
private static int reduce_fontsize = 15; //定义文字缩小到15大小
private static int expand_fontsize = 22;
private static Timer reduce_tmrAnim = null; //用于缓慢缩小字体的 时间控件
private static Timer expand_tmrAnim = null; //用于缓慢扩大字体的 时间控件
//定义两个 时间控件 分别用于不同标签的 延时扩大和延时缩小 效果
private static void reduce_InitTimer()
{
if (reduce_tmrAnim == null)
{
//实例化一个时间控件
reduce_tmrAnim = new Timer();
//14毫秒执行一次 Tick的函数
reduce_tmrAnim.Interval = 14;
//Tick要执行的函数tmrAnim_Tick
reduce_tmrAnim.Tick += new System.EventHandler(reduce_tmrAnim_Tick);
}
}
private static void expand_InitTimer()
{
if (expand_tmrAnim == null)
{
//实例化一个时间控件
expand_tmrAnim = new Timer();
//14毫秒执行一次 Tick的函数
expand_tmrAnim.Interval = 14;
//Tick要执行的函数tmrAnim_Tick
expand_tmrAnim.Tick += new System.EventHandler(expand_tmrAnim_Tick);
}
}
private void reduce(Control con) //要缩小文字 的label需要调用这个函数
{
reduce_InitTimer(); //调用函数 创建时间控件同时
reduce_control = con; //获取需要缩小文字的控件
reduce_tmrAnim.Start(); //开启时间控件的tick执行的函数
}
private void expand(Control con) //要扩大文字 的label需要调用这个函数
{
expand_InitTimer();
expand_control = con;
expand_tmrAnim.Start();
}
private static void reduce_tmrAnim_Tick(object sender, System.EventArgs e) //该函数是时间控件tick 需要执行的函数
{
reduce_control.Font = new Font("微软雅黑", expand_fontsize); //每14毫秒 重置该控件字体的大小 每次缩小1单位
expand_fontsize--;
if (expand_fontsize <= 15)
{
expand_fontsize = 22;
reduce_tmrAnim.Stop(); //当字体缩小到15 大小后 停止该时间控件的tick事件
}
}
private static void expand_tmrAnim_Tick(object sender, System.EventArgs e)
{
expand_control.Font = new Font("微软雅黑", reduce_fontsize);
reduce_fontsize++;
if (reduce_fontsize >= 22)
{
reduce_fontsize = 15;
expand_tmrAnim.Stop();
}
}
//
//文本的缓慢扩大和缩小结束
//
定义每一个label的点击事件
同时 刚刚自定义事件 的触发 放在 每一个label的点击事件中(使得任意一个label被点击都会触发该事件)
private void label1_Click(object sender, EventArgs e)
{
label1.ForeColor = _isSeleColor;
label2.ForeColor = _noSeleColor;
label3.ForeColor = _noSeleColor;
expand(label1);
if (SelectStatus == 2)
{
reduce(label2);
}
else if (SelectStatus == 3)
{
reduce(label3);
}
SelectStatus = 1; //标识 用于返回 当前用户点击的是哪一个label
if (button_userControl_click != null) //自定义事件的触发
{
button_userControl_click(sender, e);
}
}
private void label2_Click(object sender, EventArgs e)
{
label2.ForeColor = _isSeleColor;
label1.ForeColor = _noSeleColor;
label3.ForeColor = _noSeleColor;
expand(label2);
if (SelectStatus == 1)
{
reduce(label1);
}
else if (SelectStatus == 3)
{
reduce(label3);
}
SelectStatus = 2;
if (button_userControl_click != null)
{
button_userControl_click(sender, e);
}
}
private void label3_Click(object sender, EventArgs e)
{
label3.ForeColor = _isSeleColor;
label1.ForeColor = _noSeleColor;
label2.ForeColor = _noSeleColor;
expand(label3);
if (SelectStatus == 1)
{
reduce(label1);
}
else if (SelectStatus == 2)
{
reduce(label2);
}
SelectStatus = 3;
if (button_userControl_click != null)
{
button_userControl_click(sender, e);
}
}
在每一个label的属性窗口添加 对应的函数
添加完毕后直接 调试程序 即可
第三步 效果展示
第四步 附上所有代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace blog_cartoonFont
{
public partial class UserControl1: UserControl
{
public UserControl1()
{
InitializeComponent();
}
private Color _isSeleColor = Color.FromArgb(46, 204, 113);
[Description("控件被选中时的颜色")]
public Color IsSeleColor
{
get { return _isSeleColor; }
set
{
_isSeleColor = value;
}
}
private int _selectedStatus = 1;
[Description("那个标签被选中")]
public int SelectStatus
{
get { return _selectedStatus; }
set { _selectedStatus = value; }
}
private Color _noSeleColor = Color.FromArgb(223, 223, 223);
[Description("控件未被选中时的颜色")]
public Color NoSeleColor
{
get { return _noSeleColor; }
set
{
_noSeleColor = value;
}
}
public event EventHandler button_userControl_click;
private void button_userControl_Load(object sender, EventArgs e)
{
label1.ForeColor = Color.FromArgb(46, 204, 113);
label1.Font = new Font("微软雅黑", 22);
label2.ForeColor = Color.FromArgb(223, 223, 223);
label3.ForeColor = Color.FromArgb(223, 223, 223);
}
private void label1_Click(object sender, EventArgs e)
{
label1.ForeColor = _isSeleColor;
label2.ForeColor = _noSeleColor;
label3.ForeColor = _noSeleColor;
expand(label1);
if (SelectStatus == 2)
{
reduce(label2);
}
else if (SelectStatus == 3)
{
reduce(label3);
}
SelectStatus = 1;
if (button_userControl_click != null)
{
button_userControl_click(sender, e);
}
}
private void label2_Click(object sender, EventArgs e)
{
label2.ForeColor = _isSeleColor;
label1.ForeColor = _noSeleColor;
label3.ForeColor = _noSeleColor;
expand(label2);
if (SelectStatus == 1)
{
reduce(label1);
}
else if (SelectStatus == 3)
{
reduce(label3);
}
SelectStatus = 2;
if (button_userControl_click != null)
{
button_userControl_click(sender, e);
}
}
private void label3_Click(object sender, EventArgs e)
{
label3.ForeColor = _isSeleColor;
label1.ForeColor = _noSeleColor;
label2.ForeColor = _noSeleColor;
expand(label3);
if (SelectStatus == 1)
{
reduce(label1);
}
else if (SelectStatus == 2)
{
reduce(label2);
}
SelectStatus = 3;
if (button_userControl_click != null)
{
button_userControl_click(sender, e);
}
}
//
//文本的缓慢扩大和缩小开始
//
private static Control reduce_control = null;
private static Control expand_control = null;
private static int reduce_fontsize = 15;
private static int expand_fontsize = 22;
private static Timer reduce_tmrAnim = null;
private static Timer expand_tmrAnim = null;
//定义两个 时间控件 分别用于不同标签的 延时扩大和延时缩小 效果
private static void reduce_InitTimer()
{
if (reduce_tmrAnim == null)
{
reduce_tmrAnim = new Timer();
reduce_tmrAnim.Interval = 14;
reduce_tmrAnim.Tick += new System.EventHandler(reduce_tmrAnim_Tick);
}
}
private static void expand_InitTimer()
{
if (expand_tmrAnim == null)
{
expand_tmrAnim = new Timer();
expand_tmrAnim.Interval = 14;
expand_tmrAnim.Tick += new System.EventHandler(expand_tmrAnim_Tick);
}
}
private void reduce(Control con)
{
reduce_InitTimer();
reduce_control = con;
reduce_tmrAnim.Start();
}
private void expand(Control con)
{
expand_InitTimer();
expand_control = con;
expand_tmrAnim.Start();
}
private static void reduce_tmrAnim_Tick(object sender, System.EventArgs e)
{
reduce_control.Font = new Font("微软雅黑", expand_fontsize);
expand_fontsize--;
if (expand_fontsize <= 15)
{
expand_fontsize = 22;
reduce_tmrAnim.Stop();
}
}
private static void expand_tmrAnim_Tick(object sender, System.EventArgs e)
{
expand_control.Font = new Font("微软雅黑", reduce_fontsize);
reduce_fontsize++;
if (reduce_fontsize >= 22)
{
reduce_fontsize = 15;
expand_tmrAnim.Stop();
}
}
//
//文本的缓慢扩大和缩小结束
//
}
}