function Findwindowthroughwindowtext (windowtext:string): Thandle;
Var
Hcurrentwindow:thandle;
Cnt:integer;
Windowtitle:array [0.. 254] of Char;
Begin
Result: = Invalid_handle_value; The return value is preset to an invalid handle
Hcurrentwindow: = GetForegroundWindow; Find the first window of activity in the current operating system
CNT: = 1; Counter Reset initial value =1
While True does
Begin
If GetWindowText (Hcurrentwindow, @WindowTitle, 255) > 0 then//if the title of the window is found
If Strpos (WindowTitle, PChar (windowtext)) <> Nil then//if the target window is found
Break Jump out of the loop
Hcurrentwindow: = GetWindow (Hcurrentwindow, Gw_hwndnext); Find the next window
Exit until you find or exceed a certain number of times
If Hcurrentwindow = 0 Then
Begin
If the target window is not found after the order has been checked again, start looking again from the beginning,
Hcurrentwindow: = GetWindow (Application.handle, Gw_hwndfirst); Find the first window
Inc (CNT); Loop counter plus 1
If CNT > 10000 Then
Begin
If more than 10,000 times is exceeded (in this 10,000-cycle process, wait for Windows to set up//target window, as found in this procedure to exit successfully, or 10,000 times (about 30 seconds to 1 minutes) is still not found, prompting the user to continue looking for)
If Messagedlg (' cannot find a running ' + windowtext + ' window, the system may be corrupted!) do you want to continue running? ',
Mtconfirmation, [Mbok, Mbcancel], 0) = Mrok Then
Begin
Ask the user to choose whether to continue to find//if the user chooses to continue looking, then
CNT: = 1; Cycle counter resets the initial value =1
Continue; Start a new round of search
End
Else
Exit If the user abandons the lookup, exit
End
End
End
Result: = Hcurrentwindow; The return value is the window handle found
End
Procedure Tform1.button1click (Sender:tobject);
Var
H:thandle;
Begin
H:=findwindowthroughwindowtext (XXX);
If h <> Invalid_handle_value then
Begin
SendMessage (h,wm_ a message, parameter 1, parameter 2);
End
End
Procedure Tform1.button1click (Sender:tobject);
Var
H:thandle;
Begin
H:=findwindowthroughwindowtext (' Form1 ');
If h <> Invalid_handle_value then
Begin
ShowMessage (' ok1! ');
h:= FindWindowEx (h,0, ' tedit ', ' Edit1 ');
if (h=0) then exit;
ShowMessage (' ok2! ');
SendMessage (h,wm_settext,255, Integer (PChar (' I ')));
End
End
Procedure Tform1.button1click (Sender:tobject);
Var
FORMHWD, Okhandle, EDHwd1, Edhwd2:thandle;
Begin
FORMHWD: = FindWindow (nil, ' user login '); Window name
If formhwd > 0 Then
Begin
Okhandle: = FindWindowEx (formhwd, 0, PChar (' tbitbtn '), PChar (' OK '));//button
EDHWD1: = FindWindowEx (formhwd, 0, PChar (' Tedit '), nil);
SendMessage (EDHWD1, Wm_settext, 255, Longint (PChar (' 0 ')));
EDHWD2: = FindWindowEx (FORMHWD, EDHWD1, PChar (' Tedit '), nil);
SendMessage (EDHWD2, Wm_settext, 255, Longint (PChar (' jwc01 ')));
SendMessage (okhandle,bm_click,0,0);
End;
End;
http://blog.csdn.net/zang141588761/article/details/52061764
Delphi program sends a message to another executable program (using GetForegroundWindow; Find the first window of activity in the current operating system)