快顯視窗殺手(下)

來源:互聯網
上載者:User

註冊系統熱鍵
系統熱鍵用在像快顯視窗殺手這種應用程式非常有用, Ctrl+Shift+J是預設熱鍵.
 
說道實現,我們繼續用RegisterHotkey(HWND hWnd, int id, UINT fsModifiers, UINT vkey). 完成,代碼如下:

public void SetHotKey(Keys c, bool bCtrl, bool bShift, bool bAlt, bool bWindows)
{
    m_hotkey = c;
    m_ctrlhotkey = bCtrl;
    m_shifthotkey = bShift;
    m_althotkey = bAlt;
    m_winhotkey = bWindows;
 
    // update hotkey
    NativeWIN32.KeyModifiers modifiers = NativeWIN32.KeyModifiers.None;
    if (m_ctrlhotkey)
        modifiers |= NativeWIN32.KeyModifiers.Control;
    if (m_shifthotkey)
        modifiers |= NativeWIN32.KeyModifiers.Shift;
    if (m_althotkey)
        modifiers |= NativeWIN32.KeyModifiers.Alt;
    if (m_winhotkey)
        modifiers |= NativeWIN32.KeyModifiers.Windows;
 
    NativeWIN32.RegisterHotKey(Handle, 100, modifiers, m_hotkey); //Keys.J);
}
一般的,註冊熱鍵要一下幾步

/* ------- using HOTKEYs in a C# application -------
 
   -- code snippet by James J Thompson --
 
在Form的load 中 : Ctrl+Shift+J
 
         bool success = RegisterHotKey(Handle,
                                      100,
                                      KeyModifiers.Control | KeyModifiers.Shift,
                                      Keys.J);
 
 
 在 form的closing中 :
 
         UnregisterHotKey(Handle, 100);
 
 
 如何處理熱鍵 :
 
     protected override void WndProc( ref Message m )
     {  
         const int WM_HOTKEY = 0x0312;      
        
         switch(m.Msg)    
          {      
             case WM_HOTKEY:        
                                  
                 MessageBox.Show("Hotkey pressed");           
 
                 ProcessHotkey();
 
                 break;     
         }        
         base.WndProc(ref m );
     }
 
 
public class NativeWIN32
{
    [DllImport("user32.dll", SetLastError=true)]
    public static extern bool RegisterHotKey( IntPtr hWnd, // handle to window   
                                              int id, // hot key identifier   
                                              KeyModifiers fsModifiers,  // key-modifier options   
                                              Keys vk            // virtual-key code   
    );
                
    [DllImport("user32.dll", SetLastError=true)]
    public static extern bool UnregisterHotKey( IntPtr hWnd, // handle to window   
                                                int id      // hot key identifier   
    );
 
    [Flags()]
    public enum KeyModifiers
    { 
        None = 0,
        Alt = 1,   
        Control = 2,   
        Shift = 4,   
        Windows = 8
    }
 
}
 
------- using HOTKEYs in a C# application ------- */
 
當我們按下熱鍵以後,流程是這樣:首先用HWND GetForegroundWindow()來得到表單,然後要抓出表單的標題, GetWindowText(HWND hwnd, /*out*/LPTSTR lpString, int nMaxCount). 具體如下:

 

protected void ProcessHotkey()
{
    IntPtr hwnd = NativeWIN32.GetForegroundWindow();
    if (!hwnd.Equals(IntPtr.Zero))
    {
        NativeWIN32.STRINGBUFFER sWindowTitle;
        NativeWIN32.GetWindowText(hwnd, out sWindowTitle, 256);
 
        if (sWindowTitle.szText.Length>0)
            AddWindowTitle( sWindowTitle.szText ); // add to the ListView (Form)
    }
}
 

代碼下載: http://www.codeproject.com/useritems/popupkiller/popupkiller_src_update.zip

示範程式: http://www.codeproject.com/useritems/popupkiller/popupkiller_demo_update.zip

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.