2009-08-20 65 views
1

我想制作一个按键,与鼠标一样使用键盘。我采取这种方式:用Windows窗体中的键盘模拟点击

class FunctionButton : System.Windows.Forms.Button 
{ 
    public FunctionButton() : base() { } 

    protected override void OnGotFocus(EventArgs e) 
    { 
     OnMouseEnter(null); 
     base.OnGotFocus(e); 
    } 

    protected override void OnLostFocus(EventArgs e) 
    { 
     OnMouseLeave(null); 
     base.OnLostFocus(e); 
    } 

    protected override void OnMouseLeave(EventArgs e) 
    { 
     if (!Focused) 
     { 
      base.OnMouseLeave(e); 
     } 
    } 

    public void FunctionKeyPressed() 
    { 
     OnMouseDown(new MouseEventArgs(MouseButtons.Left,1,0,0,0)); 
     PerformClick(); 
    } 

    public void FunctionKeyReleased() 
    { 
     if (Focused) 
     { 
      OnMouseEnter(null); 
     } 
     else 
     { 
      base.OnMouseLeave(null); 
     } 
    } 
} 

我不知道如何获得有效的点击位置此按钮可产生解事件

OnMouseDown(new MouseEventArgs(MouseButtons.Left,1,X,Y,0)); 

我怎样才能做到这一点?实现这种按钮的更好方法是什么?

+0

Highlt与此问题相关 http://stackoverflow.com/questions/1299157/simulate-flat-button-mouse-mousedown-and-mouseover – yeyeyerman 2009-08-20 14:12:21

回答

0

最后我实现了按钮更改背景:

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

这不是最干净的方式,但它工作得很好。我希望更多的例子以更清洁和更优雅的风格来做到这一点。

0

如果我正确理解你的问题,你正在创建一个控件,它将显示一个用户可以点击的按钮,并且该控件也将被绑定到应该调用相同命令的功能键。

我认为最简单的(虽然也许稍微“哈克”)的方式来做到这一点,是创建一个UserControl,一个MenuStrip控件添加到您的用户控件添加ToolStripMenuItemMenuStrip控制,并成立了Click事件处理程序,并将MenuStripVisible属性设置为false。还要向用户控件添加一个按钮,并为该控件设置一个事件处理程序。同时拥有事件处理程序调用一些方法来执行的操作,并最终创建一个ShortcutKeys物业反映菜单项的ShortcutKeys属性:

public partial class FunctionButton : UserControl 
{ 
    public FunctionButton() 
    { 
     InitializeComponent(); 
    } 

    private void MenuItem_Click(object sender, EventArgs e) 
    { 
     PerformCommand();    
    } 

    private void Button_Click(object sender, EventArgs e) 
    { 
     PerformCommand(); 
    } 

    private void PerformCommand() 
    { 
     OnClick(EventArgs.Empty); 
    } 

    public Keys ShortcutKeys 
    { 
     get 
     { 
      return _menuItem.ShortcutKeys; 
     } 
     set 
     { 
      _menuItem.ShortcutKeys = value; 
     } 
    } 
} 

在我的样本的Click事件处理程序将简单地提高Click事件的用户控制,以便消费者可以将事件处理程序附加到该事件并对其执行操作。