选择用线程函数解决。
调用主窗体代码:
procedure TForm1.Button17Click(Sender: TObject); var Thd:FindForm; begin Thd := FindForm.Create(False); thd.DoIt := True; Thd.MainWnd := Self.Handle; emrpd.Print(2) ; //弹出窗体 Thd.DoIt := False; Thd.Free; end;
线程类FindForm如下:
unit Unit2; interface uses Classes,Windows,SysUtils,Messages; type FindForm = class(TThread) private { Private declarations } protected procedure Execute; override; public MainWnd:THandle; DoIt:Boolean; end; implementation uses Unit1; { Important: Methods and properties of objects in visual components can only be used in a method called using Synchronize, for example, Synchronize(UpdateCaption); and UpdateCaption could look like, procedure FindForm.UpdateCaption; begin Form1.Caption := ‘Updated in a thread‘; end; } { FindForm } function ExFindChildWindow(const TopWnd, ParentWnd: HWND; const WindowClass, WindowName: string): HWND; var ChdWnd, wnd: HWND; sClass, sName: string; begin ChdWnd := 0; result := 0; repeat ChdWnd := FindWindowEx(TopWnd, ChdWnd, nil, nil); if ChdWnd > 0 then begin sClass := GetClassNameByWnd(ChdWnd); sName := GetTextByWnd(ChdWnd); if ((WindowClass = sClass) or (WindowClass = ‘‘)) and ((WindowName = sName) or (Pos(WindowName, sName) > 0) or (WindowName = ‘‘)) then begin Result := ChdWnd; Exit; end else begin wnd := ExFindChildWindow(ChdWnd, ParentWnd, WindowClass, WindowName); if wnd > 0 then begin Result := wnd; Exit; end end; end; until ChdWnd = 0; end; procedure FindForm.Execute; var FormWnd:THandle; FilePathWnd:THandle; Edt_Hnd1,Edt_Hnd2,button_hnd1:THandle; t:string; begin { Place thread code here } while DoIt do begin FormWnd := ExFindChildWindow(0, MainWnd,‘‘, ‘行范围打印‘); if formwnd >0 then begin form1.Caption := ‘Find:行范围打印,‘+‘Handle=‘+inttostr(FormWnd); Edt_Hnd1 := FindWindowEx(FormWnd, 0, ‘Edit‘, ‘‘); Edt_Hnd2 := GetWindow(Edt_Hnd1,GW_HWNDNEXT); //找到下一个 控件 button_hnd1 :=GetWindow(Edt_Hnd2,GW_HWNDNEXT);// 找到下一个 控件 form1.Caption := ‘Find:行范围打印,‘+‘Handle=‘+inttostr(FormWnd)+‘,EDT1= ‘ +inttostr(Edt_Hnd1)+‘,Edt2=‘+Inttostr(Edt_Hnd2)+‘,btn1=‘+ IntToStr(button_hnd1); t:= ‘100‘; sendmessage(Edt_Hnd1, WM_SETTEXT, 0, LongInt(t)); //赋值消息 WM_SETTEXT sendmessage(Edt_Hnd2, WM_SETTEXT, 0, LongInt(t)); sendmessage(button_hnd1,WM_LBUTTONDOWN,0,0); //按下鼠标 sendmessage(button_hnd1,WM_LBUTTONUP,0,0); //释放鼠标 相当于click // sendmessage(button_hnd1,BN_CLICKED,0,0); //点击事件不起作用,不知为什么 DoIt := False; Exit; end; end; end; end.
时间: 2024-10-12 16:28:03