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

后端 未结 1 328
你的背包
你的背包 2021-01-25 17:41

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

相关标签:
1条回答
  • 2021-01-25 18:10

    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 });
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题