如何在窗体中的所有按钮上应用相同的样式 [英] How to apply same style on all buttons in windows form
本文介绍了如何在窗体中的所有按钮上应用相同的样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的windows窗体应用程序中有多个按钮,我想在btnhover
上应用一些样式
i have multiple buttons in my windows form application , and i want to apply some style on btnhover
like this
private void button1_MouseEnter(object sender, EventArgs e) {
button1.UseVisualStyleBackColor = false;
button1.BackColor = Color.GhostWhite;
}
private void button1_MouseLeave(object sender, EventArgs e) {
button1.UseVisualStyleBackColor = true;
}
我想把这个样式放在一个地方,我希望它自动应用于我表单中的所有按钮.我该怎么做,请帮助我并提前致谢
i want to put this style at one place and i want that it atuomatically apply on all buttons in my form . how can i do this , Please help me and thanks in advance
推荐答案
如果你真的想把它放在一个地方并拥有自动样式的表单,那就是它自己的类:
If you really want to put this in one place and have auto-styled forms, that would be its own class:
class ButtonStyledForm : Form
{
protected override void OnControlAdded(ControlEventArgs e)
{
base.OnControlAdded(e);
if (e.Control.GetType() == typeof(Button))
{
e.Control.MouseEnter += button_MouseEnter;
e.Control.MouseLeave += button_MouseLeave;
}
}
protected override void OnControlRemoved(ControlEventArgs e)
{
base.OnControlRemoved(e);
if (e.Control.GetType() == typeof(Button))
{
e.Control.MouseEnter -= button_MouseEnter;
e.Control.MouseLeave -= button_MouseLeave;
}
}
private void button_MouseEnter(object sender, EventArgs e) {
var c = (Button)sender;
c.UseVisualStyleBackColor = false;
c.BackColor = Color.GhostWhite;
}
private void button_MouseLeave(object sender, EventArgs e) {
var c = (Button)sender;
c.UseVisualStyleBackColor = true;
}
}
然后继承这个类而不是Form
.
Then inherit from this class instead of Form
.
这篇关于如何在窗体中的所有按钮上应用相同的样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文