前几天帮朋友做的。。。发送组合键到游戏中<br>用到些第三方控件,可以换为对应的普通控件<br>可以全局按键操作或非全局按键操作<br><br>Unit Main;<br><br>Interface<br><br>Uses<br> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> Dialogs, StdCtrls, RzCommon, ComCtrls, RzEdit, RzTray, RzButton, RzRadChk,<br> RzLabel, ExtCtrls, ImgList, Inifiles, menus, Mask;<br><br>Type<br> TFrmMain = Class(TForm)<br> Tray: TRzTrayIcon;<br> HotKey1: TRzHotKeyEdit;<br> RzFrameController1: TRzFrameController;<br> lbl1: TLabel;<br> lbl2: TLabel;<br> HotKey2: TRzHotKeyEdit;<br> LbHwnd: TRzLabel;<br> lbl3: TLabel;<br> Timer: TTimer;<br> CmdStart: TRzBitBtn;<br> CmdStop: TRzBitBtn;<br> CmdMouse: TRzToolButton;<br> il1: TImageList;<br> Up: TRzNumericEdit;<br> lbl4: TLabel;<br> ChkGol: TRzCheckBox;<br> Procedure CmdMouseMouseDown(Sender: TObject; Button: TMouseButton;<br> Shift: TShiftState; X, Y: Integer);<br> Procedure CmdMouseMouseUp(Sender: TObject; Button: TMouseButton;<br> Shift: TShiftState; X, Y: Integer);<br> Procedure CmdMouseMouseMove(Sender: TObject; Shift: TShiftState; X,<br> Y: Integer);<br> Procedure FormCreate(Sender: TObject);<br> Procedure FormClose(Sender: TObject; Var Action: TCloseAction);<br> Procedure CmdStartClick(Sender: TObject);<br> Procedure CmdStopClick(Sender: TObject);<br> Procedure TimerTimer(Sender: TObject);<br> Private<br> Hwd: Hwnd;<br> SpyMode: Boolean;<br> P: TPoint;<br><br> HotID: Integer;<br> CurV: Integer;<br> Ini: Tinifile;<br> Procedure HotKey(Var msg: TWMHotKey); Message WM_HOTKEY;<br> Procedure LoadConfigs;<br> Procedure SaveConfigs;<br> Procedure SetControls(B: Boolean);<br> Public<br> { Public declarations }<br> End;<br><br>Var<br> FrmMain: TFrmMain;<br><br>Implementation<br><br>{$R *.dfm}<br>Procedure SetUpKey(ShortCut: TShortCut; Var Modifiers, Key: Word);<br>Var<br> Shift: TShiftState;<br>Begin<br> ShortCutToKey(ShortCut, Key, Shift);<br> Modifiers := 0;<br> If ssShift In Shift Then Modifiers := Modifiers Or MOD_SHIFT;<br> If ssCtrl In Shift Then Modifiers := Modifiers Or MOD_CONTROL;<br> If ssAlt In Shift Then Modifiers := Modifiers Or MOD_ALT;<br>End;<br><br>Procedure TFrmMain.HotKey(Var msg: TWMHotKey);<br>Begin<br> If HotKey1.Focused Then exit;<br> If msg.HotKey = HotID Then<br> If CmdStop.Enabled Then<br> CmdStop.Click Else CmdStart.Click;<br>End;<br><br>Procedure TFrmMain.LoadConfigs;<br>Var<br> Modifiers, Key: Word;<br>Begin<br> Ini := Tinifile.Create(ExtractFilePath(ParamStr(0)) + 'Config.ini');<br> HotKey1.HotKey := TextToShortCut(Ini.ReadString('Config', 'AppKey', 'CTRL+F12'));<br> HotKey2.HotKey := TextToShortCut(Ini.ReadString('Config', 'GameKey', '无'));<br> Up.IntValue := Ini.ReadInteger('Config', 'DoTime', 3);<br> ChkGol.Checked := Ini.ReadBool('Config', 'IsGobalKey', False);<br> SetUpKey(HotKey1.HotKey, Modifiers, Key);<br> HotID := GlobalAddAtom('HotKey_AppGame');<br> RegisterHotKey(handle, HotID, Modifiers, Key);<br>End;<br><br>Procedure TFrmMain.SaveConfigs;<br>Begin<br> UnregisterHotKey(handle, HotID);<br> GlobalDeleteAtom(HotID);<br> Ini.WriteString('Config', 'AppKey', ShortCutToText(HotKey1.HotKey));<br> Ini.WriteString('Config', 'GameKey', ShortCutToText(HotKey2.HotKey));<br> Ini.WriteInteger('Config', 'DoTime', Up.IntValue);<br> Ini.WriteBool('Config', 'IsGobalkey', ChkGol.Checked);<br>End;<br><br>Procedure TFrmMain.SetControls(B: Boolean);<br>Begin<br> CmdStart.Enabled := B;<br> CmdStop.Enabled := Not B;<br> Timer.Enabled := Not B;<br> HotKey1.Enabled := B;<br> HotKey2.Enabled := B;<br> CmdMouse.Enabled := B;<br> Up.Enabled := B;<br> LbHwnd.Enabled := B;<br> ChkGol.Enabled := B;<br>End;<br><br>Procedure TFrmMain.CmdMouseMouseDown(Sender: TObject; Button: TMouseButton;<br> Shift: TShiftState; X, Y: Integer);<br>Begin<br> Screen.Cursor := crCross;<br> SetCapture(handle);<br> SpyMode := true;<br> SetWindowPos(handle, HWND_TOPMOST, 0, 0, 0, 0, swp_nomove Or swp_nosize);<br>End;<br><br>Procedure TFrmMain.CmdMouseMouseUp(Sender: TObject; Button: TMouseButton;<br> Shift: TShiftState; X, Y: Integer);<br>Begin<br> Screen.Cursor := crDefault;<br> ReleaseCapture;<br> SpyMode := False;<br> SetWindowPos(handle, HWND_NOTOPMOST, 0, 0, 0, 0, swp_nomove Or swp_nosize);<br>End;<br><br>Procedure TFrmMain.CmdMouseMouseMove(Sender: TObject; Shift: TShiftState;<br> X, Y: Integer);<br>Begin<br> If Not SpyMode Then exit;<br> GetCursorPos(P);<br> Hwd := WindowFromPoint(P);<br> LbHwnd.Caption := IntToStr(Hwd);<br>End;<br><br>Procedure TFrmMain.FormCreate(Sender: TObject);<br>Begin<br> Ini := Tinifile.Create(ExtractFilePath(ParamStr(0)) + 'Config.ini');<br> LoadConfigs;<br>End;<br><br>Procedure TFrmMain.FormClose(Sender: TObject; Var Action: TCloseAction);<br>Begin<br> CmdStop.Click;<br> SaveConfigs;<br> Ini.Free;<br>End;<br><br>Procedure TFrmMain.CmdStartClick(Sender: TObject);<br>Begin<br> SaveConfigs;<br> LoadConfigs;<br> If (Hwd = 0) Then<br> Begin<br> MessageBox(handle, '请查找准备控制的窗口句柄!', '错误', MB_ICONHAND);<br> exit;<br> End;<br> If (HotKey2.HotKey = 0) Then<br> Begin<br> MessageBox(handle, '请设置执行键值!', '错误', MB_ICONHAND);<br> exit;<br> End;<br> SetControls(False);<br> CurV := 0;<br>End;<br><br>Procedure TFrmMain.CmdStopClick(Sender: TObject);<br>Begin<br> SetControls(true);<br>End;<br><br>Type<br> TSMsg = Packed Record<br> Shift: TShiftState;<br> Key: Word;<br> End;<br><br>Procedure SendMsg(AhWnd: Hwnd; M: TSMsg);<br>Begin<br> If ssShift In M.Shift Then<br> PostMessage(AhWnd, WM_KEYDOWN, VK_SHIFT, 0);<br> If ssAlt In M.Shift Then<br> PostMessage(AhWnd, WM_KEYDOWN, VK_MENU, 0);<br> If ssCtrl In M.Shift Then<br> PostMessage(AhWnd, WM_KEYDOWN, VK_CONTROL, 0);<br><br> PostMessage(AhWnd, WM_KEYDOWN, M.Key, 0);<br> PostMessage(AhWnd, WM_KEYUP, M.Key, 0);<br><br> If ssShift In M.Shift Then<br> PostMessage(AhWnd, WM_KEYUP, VK_SHIFT, 0);<br> If ssAlt In M.Shift Then<br> PostMessage(AhWnd, WM_KEYUP, VK_MENU, 0);<br> If ssCtrl In M.Shift Then<br> PostMessage(AhWnd, WM_KEYUP, VK_CONTROL, 0);<br>End;<br><br>Function EnumChildWndProc(AhWnd: Hwnd; AlParam: Pointer): Boolean; Stdcall;<br>Begin//遍历窗口句柄内的所有控件并发送键消息<br> SendMsg(AhWnd, TSMsg(AlParam^));<br> result := true;<br>End;<br><br>定时发送<br>Procedure TFrmMain.TimerTimer(Sender: TObject);<br>Var<br> M: TSMsg;<br> ThreadID: DWord;<br>Begin<br> Inc(CurV);<br> If CurV = Up.IntValue Then<br> Begin<br> CurV := 0;<br> ShortCutToKey(HotKey2.HotKey, M.Key, M.Shift);<br> If ChkGol.Checked Then<br> Begin<br> ThreadID := GetWindowThreadProcessId(Hwd, Nil);<br> AttachThreadInput(GetCurrentThreadId, ThreadID, true);<br> //ShowWindow(Hwd, SW_SHOWNOACTIVATE);<br> SetForegroundWindow(Hwd);<br> If ssShift In M.Shift Then<br> keybd_event(VK_SHIFT, MapVirtualKey(VK_SHIFT, 0), 0, 0);<br> If ssAlt In M.Shift Then<br> keybd_event(VK_MENU, MapVirtualKey(VK_MENU, 0), 0, 0);<br> If ssCtrl In M.Shift Then<br> keybd_event(VK_CONTROL, MapVirtualKey(VK_CONTROL, 0), 0, 0);<br><br> keybd_event(M.Key, MapVirtualKey(M.Key, 0), 0, 0);<br> keybd_event(M.Key, MapVirtualKey(M.Key, 0), KEYEVENTF_KEYUP, 0);<br><br> If ssShift In M.Shift Then<br> keybd_event(VK_SHIFT, MapVirtualKey(VK_SHIFT, 0), KEYEVENTF_KEYUP, 0);<br> If ssAlt In M.Shift Then<br> keybd_event(VK_MENU, MapVirtualKey(VK_MENU, 0), KEYEVENTF_KEYUP, 0);<br> If ssCtrl In M.Shift Then<br> keybd_event(VK_CONTROL, MapVirtualKey(VK_CONTROL, 0), KEYEVENTF_KEYUP, 0);<br><br> AttachThreadInput(GetCurrentThreadId, ThreadID, False);<br> ShowWindow(Hwd, SW_RESTORE);<br> End Else<br> Begin<br> SendMsg(Hwd, M);<br> EnumChildWindows(Hwd, @EnumChildWndProc, LongInt(@M));<br> End;<br> End;<br>End;<br><br>End.