在 QQ 對戰平台或浩方平台玩遊戲時,熱門房間常滿員。玩家需反覆點擊房間、關閉彈出的「房間已滿」提示視窗(竟然彈兩次!),十幾次還好,上百次純屬折磨。本文實作一個自動化工具,模擬滑鼠點擊與鍵盤關閉視窗,解放雙手。
RichEdit20A 或 Afx:44a0000:0)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);
}
}
}
mouse_event 配合 MOUSEEVENTF_ABSOLUTE 需將座標轉為 0~65535 範圍,或直接用螢幕座標搭配相對移動BackgroundWorker 或 Task.Run 中,避免凍結 UIThread.Sleep(Random.Next(100,300))),降低被反作弊偵測風險RegisterHotKey)控制啟動/停止,提升易用性此工具僅供技術學習與個人便利使用。大多數遊戲平台用戶協議禁止自動化腳本,濫用可能導致帳號封禁。請遵守平台規則,勿用於商業牟利或破壞遊戲公平性。