萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> .NET 程式設計

C# 實戰:打造 QQ 對戰平台自動「擠房」工具 — Win32 API 滑鼠鍵盤模擬與視窗偵測

痛點:手動擠房的無奈

在 QQ 對戰平台或浩方平台玩遊戲時,熱門房間常滿員。玩家需反覆點擊房間、關閉彈出的「房間已滿」提示視窗(竟然彈兩次!),十幾次還好,上百次純屬折磨。本文實作一個自動化工具,模擬滑鼠點擊與鍵盤關閉視窗,解放雙手。

自動化邏輯三步走

  1. 點擊房間:模擬滑鼠左鍵點擊當前滑鼠位置
  2. 判斷是否進入:偵測滑鼠下方視窗類別是否為聊天室(RichEdit20A 或 Afx:44a0000:0)
  3. 未進入則關閉提示:偵測前景視窗是否含「確定」按鈕,送 Enter 鍵關閉,回步驟 1

核心 Win32 API 封裝

public class Win32API {
    [DllImport("user32.dll")]
    public static extern bool GetCursorPos(out Point lpPoint);

    [DllImport("user32.dll")]
    public static extern IntPtr WindowFromPoint(Point pt);

    [DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    public static extern int GetClassName(IntPtr hWnd, StringBuilder buf, int nMaxCount);

    [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
    public static extern IntPtr GetForegroundWindow();

    [DllImport("user32.dll", EntryPoint = "FindWindowEx", SetLastError = true, CharSet = CharSet.Unicode)]
    public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

    public static string GetWindowClassName(IntPtr hWnd) {
        var sb = new StringBuilder(256);
        GetClassName(hWnd, sb, sb.Capacity);
        return sb.ToString();
    }
}

public class MouseAPI {
    [Flags] public enum MouseEvents : uint {
        LeftDown = 0x0002, LeftUp = 0x0004,
        RightDown = 0x0008, RightUp = 0x0010
    }
    [DllImport("user32.dll", EntryPoint = "mouse_event")]
    public static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
    public static void SendMouseEvent(MouseEvents flags, Point pt, bool absolute) {
        int dwFlags = (int)flags;
        if (absolute) dwFlags |= 0x8000; // MOUSEEVENTF_ABSOLUTE
        mouse_event(dwFlags, pt.X, pt.Y, 0, 0);
    }
}

public class KeyboardAPI {
    [DllImport("user32.dll")]
    public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
    public enum KeyboardEvents : int { KeyDown = 0, KeyUp = 2 }
    public static void SendKeyEvent(Keys key, KeyboardEvents evt) {
        keybd_event((byte)key, 0, (int)evt, 0);
    }
}

關鍵判斷邏輯

// 1. 判斷滑鼠是否在大廳視窗 (Afx:400000:3)
Point pt; IntPtr hwnd;
if (Win32API.GetCursorPos(out pt)) {
    hwnd = Win32API.WindowFromPoint(pt);
    if (hwnd != IntPtr.Zero && Win32API.GetWindowClassName(hwnd).Equals("Afx:400000:3")) {
        MouseAPI.SendMouseEvent(MouseAPI.MouseEvents.LeftDown | MouseAPI.MouseEvents.LeftUp, pt, true);
    }
}

// 2. 判斷是否已進入聊天室
bool isOk = false;
if (Win32API.GetCursorPos(out pt)) {
    hwnd = Win32API.WindowFromPoint(pt);
    if (hwnd != IntPtr.Zero) {
        string cls = Win32API.GetWindowClassName(hwnd);
        if (cls.Equals("RichEdit20A") || cls.Equals("Afx:44a0000:0")) isOk = true;
    }
}

// 3. 未進入則關閉提示視窗 (偵測前景視窗的「確定」按鈕)
if (!isOk) {
    hwnd = Win32API.GetForegroundWindow();
    if (hwnd != IntPtr.Zero) {
        IntPtr btn = Win32API.FindWindowEx(hwnd, IntPtr.Zero, "Button", null);
        if (btn != IntPtr.Zero) {
            KeyboardAPI.SendKeyEvent(Keys.Enter, KeyboardAPI.KeyboardEvents.KeyDown);
            KeyboardAPI.SendKeyEvent(Keys.Enter, KeyboardAPI.KeyboardEvents.KeyUp);
        }
    }
}

實作細節與避坑指南

  • 視窗類別名稱:使用 Spy++ 取得,不同版本平台可能不同,需實測調整
  • 座標系統:mouse_event 配合 MOUSEEVENTF_ABSOLUTE 需將座標轉為 0~65535 範圍,或直接用螢幕座標搭配相對移動
  • 執行緒安全:自動化迴圈建議放在 BackgroundWorker 或 Task.Run 中,避免凍結 UI
  • 防檢測:加入隨機延遲(Thread.Sleep(Random.Next(100,300))),降低被反作弊偵測風險
  • 熱鍵啟停:註冊全域熱鍵(RegisterHotKey)控制啟動/停止,提升易用性

法律與道德提醒

此工具僅供技術學習與個人便利使用。大多數遊戲平台用戶協議禁止自動化腳本,濫用可能導致帳號封禁。請遵守平台規則,勿用於商業牟利或破壞遊戲公平性。

copyright © 萬盛學電腦網 all rights reserved