SendMessage执行按钮并等待完成

时间:2019-12-25 04:54:42

标签: delphi-xe2 delphi-xe

我正在使用应用程序A控制应用程序B。我使用 SendMessage 在应用程序B中执行按钮。在应用程序B中的按钮执行之后,应用程序A无需等待应用程序B完成其操作即可完成另一项任务任务。在启动应用程序A的另一个任务之前,如何等待应用程序B中的单击按钮的过程完成?

begin
  ParentWindow := FindWindow('TfrmMain', nil);
  if (ParentWindow = 0) then
  begin
    ShowMessage('App cannot be found!!');
    Exit;
  end;

  // Get handle of Combobox
  Memo_temP.Clear;
  EnumChildWindows(ParentWindow, @EnumChildrenTComboBox, UINT_PTR(Memo_temP.Lines));
  temStr := Memo_temP.Lines.Strings[0];
  ComboboxHWND := StrToInt64('$' + temStr);

  // Get handle of output vaule
  Memo_temP.Clear;
  EnumChildWindows(ParentWindow, @EnumChildrenTMemo, UINT_PTR(Memo_temP.Lines));
  temStr := Memo_temP.Lines.Strings[0];
  MemoTxt2HWND := StrToInt64('$' + temStr);

  // Get handle of btn
  Memo_temP.Clear;
  EnumChildWindows(ParentWindow, @EnumChildrenTButton, UINT_PTR(Memo_temP.Lines));
  temStr := Memo_temP.Lines.Strings[1];
  if (temStr = '') then begin ShowMessage('Error4'); Exit; end;
  AsyncRunTestHWND := StrToInt64('$' + temStr);

  i := -1;
  while True do
  begin
    Inc(i);

    // Select each item in ComboBox
    SendMessage(ComboboxHWND, CB_SETCURSEL, i, 0);
    SendMessage(ComboboxHWND, WM_COMMAND, MakeWParam(0, CBN_SELCHANGE), ComboboxHWND);

    // Click btn
    SendMessage(AsyncRunTestHWND, WM_LBUTTONDOWN, MK_LBUTTON, MakeLParam(5, 5));
    SendMessage(AsyncRunTestHWND, WM_LBUTTONUP, MK_LBUTTON, MakeLParam(5, 5));

    // get result text from memo
    BufLen := SendMessage(MemoTxt2HWND, WM_GETTEXTLENGTH, 0, 0);
    SetLength(Buffer, BufLen + 1);
    SendMessage(MemoTxt2HWND, WM_GETTEXT, BufLen, LParam(PChar(Buffer)));

    if (PosEx('Error', Buffer) > 0) then
      Memo_Error.Lines.Add(Buffer)
    else
      Memo_Succeed.Lines.Add(Buffer);

    if SendMessage(ComboboxHWND, CB_SETCURSEL, i, 0) < 0 then Break;

  end;
end;

0 个答案:

没有答案