C# 模拟平面按钮鼠标向下和鼠标上方
我有一个Windows窗体应用程序,带有一些F键按钮。当您将鼠标放在按钮上时,按钮将变为灰色,当您单击按钮时,按钮将变为略亮灰色。我想用F键模拟这种行为。。。如何操作?使用Button.PerformClick()。使用Button.PerformClick()。将表单的C# 模拟平面按钮鼠标向下和鼠标上方,c#,user-interface,C#,User Interface,我有一个Windows窗体应用程序,带有一些F键按钮。当您将鼠标放在按钮上时,按钮将变为灰色,当您单击按钮时,按钮将变为略亮灰色。我想用F键模拟这种行为。。。如何操作?使用Button.PerformClick()。使用Button.PerformClick()。将表单的KeyPreview属性设置为true,处理向下键和向上键事件,跟踪按下的功能键,并为每个向下或向上的键调用按钮上的Invalidate方法 然后,处理按钮的Paint事件,如果其按键处于按下状态,则使用来绘制按钮,就像按钮被按
KeyPreview
属性设置为true
,处理向下键
和向上键
事件,跟踪按下的功能键,并为每个向下或向上的键调用按钮上的Invalidate
方法
然后,处理按钮的
Paint
事件,如果其按键处于按下状态,则使用来绘制按钮,就像按钮被按下一样。将窗体的KeyPreview
属性设置为true
,处理KeyDown
和KeyUp
事件,跟踪按下的功能键,并为每个向下或向上的键调用按钮上的Invalidate
方法
然后,处理按钮的
绘制事件,如果其按键按下,则使用绘制按钮,就像按下按钮一样。和ReleaseCapture可能工作。和ReleaseCapture可能工作。最后,我实现了更改背景的按钮:
class FunctionButton : Button
{
private Color m_colorOver;
private bool m_isPressed;
public FunctionButton() : base()
{
m_isPressed = false;
}
protected override void OnGotFocus(EventArgs e)
{
OnMouseEnter(null);
base.OnGotFocus(e);
}
protected override void OnLostFocus(EventArgs e)
{
if (!m_isPressed)
{
OnMouseLeave(null);
}
base.OnLostFocus(e);
}
protected override void OnMouseLeave(EventArgs e)
{
if (!Focused && !m_isPressed)
{
base.OnMouseLeave(e);
}
}
public void FunctionKeyPressed()
{
// Handle just the first event
if (!m_isPressed)
{
m_isPressed = true;
m_colorOver = FlatAppearance.MouseOverBackColor;
FlatAppearance.MouseOverBackColor = FlatAppearance.MouseDownBackColor;
OnMouseEnter(null);
PerformClick();
}
}
public void FunctionKeyReleased()
{
m_isPressed = false;
FlatAppearance.MouseOverBackColor = m_colorOver;
if (Focused)
{
OnMouseEnter(null);
}
else
{
base.OnMouseLeave(null);
}
}
}
这不是最干净的方法,但效果很好。我希望有更多的例子能以更干净、更优雅的风格来实现这一点。最后,我实现了改变背景的按钮:
class FunctionButton : Button
{
private Color m_colorOver;
private bool m_isPressed;
public FunctionButton() : base()
{
m_isPressed = false;
}
protected override void OnGotFocus(EventArgs e)
{
OnMouseEnter(null);
base.OnGotFocus(e);
}
protected override void OnLostFocus(EventArgs e)
{
if (!m_isPressed)
{
OnMouseLeave(null);
}
base.OnLostFocus(e);
}
protected override void OnMouseLeave(EventArgs e)
{
if (!Focused && !m_isPressed)
{
base.OnMouseLeave(e);
}
}
public void FunctionKeyPressed()
{
// Handle just the first event
if (!m_isPressed)
{
m_isPressed = true;
m_colorOver = FlatAppearance.MouseOverBackColor;
FlatAppearance.MouseOverBackColor = FlatAppearance.MouseDownBackColor;
OnMouseEnter(null);
PerformClick();
}
}
public void FunctionKeyReleased()
{
m_isPressed = false;
FlatAppearance.MouseOverBackColor = m_colorOver;
if (Focused)
{
OnMouseEnter(null);
}
else
{
base.OnMouseLeave(null);
}
}
}
这不是最干净的方法,但效果很好。我希望有更多的示例以更简洁、更优雅的风格完成此操作。这会调用功能,但按钮的图形状态不会更改。这会调用功能,但按钮的图形状态不会更改。我将尝试类似的操作。谢谢我不工作,因为你不能用ButtonRenderer渲染平面按钮:-我会尝试类似的方法。谢谢我不工作,因为你不能用ButtonRenderer呈现平面按钮:-哦,与这个问题高度相关,与这个问题高度相关