[C#] 使用Application.AddMessageFilter當做Form的熱鍵

來源:互聯網
上載者:User

標籤:blog   http   io   使用   ar   for   sp   log   on   

Application.AddMessageFilter可以幫你監視所有系統發送到Form的訊息 
我們利用它來過濾訊息 
便可以做出我們需要的熱鍵

 

這是Application.AddMessageFilter的來電者式 
必須傳遞一個IMessageFilter的對象當做參數

public static void AddMessageFilter(

    IMessageFilter value

)

我們這邊做一個繼承IMessageFilter介面的HotKey類別

class HotKey : IMessageFilter
{
    ...
}

然後在HotKey類別裡使用兩個屬性記錄熱鍵的組合方式 
並在HotKey建立時給予初始值

Keys _hotKey = Keys.None;
Keys _comboKey = Keys.None;
 
public HotKey(Keys hotKey, Keys comboKey)
{
    _hotKey = hotKey; //熱鍵
    _comboKey = comboKey; //按鍵組合, 必須設定Keys.Control, Keys.Alt, Keys.Shift, Keys.None等值才有作用
    Application.AddMessageFilter(this); //使用HotKey類別來監視訊息
}

IMessageFilter介面還必須實作一個PreFilterMessage方法 
用來監視/過濾我們要的訊息 
當我們按下正確的熱鍵時 
它會觸發自訂的事件OnHotKey

public delegate void HotkeyEventHandler(object sender, EventArgs e);
public event HotkeyEventHandler OnHotkey; //自訂事件
 
const int WM_KEYDOWN = 0x100; //按下鍵盤的訊息
const int WM_ALTKEYDOWN = 0x104; //按下Alt鍵的訊息
 
public bool PreFilterMessage(ref Message m)
{
    if (OnHotkey != null && (m.Msg == WM_KEYDOWN || m.Msg == WM_ALTKEYDOWN)) //當鍵盤按下時
    {
        if (((Keys)(int)m.WParam & Keys.KeyCode) == _hotKey && Control.ModifierKeys == _comboKey) //如果熱鍵+按鍵組合相符
        {
            OnHotkey(this, null); //觸發自訂事件
            return true; //並攔截這個訊息, Form將不再接收到這個訊息
        }
    }
    return false;
}

到這邊我們就完成了一個簡單版的HotKey了 
我們來測試看看 
在Form1_Load裡產生四組熱鍵

private void Form1_Load(object sender, EventArgs e)
{
    HotKey hotkey = new HotKey(Keys.A, Keys.Alt); //註冊Alt+A為熱鍵
    hotkey.OnHotkey += new HotKey.HotkeyEventHandler(hotkey_OnHotkey);
 
    HotKey hotkey2 = new HotKey(Keys.B, Keys.Control); //註冊Ctrl+B為熱鍵
    hotkey2.OnHotkey += new HotKey.HotkeyEventHandler(hotkey2_OnHotkey);
 
    HotKey hotkey3 = new HotKey(Keys.C, Keys.Shift); //註冊Shift+C為熱鍵
    hotkey3.OnHotkey += new HotKey.HotkeyEventHandler(hotkey3_OnHotkey);
 
    HotKey hotkey4 = new HotKey(Keys.F2, Keys.None); //註冊F2為熱鍵, 如果不要按鍵組合請傳Keys.None當參數
    hotkey4.OnHotkey += new HotKey.HotkeyEventHandler(hotkey4_OnHotkey);
}
 
 
void hotkey_OnHotkey(object sender, EventArgs e)
{
    MessageBox.Show("熱鍵Alt+A被按下了");
}
 
void hotkey2_OnHotkey(object sender, EventArgs e)
{
    MessageBox.Show("熱鍵Ctrl+B被按下了");
}
 
private void hotkey3_OnHotkey(object sender, EventArgs e)
{
    MessageBox.Show("熱鍵Shift+C被按下了");
}
 
private void hotkey4_OnHotkey(object sender, EventArgs e)
{
    MessageBox.Show("熱鍵F2被按下了");
}

 http://www.dotblogs.com.tw/sam319/archive/2010/01/06/12876.aspx

[C#] 使用Application.AddMessageFilter當做Form的熱鍵

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.