给你写了一个发消息的简单Demo,你可以参考一下:(代码未加入异常控制)unit Unit1;interfaceuses Windows, Messages, SysUtils, Classes, Controls, Forms, StdCtrls, ExtCtrls;type TForm1 = class(TForm) btn1: TButton; procedure btn1Click(Sender: TObject); end;var Form1: TForm1; fwHWND: HWND = 0; OKBtn: HWND = 0;implementation{$R *.dfm}function EnumChildWindowsProc(lHwnd, lParam: Integer): Boolean; stdcall;var WndCaption: array[0..254] of Char; WndClassName: array[0..254] of Char;begin GetWindowText(lHwnd, @WndCaption, 254); GetClassName(lHwnd, @WndClassName, 254); if StrPas(WndClassName) = 'TEdit' then //如果是 Edit 类 begin if SendMessage(lHwnd, EM_GETPASSWORDCHAR, 0, 0) <> 0 then //根据掩码来判断密码框 SendMessage(lHwnd, WM_SETTEXT, 0, Integer(PChar('password'))) else SendMessage(lHwnd, WM_SETTEXT, 0, Integer(PChar('username'))); end; //根据类名及按键的 Caption 来得到 "确定" 按键的句柄 //本来可以在这里直接用 SendMessage(lHwnd, BM_CLICK, 0, 0); 来触发点击的 //但测试时发现有时不起作用, 故将按键句柄传出后在 btn1.Click 中加入上述语句 if (StrPas(WndClassName) = 'TButton') and (Copy(WndCaption, 1, 4) = '确定') then OKBtn := lHwnd; Result := True;end;procedure TForm1.btn1Click(Sender: TObject);var hCurrentWindow: HWND; szText: array[0..1024] of Char;begin //这里只是简单的调用, 可以考虑引用 ShellAPI 单元文件后用以下语句 //ShellExecute(0, 'open', 'demo.exe', '', 'D:/', SW_NORMAL); WinExec(PChar('D:/demo.exe'), SW_NORMAL); //有些程序启动时会有延时, 为了确保能够得到其登录窗体的句柄, 加入延时 Sleep(1000); hCurrentWindow := GetWindow(Handle, GW_HWNDFIRST); while hCurrentWindow <> 0 do begin if GetWindowText(hCurrentWindow, @szText, 255) > 0 then if StrPas(@szText) = '目标程序' then //根据目标程序的登录窗体的标题查找得到该窗体句柄 begin fwHWND := hCurrentWindow; Break; end; hCurrentWindow := GetWindow(hCurrentWindow, GW_HWNDNEXT); end; //枚举登录窗体的控件 if fwHWND <> 0 then EnumChildWindows(fwHWND, @EnumChildWindowsProc, 0); if OKBtn <> 0 then SendMessage(OKBtn, BM_CLICK, 0, 0);end;end.