首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> VC >> VC 教學

彈出視窗殺手(下):C# 註冊系統熱鍵與前景視窗標題擷取

     編輯:VC 教學

前言

承接上篇,本文說明如何在 C# 中註冊系統熱鍵(RegisterHotKey),並在熱鍵觸發時取得前景視窗標題,作為攔截彈出視窗的判斷依據。

註冊熱鍵步驟

// 表單載入時註冊 Ctrl+Shift+J
private const int HOTKEY_ID = 100;
private void Form1_Load(object sender, EventArgs e)
{
    bool success = RegisterHotKey(this.Handle, HOTKEY_ID,
        KeyModifiers.Control | KeyModifiers.Shift, Keys.J);
    if (!success) MessageBox.Show("熱鍵註冊失敗,可能已被佔用。");
}

// 表單關閉時解除註冊
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    UnregisterHotKey(this.Handle, HOTKEY_ID);
}

// P/Invoke 宣告
[DllImport("user32.dll")]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);

[DllImport("user32.dll")]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);

[Flags]
private enum KeyModifiers : uint
{
    None = 0,
    Alt = 1,
    Control = 2,
    Shift = 4,
    Windows = 8
}

處理熱鍵訊息

覆寫 WndProc 攔截 WM_HOTKEY(0x0312):

protected override void WndProc(ref Message m)
{
    if (m.Msg == 0x0312 && m.WParam.ToInt32() == HOTKEY_ID)
    {
        ProcessHotkey();
    }
    base.WndProc(ref m);
}

private void ProcessHotkey()
{
    IntPtr hwnd = GetForegroundWindow();
    if (hwnd != IntPtr.Zero)
    {
        StringBuilder sb = new StringBuilder(256);
        GetWindowText(hwnd, sb, sb.Capacity);
        string title = sb.ToString();
        if (!string.IsNullOrEmpty(title))
        {
            // 將標題加入列表或進行比對
            listView1.Items.Add(title);
        }
    }
}

[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();

[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

關鍵點提醒

  • 熱鍵 ID 唯一:同一視窗內不可重複;跨視窗可重複但建議使用 GlobalAddAtom 產生全域唯一 ID。
  • 修飾鍵組合:Control | Shift 等於 6,需與虛擬鍵碼(Keys.J = 0x4A)一起傳遞。
  • 權限問題:UAC 高權限視窗無法被低權限處理序取得標題,需以管理員身分執行或加入 uiAccess 清單。

結語

掌握 RegisterHotKey 與 GetForegroundWindow/GetWindowText 的搭配,即可在 C# 輕鬆實作全域熱鍵觸發的視窗標題擷取,為彈出視窗攔截、快速截圖、記事本呼叫等工具奠定基礎。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved