C#Winforms:以编程方式显示Button的悬停状态 [英] C# Winforms: programatically display Button's Hover State
问题描述
如果我们使用鼠标单击按钮,则会显示蓝色效果,以显示悬停&跌落状态..
我在想我是否可以根据用户在数字键盘上按下的键以某种方式以编程方式显示按钮的按下状态 ... 操作方法 >
不可能通过使用按钮类的默认实现来模拟此行为.但是,您可以对按钮进行子类化以添加此行为:
public class KeyboardButton : Button
{
public void SimulateButtonDown()
{
this.OnMouseDown(new MouseEventArgs(MouseButtons.Left, 0, 1, 1, 0));
}
public void SimulateButtonUp()
{
this.OnMouseUp(new MouseEventArgs(MouseButtons.Left, 0, 1, 1, 0));
}
}
当您调用SimulateButtonDown
时,该按钮将进入(可视)状态,仿佛将在该按钮上单击(并按住)鼠标.您也可以为悬停事件实现此类方法.
i am displaying a numeral keypad in on a winform to enter code. i am displaying nummpad with buttons... The users will be using only keyboards numpad to enter the code\password\? but off-course you can use mouse...
If we use mouse to click button we get a blue-ish effect to display hover & down states..
i was thinking if i can somehow programatically display the down-state of a button according to he key that user pressed on the numpad... HOW TO
It is not possible to simulate this behavior by using the default implementation of the button class. However, you can sub class the button to add this behavior:
public class KeyboardButton : Button
{
public void SimulateButtonDown()
{
this.OnMouseDown(new MouseEventArgs(MouseButtons.Left, 0, 1, 1, 0));
}
public void SimulateButtonUp()
{
this.OnMouseUp(new MouseEventArgs(MouseButtons.Left, 0, 1, 1, 0));
}
}
When you call SimulateButtonDown
the button will go into a (visual) state imitating that mouse would have been clicked (and held) on the button. You can implement methods like these for hover events as well.
这篇关于C#Winforms:以编程方式显示Button的悬停状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!