我制作了自己的CustomButton。
public partial class CustomButton : Button
{
public Image Background_Hover { get; set; }
public Image Background_Down { get; set; }
public Image Background_Normal;
public Image Image_Hover { get; set; }
public Image Image_Down { get; set; }
public Image Image_Normal;
public Color ForeColor_Hover { get; set; }
public Color ForeColor_Down { get; set; }
public Color ForeColor_Normal;
public CustomButton()
{
InitializeComponent();
this.SetStyle(ControlStyles.Selectable, false);
}
protected override void OnMouseEnter(System.EventArgs e)
{
Background_Normal = base.BackgroundImage;
if (Background_Hover != null) base.BackgroundImage = Background_Hover;
Image_Normal = base.Image;
if (Image_Hover != null) base.Image = Image_Hover;
ForeColor_Normal = base.ForeColor;
if (ForeColor_Hover != Color.Empty) base.ForeColor = ForeColor_Hover;
base.OnMouseEnter(e);
}
protected override void OnMouseLeave(System.EventArgs e)
{
if (Background_Hover != null) base.BackgroundImage = Background_Normal;
if (Image_Hover != null) base.Image = Image_Normal;
if (ForeColor_Hover != Color.Empty) base.ForeColor = ForeColor_Normal;
base.OnMouseLeave(e);
}
protected override void OnMouseDown(MouseEventArgs e)
{
if (Background_Down != null) base.BackgroundImage = Background_Down;
if (Image_Down != null) base.Image = Image_Down;
if (ForeColor_Down != Color.Empty) base.ForeColor = ForeColor_Down;
base.OnMouseDown(e);
}
protected override void OnMouseUp(MouseEventArgs e)
{
if (Background_Hover != null) base.BackgroundImage = Background_Hover;
if (Image_Hover != null) base.Image = Image_Hover;
if (ForeColor_Hover != Color.Empty) base.ForeColor = ForeColor_Hover;
base.OnMouseUp(e);
}
}
我制作了登录表单,因此我希望在用户输入Enter或ESC时关闭登录表单。
因此,我添加CustomButton并在登录表单的AcceptButton和Cancel Button属性上分配CustomButton。
但是当我输入Enter或ESC时它不起作用,甚至单击事件都不会发生。
我不知道为什么它不起作用。
因为我使CustomButton继承了System.Windows.Forms.Button。
我认为它的工作方式类似于System.Windows.Forms.Button,但事实并非如此。
我该如何解决这个问题?