The default winforms Button control only draws itself in a \"clicked state\" when the user left clicks the button. I need the Button control to draw itself in the clicked state
Standard button control sets the button in down and pressed mode using a private SetFlag
method. You can do it yourself too. I did it in following code:
using System.Windows.Forms;
public class MyButton : Button
{
protected override void OnMouseDown(MouseEventArgs e)
{
SetPushed(true);
base.OnMouseDown(e);
Invalidate();
}
protected override void OnMouseUp(MouseEventArgs e)
{
SetPushed(false);
base.OnMouseUp(e);
Invalidate();
}
private void SetPushed(bool value)
{
var setFlag = typeof(ButtonBase).GetMethod("SetFlag",
System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.NonPublic);
if(setFlag != null)
{
setFlag.Invoke(this, new object[] { 2, value });
setFlag.Invoke(this, new object[] { 4, value });
}
}
}