WinForm——Button总结
- 常用属性
- 实例总结
- 如何动态创建Button控件。
想要动态创建控件,首先要创建控件类的对象,然后使用系统Controls对象的Add方法将控件注册到窗体中。例:
private void Form1_MouseDown(object sender,MouseEventArgs e)
{
Button btn=new Button();
btn.Left=e.X;
btn.Top=e.Y;
btn.Name=”Button”+intCount.ToString();
btn.Text=”Button:+intCount.ToString();
this.Controls.Add(btn);
intCount++;
}
- 实现对Button按钮的重绘
新建用户控件,命名为ControlButton。然后将继承改为Button
public partial class ControlButton : Button
{
public ControlButton()
{
this.SetStyle(
ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint |
ControlStyles.OptimizedDoubleBuffer |
ControlStyles.ResizeRedraw |
ControlStyles.SupportsTransparentBackColor, true);
FlatStyle = FlatStyle.Flat;
FlatAppearance.BorderSize = 1;
FlatAppearance.BorderColor = Color.FromArgb(102, 102, 102);
FlatAppearance.MouseOverBackColor = Color.FromArgb(102, 102, 102);
FlatAppearance.MouseDownBackColor = Color.FromArgb(150, 150, 150);
}
private bool _isClicked = false;
public bool IsClicked
{
get { return _isClicked; }
set { _isClicked = value; }
}
private bool _inClient = false;
private bool _mouseDown = false;
private bool _enableClicked = false;
[Category("自定义属性"), Description("启用按下状态")]
public bool EnableClicked
{
get { return _enableClicked; }
set { _enableClicked = value; }
}
private Color _fontColor = Color.Gainsboro;
[Category("自定义属性"), Description("按下状态字体颜色")]
public Color ClickedForeColor
{
get { return _fontColor; }
set { _fontColor = value; }
}
private Size _imgSize;
[Category("自定义属性"), Description("图片尺寸")]
public Size ImageSize
{
get { return _imgSize; }
set
{
_imgSize = value;
Invalidate();
}
}
private Image _enterImage = null;
[Category("自定义属性"), Description("鼠标悬浮时图片")]
public Image EnterImage
{
get { return _enterImage; }
set
{
_enterImage = value;
}
}
private Image _downImage = null;
[Category("自定义属性"), Description("鼠标按下时图片")]
public Image DownImage
{
get { return _downImage; }
set { _downImage = value; }
}
protected override void OnMouseDown(MouseEventArgs mevent)
{
IsClicked = !IsClicked;
_mouseDown = true;
base.OnMouseDown(mevent);
}
protected override void OnMouseUp(MouseEventArgs mevent)
{
_mouseDown = false;
base.OnMouseUp(mevent);
}
protected override void OnMouseEnter(EventArgs e)
{
_inClient = true;
base.OnMouseEnter(e);
}
protected override void OnMouseLeave(EventArgs e)
{
_inClient = false;
base.OnMouseLeave(e);
}
}
生成成功后,在工具箱里面就可以看到。
代码参照:https://blog.csdn.net/qq_33324878/article/details/81065310
文章来源: wanghao.blog.csdn.net,作者:AI浩,版权归原作者所有,如需转载,请联系作者。
原文链接:wanghao.blog.csdn.net/article/details/105635511
- 点赞
- 收藏
- 关注作者
评论(0)