2009-08-20 64 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事件的用戶控制,以便消費者可以將事件處理程序附加到該事件並對其執行操作。