其实就是一个模拟按键了,给你看看 :<br><br>unit Unit1;<br><br>interface<br><br>uses<br> SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,<br> Forms, Dialogs, StdCtrls;<br><br>type<br> TForm1 = class(TForm)<br> Button1: TButton;<br> Button2: TButton;<br> procedure Button1Click(Sender: TObject);<br> procedure Button2Click(Sender: TObject);<br> procedure FormKeyPress(Sender: TObject; var Key: Char);<br> private<br> AppInst: THandle;<br> AppWind: THandle;<br> public<br> { Public declarations }<br> end;<br><br>var<br> Form1: TForm1;<br><br>implementation<br><br>{$R *.DFM}<br><br>uses ShellAPI;<br> <br><br>procedure SendShift(H: HWnd; Down: Boolean);<br>var vKey, ScanCode, wParam: Word;<br> lParam: longint;<br>begin<br> vKey:= $10;<br> ScanCode:= MapVirtualKey(vKey, 0);<br> wParam:= vKey or ScanCode shl 8;<br> lParam:= longint(ScanCode) shl 16 or 1;<br> if not(Down) then lParam:= lParam or $C0000000;<br> SendMessage(H, WM_KEYDOWN, vKey, lParam);<br>end;<br><br>procedure SendCtrl(H: HWnd; Down: Boolean);<br>var vKey, ScanCode, wParam: Word;<br> lParam: longint;<br>begin<br> vKey:= $11;<br> ScanCode:= MapVirtualKey(vKey, 0);<br> wParam:= vKey or ScanCode shl 8;<br> lParam:= longint(ScanCode) shl 16 or 1;<br> if not(Down) then lParam:= lParam or $C0000000;<br> SendMessage(H, WM_KEYDOWN, vKey, lParam);<br>end;<br><br>procedure SendKey(H: Hwnd; Key: char);<br>var vKey, ScanCode, wParam: Word;<br> lParam, ConvKey: longint;<br> Shift, Ctrl: boolean;<br>begin<br> ConvKey:= OemKeyScan(ord(Key));<br> Shift:= (ConvKey and $00020000) <> 0;<br> Ctrl:= (ConvKey and $00040000) <> 0;<br> ScanCode:= ConvKey and $000000FF or $FF00;<br> vKey:= ord(Key);<br> wParam:= vKey;<br> lParam:= longint(ScanCode) shl 16 or 1;<br> if Shift then SendShift(H, true);<br> if Ctrl then SendCtrl(H, true);<br> SendMessage(H, WM_KEYDOWN, vKey, lParam);<br> SendMessage(H, WM_CHAR, vKey, lParam);<br> lParam:= lParam or $C0000000;<br> SendMessage(H, WM_KEYUP, vKey, lParam);<br> if Shift then SendShift(H, false);<br> if Ctrl then SendCtrl(H, false);<br>end;<br><br>function EnumFunc(Handle: HWnd; TF: TForm1): Bool; Far;<br>begin<br> TF.AppWind:= 0;<br> if GetWindowWord(Handle, GWW_HINSTANCE) = TF.AppInst then<br> TF.AppWind:= Handle;<br> result:= (TF.AppWind = 0);<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var Text: Array[0..255] of char;<br>begin<br> AppInst:= ShellExecute(Handle, 'open', 'notepad.exe', nil, '', SW_NORMAL);<br> EnumWindows(@EnumFunc, longint(self));<br> AppWind:= GetWindow(AppWind, GW_CHILD);<br>end;<br><br> <br>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br> SendKey(AppWind, 'T');<br> SendKey(AppWind, 'e');<br> SendKey(AppWind, 's');<br> SendKey(AppWind, 't');<br>end;<br><br>procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);<br>begin<br> if AppWind <> 0 then SendKey(AppWind, Key);<br>end;<br><br>end.<br>