Winforms Button Right-Click visual feedback (Show button in pushed state)

蓝咒 提交于 2019-12-02 07:22:21

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 });
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!