Delphi sendmessage和postmessage怎么用
代码如下unitUnit1;interfaceusesWindows,Messages,SysUtils,Variants,Classes,Graphics,Contro...
代码如下
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
f: TextFile;
s: string;
ts: string;
GameH: HWND;
implementation
{$R *.dfm}
Procedure Find;
begin
AssignFile(f, 'D:\360data\重要数据\我的文档\GTA San Andreas User Files\SAMP\chatlog.txt');
Reset(f);
try
while not eof(f) do
begin
readln(f, s);
ts := Copy(s, 2, 8);
if (Pos(s, ' ') > 0) then
begin
ts := Copy(s, Pos(s, '---->') + 19, Length(s));
end;
end;
finally
Close(f);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
sleep(5000);
Find;
Memo1.Text:=(ts);
GameH:=FindWindow('Grand theft auto San Andreas','GTA:SA:MP');
if GameH<>0 then
begin
keybd_event(84,0,0,0);
keybd_event(84,0,2,0);
PostMessage(GameH,0,0,Integer(Memo1.Text));//应该这里出了问题
end;
end;
end.
我得着段先从一个txt文本中查找内容,将内容输入memo1.text,然后再将memo1.text的内容用sendmessage或postmessage输出(自动键入),但是调试多次memo1的文本确实变了,但是自动输入不进指定的框。
PS:我要输入的是一个游戏(3D的)。我有一次调试把该游戏的标题文本给自动改变了。。。应该可以证明窗口被找到了吧。 展开
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
f: TextFile;
s: string;
ts: string;
GameH: HWND;
implementation
{$R *.dfm}
Procedure Find;
begin
AssignFile(f, 'D:\360data\重要数据\我的文档\GTA San Andreas User Files\SAMP\chatlog.txt');
Reset(f);
try
while not eof(f) do
begin
readln(f, s);
ts := Copy(s, 2, 8);
if (Pos(s, ' ') > 0) then
begin
ts := Copy(s, Pos(s, '---->') + 19, Length(s));
end;
end;
finally
Close(f);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
sleep(5000);
Find;
Memo1.Text:=(ts);
GameH:=FindWindow('Grand theft auto San Andreas','GTA:SA:MP');
if GameH<>0 then
begin
keybd_event(84,0,0,0);
keybd_event(84,0,2,0);
PostMessage(GameH,0,0,Integer(Memo1.Text));//应该这里出了问题
end;
end;
end.
我得着段先从一个txt文本中查找内容,将内容输入memo1.text,然后再将memo1.text的内容用sendmessage或postmessage输出(自动键入),但是调试多次memo1的文本确实变了,但是自动输入不进指定的框。
PS:我要输入的是一个游戏(3D的)。我有一次调试把该游戏的标题文本给自动改变了。。。应该可以证明窗口被找到了吧。 展开
1个回答
展开全部
//如果只打开一个记事本的话用下面的代码,如果是有多个的话,需要修改一下枚举窗口时的窗口名
//if pos(sample.txt - 记事本',StrPas(WinText))>0 then
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Controls, Forms, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
private
public
end;
var
Form1: TForm1;
HEdit: THandle;
implementation
{$R *.dfm}
//向控件发送字符
procedure SendDBCSString(hFocus: HWND; const sSend: string);
var
k : integer;
ch : byte;
begin
k := 1;
while k <= Length(sSend) do begin
ch := byte(sSend[k]);
if Windows.IsDBCSLeadByte(ch) then
begin
Inc(k);
SendMessage(hFocus, WM_IME_CHAR, MakeWord(byte(sSend[k]), ch), 0);
end
else
SendMessage(hFocus, WM_IME_CHAR, word(ch), 0);
Inc(k);
end;
end;
//枚举记事本窗口,获取记事本的内容输入框句柄
Function EnumWinProc(Wnd: HWND; Form1: TForm1): Boolean; Export;StdCall;
var
WinText : Array[0..255] of Char;
begin
GetWindowText(Wnd, WinText, 255);
Result := True;
if pos('记事本',StrPas(WinText))>0 then
HEdit:=FindWindowEx(WND,0,'Edit','');
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
EnumWindows(@EnumWinProc, LongInt(Self));
SendDBCSString(HEdit,'预先设定的文字');
end;
end.
这样可以么?
//if pos(sample.txt - 记事本',StrPas(WinText))>0 then
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Controls, Forms, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
private
public
end;
var
Form1: TForm1;
HEdit: THandle;
implementation
{$R *.dfm}
//向控件发送字符
procedure SendDBCSString(hFocus: HWND; const sSend: string);
var
k : integer;
ch : byte;
begin
k := 1;
while k <= Length(sSend) do begin
ch := byte(sSend[k]);
if Windows.IsDBCSLeadByte(ch) then
begin
Inc(k);
SendMessage(hFocus, WM_IME_CHAR, MakeWord(byte(sSend[k]), ch), 0);
end
else
SendMessage(hFocus, WM_IME_CHAR, word(ch), 0);
Inc(k);
end;
end;
//枚举记事本窗口,获取记事本的内容输入框句柄
Function EnumWinProc(Wnd: HWND; Form1: TForm1): Boolean; Export;StdCall;
var
WinText : Array[0..255] of Char;
begin
GetWindowText(Wnd, WinText, 255);
Result := True;
if pos('记事本',StrPas(WinText))>0 then
HEdit:=FindWindowEx(WND,0,'Edit','');
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
EnumWindows(@EnumWinProc, LongInt(Self));
SendDBCSString(HEdit,'预先设定的文字');
end;
end.
这样可以么?
追问
不行。。。。还有,这个百度上有。。。我试过了...
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询