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