Winforms按钮右键单击视觉反馈(处于按下状态的“显示”按钮) [英] Winforms Button Right-Click visual feedback (Show button in pushed state)

查看:100
本文介绍了Winforms按钮右键单击视觉反馈(处于按下状态的“显示”按钮)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认的winforms Button控件仅在用户左键单击按钮时将其自身绘制为单击状态。我需要Button控件将其自身绘制为单击状态,而不管它是左键单击还是右键单击。我将如何实现呢?

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 regardless of it was left clicked or right clicked. How would I accomplish this?

更具体地说,我知道我需要从Button派生一个扩展功能的控件,但是我没有扩展Winforms功能的经验GDI +中的控件或绘图。因此,我对在那里到底需要做的事情一无所知。

More specifically, I know I will need to derive a control from Button which extends the functionality, but I have no experience in extending functionality of winforms controls or drawing in GDI+. So I'm a little dumbfounded on what exactly I'll need to do once in there.

感谢您的帮助。

推荐答案

标准按钮控件使用专用的 SetFlag 方法将按钮设置为按下和按下模式。您也可以自己做。我是在以下代码中完成的:

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 });
        }
    }
}

这篇关于Winforms按钮右键单击视觉反馈(处于按下状态的“显示”按钮)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆