Delphi编程求助
本帖最后由 冥界3大法王 于 2019-12-11 15:27 编辑直接是Button1单击时,测试正常!想变成函数或过程调用,好节约或重复代码!
当定义成函数或过程 ,再Button2调用时就没有反映了,为什么?
procedure TForm1.Button2Click(Sender: TObject);
begin
Test1(Edit1.Text);
end;
function Test1(s: string): string;
加上面的代码
var
hCurrentWindow: hWnd;
szText: array of char;
h: Integer;
lpCaption: array of Char;
begin
hCurrentWindow := GetWindow(Handle, GW_HWNDFIRST); //获取第一个窗口的句柄
while hCurrentWindow <> 0 do
begin //获取窗口的名称
if GetWindowText(hCurrentWindow, szText, 255) > 0 then
begin
//ListBox1.Items.Add(StrPas(szText)); //获取下一窗口的句柄
if (POS('- x32dbg', StrPas(szText)) > 0) or (POS('- x64dbg', StrPas(szText)) > 0) then
begin
//ShowMessage(StrPas(szText));
h := FindWindow(nil, PWideChar(StrPas(szText)));
//ShowMessage(IntToStr(h));
IntToStr(GetWindowText(h, lpCaption, 255));
//ShowMessage(lpCaption);
SetForegroundWindow(FindWindow(nil, PWideChar(StrPas(szText))));
Sleep(500);
SetCursorPos(850, 350);
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
Sleep(1000);
keybd_event(VK_Control, MapVirtualKey(VK_Control, 0), 0, 0); //按下Ctrl键
keybd_event(VK_RETURN, MapVirtualKey(VK_RETURN, 0), 0, 0); //按下回车键
keybd_event(VK_RETURN, MapVirtualKey(VK_RETURN, 0), KEYEVENTF_KEYUP, 0); //弹起回车键
keybd_event(VK_Control, MapVirtualKey(VK_Control, 0), KEYEVENTF_KEYUP, 0); //放开Ctrl键
Sleep(1000);
keybd_event(VK_Control, MapVirtualKey(VK_Control, 0), 0, 0); //按下Ctrl键
keybd_event(ord('V'), MapVirtualKey(ord('V'), 0), 0, 0); //按下v键
keybd_event(VK_Control, MapVirtualKey(VK_Control, 0), KEYEVENTF_KEYUP, 0); //放开Ctrl键
keybd_event(ord('V'), MapVirtualKey(ord('V'), 0), KEYEVENTF_KEYUP, 0); //放开v键
end;
end;
hCurrentWindow := GetWindow(hCurrentWindow, GW_HWNDNEXT);
end;
end; 楼主:“以下”,“上面”表达不清,没看明白你的问题。 本帖最后由 冥界3大法王 于 2019-12-11 15:06 编辑
晕,还行不行,最后请来一个ActrionList!
procedure TForm1.Button2Click(Sender: TObject);
begin
SendPressExecute(SendPress);这样调用!
end;
SendPress是动作的名字 ,这样竟然正常了。{:301_1008:}
什么原因呢,还得请上仙解答吧。 GetWindow(Handle, GW_HWNDFIRST);
这句代码引起的原因
function Test1(s: string): string;
改成
function TForm1.Test1(s: string): string;
才是完全的相等
TForm1.Test1时
Handle指的是TForm1.Handle
Test1时
Handle会编译前报错的,就是编译不过去的
不知道你为什么没有报错 pzx521521 发表于 2019-12-11 15:04
GetWindow(Handle, GW_HWNDFIRST);
这句代码引起的原因
function Test1(s: string): string;
因为我把Handle 改成0了。
虽然编译通过,但是点击没效果。 改成function TForm1.Test1(s: string): string;
结果报错:E2003 Undeclared identifier: 'Test1' 说句泄气的话:delphi其实不是一个好选择。 dazhuangzhuang 发表于 2019-12-11 15:02
楼主:“以下”,“上面”表达不清,没看明白你的问题。
还是上完整的代码吧。
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
Vcl.StdCtrls, System.Actions, Vcl.ActnList;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Edit1: TEdit;
ActionList1: TActionList;
SendPress: TAction;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure SendPressExecute(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses
Tlhelp32;
function TForm1.Test2(s: string): string;
var
hCurrentWindow: hWnd;
szText: array of char;
h: Integer;
lpCaption: array of Char;
begin
hCurrentWindow := GetWindow(0, GW_HWNDFIRST); //获取第一个窗口的句柄
while hCurrentWindow <> 0 do
begin //获取窗口的名称
if GetWindowText(hCurrentWindow, szText, 255) > 0 then
begin
//ListBox1.Items.Add(StrPas(szText)); //获取下一窗口的句柄
if (POS('- x32dbg', StrPas(szText)) > 0) or (POS('- x64dbg', StrPas(szText)) > 0) then
begin
//ShowMessage(StrPas(szText));
h := FindWindow(nil, PWideChar(StrPas(szText)));
//ShowMessage(IntToStr(h));
IntToStr(GetWindowText(h, lpCaption, 255));
//ShowMessage(lpCaption);
SetForegroundWindow(FindWindow(nil, PWideChar(StrPas(szText))));
Sleep(500);
SetCursorPos(850, 350);
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
Sleep(1000);
keybd_event(VK_Control, MapVirtualKey(VK_Control, 0), 0, 0); //按下Ctrl键
keybd_event(VK_RETURN, MapVirtualKey(VK_RETURN, 0), 0, 0); //按下回车键
keybd_event(VK_RETURN, MapVirtualKey(VK_RETURN, 0), KEYEVENTF_KEYUP, 0); //弹起回车键
keybd_event(VK_Control, MapVirtualKey(VK_Control, 0), KEYEVENTF_KEYUP, 0); //放开Ctrl键
Sleep(1000);
keybd_event(VK_Control, MapVirtualKey(VK_Control, 0), 0, 0); //按下Ctrl键
keybd_event(ord('V'), MapVirtualKey(ord('V'), 0), 0, 0); //按下v键
keybd_event(VK_Control, MapVirtualKey(VK_Control, 0), KEYEVENTF_KEYUP, 0); //放开Ctrl键
keybd_event(ord('V'), MapVirtualKey(ord('V'), 0), KEYEVENTF_KEYUP, 0); //放开v键
end;
end;
hCurrentWindow := GetWindow(hCurrentWindow, GW_HWNDNEXT);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
hCurrentWindow: hWnd;
szText: array of char;
h: Integer;
lpCaption: array of Char;
begin
hCurrentWindow := GetWindow(0, GW_HWNDFIRST); //获取第一个窗口的句柄
while hCurrentWindow <> 0 do
begin //获取窗口的名称
if GetWindowText(hCurrentWindow, szText, 255) > 0 then
begin
//ListBox1.Items.Add(StrPas(szText)); //获取下一窗口的句柄
if (POS('- x32dbg', StrPas(szText)) > 0) or (POS('- x64dbg', StrPas(szText)) > 0) then
begin
//ShowMessage(StrPas(szText));
h := FindWindow(nil, PWideChar(StrPas(szText)));
//ShowMessage(IntToStr(h));
IntToStr(GetWindowText(h, lpCaption, 255));
//ShowMessage(lpCaption);
SetForegroundWindow(FindWindow(nil, PWideChar(StrPas(szText))));
Sleep(500);
SetCursorPos(850, 350);
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
Sleep(1000);
keybd_event(VK_Control, MapVirtualKey(VK_Control, 0), 0, 0); //按下Ctrl键
keybd_event(VK_RETURN, MapVirtualKey(VK_RETURN, 0), 0, 0); //按下回车键
keybd_event(VK_RETURN, MapVirtualKey(VK_RETURN, 0), KEYEVENTF_KEYUP, 0); //弹起回车键
keybd_event(VK_Control, MapVirtualKey(VK_Control, 0), KEYEVENTF_KEYUP, 0); //放开Ctrl键
Sleep(1000);
keybd_event(VK_Control, MapVirtualKey(VK_Control, 0), 0, 0); //按下Ctrl键
keybd_event(ord('V'), MapVirtualKey(ord('V'), 0), 0, 0); //按下v键
keybd_event(VK_Control, MapVirtualKey(VK_Control, 0), KEYEVENTF_KEYUP, 0); //放开Ctrl键
keybd_event(ord('V'), MapVirtualKey(ord('V'), 0), KEYEVENTF_KEYUP, 0); //放开v键
end;
end;
hCurrentWindow := GetWindow(hCurrentWindow, GW_HWNDNEXT);
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
SendPressExecute(SendPress);
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
Test2(Edit1.Text);
end;
procedure TForm1.SendPressExecute(Sender: TObject);
var
hCurrentWindow: hWnd;
szText: array of char;
h: Integer;
lpCaption: array of Char;
begin
hCurrentWindow := GetWindow(Handle, GW_HWNDFIRST); //获取第一个窗口的句柄
while hCurrentWindow <> 0 do
begin //获取窗口的名称
if GetWindowText(hCurrentWindow, szText, 255) > 0 then
begin
//ListBox1.Items.Add(StrPas(szText)); //获取下一窗口的句柄
if (POS('- x32dbg', StrPas(szText)) > 0) or (POS('- x64dbg', StrPas(szText)) > 0) then
begin
//ShowMessage(StrPas(szText));
h := FindWindow(nil, PWideChar(StrPas(szText)));
//ShowMessage(IntToStr(h));
IntToStr(GetWindowText(h, lpCaption, 255));
//ShowMessage(lpCaption);
SetForegroundWindow(FindWindow(nil, PWideChar(StrPas(szText))));
Sleep(500);
SetCursorPos(850, 350);
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
Sleep(1000);
keybd_event(VK_Control, MapVirtualKey(VK_Control, 0), 0, 0); //按下Ctrl键
keybd_event(VK_RETURN, MapVirtualKey(VK_RETURN, 0), 0, 0); //按下回车键
keybd_event(VK_RETURN, MapVirtualKey(VK_RETURN, 0), KEYEVENTF_KEYUP, 0); //弹起回车键
keybd_event(VK_Control, MapVirtualKey(VK_Control, 0), KEYEVENTF_KEYUP, 0); //放开Ctrl键
Sleep(1000);
keybd_event(VK_Control, MapVirtualKey(VK_Control, 0), 0, 0); //按下Ctrl键
keybd_event(ord('V'), MapVirtualKey(ord('V'), 0), 0, 0); //按下v键
keybd_event(VK_Control, MapVirtualKey(VK_Control, 0), KEYEVENTF_KEYUP, 0); //放开Ctrl键
keybd_event(ord('V'), MapVirtualKey(ord('V'), 0), KEYEVENTF_KEYUP, 0); //放开v键
end;
end;
hCurrentWindow := GetWindow(hCurrentWindow, GW_HWNDNEXT);
end;
end;
end.
@coolcalf 那你说选择啥?
本帖最后由 冥界3大法王 于 2019-12-11 15:37 编辑coolcalf 发表于 2019-12-11 15:26
说句泄气的话:delphi其实不是一个好选择。易语言更狗屎
难道还有哪个学习难度低
马上就能搞出来的工具?
https://doc.ivx.cn 这个说得很牛逼,不知真的假的。骗大伙的钱我看是真! 看不懂,不理解