delphi程序向另一个可执行程序发消息(100分)

  • 主题发起人 主题发起人 rehuman
  • 开始时间 开始时间
R

rehuman

Unregistered / Unconfirmed
GUEST, unregistred user!
一个vc编写的程序,假定为ok.exe,运行时标题中含"ok"字符串。在ok.exe中定义了加速键<br>ctrl+a,该加速键的ID为:IDM_OK(32804)。现在有一个delphi程序,想在delphi程序中向ok.exe<br>发送这一加速键消息。请用delphi写一个函数以实现这一功能。<br>我不会delphi语言,手里的delphi程序夜市别人写的,所以想请高手写个函数,我直接<br>放进去就可以了。<br>我自己有一个思路,在delphi中调用enumwindow函数,在他的回调函数中判断当前枚举的<br>窗口标题是不是含有ok,如果含有则发消息。如果各位高手有更好办法,那就更好!<br>谢谢!
 
先用FindWindow之类的函数获取窗口句柄,再用PostMessage函数发消息。
 
function FindWindowThroughWindowText(WindowText: string): THandle;<br>var<br>&nbsp; hCurrentWindow: THandle;<br>&nbsp; cnt: Integer;<br>&nbsp; WindowTitle: array[0..254] of Char;<br>begin<br>&nbsp; Result := INVALID_HANDLE_VALUE; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //返回值预设为无效的句柄<br>&nbsp; hCurrentWindow := GetForegroundWindow; //找出当前操作系统中活动的第一个窗口<br>&nbsp; cnt := 1; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //计数器置初值=1<br>&nbsp; while True do<br>&nbsp; begin<br>&nbsp; &nbsp; if GetWindowText(hCurrentWindow, @WindowTitle, 255) &gt; 0 then //如果找到窗口的标题<br>&nbsp; &nbsp; if StrPos(WindowTitle, PChar(WindowText)) &lt;&gt; nil then //如果找到的正是目标窗口则<br>&nbsp; &nbsp; break; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//跳出循环<br>&nbsp; &nbsp; hCurrentWindow := GetWindow(hCurrentWindow, GW_HWNDNEXT); //找下一窗口<br><br>&nbsp; &nbsp; //直到找到或超过一定的次数后退出<br>&nbsp; &nbsp; if hCurrentWindow = 0 then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; //如果顺序查一遍后未找到目标窗口,则重新从头开始查找,<br>&nbsp; &nbsp; &nbsp; hCurrentWindow := GetWindow(Application.Handle, GW_HWNDFIRST); //找到第一个窗口<br>&nbsp; &nbsp; &nbsp; inc(cnt); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 循环计数器加1<br>&nbsp; &nbsp; &nbsp; if cnt &gt; 10000 then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; //如果超出10000次则(在此10000次循环过程中等待windows建立完//目标窗口,如在此过程中找到则成功退出,否则10000次后(约30秒至1分钟)仍未找到,提示用户是否继续查找)<br>&nbsp; &nbsp; &nbsp; &nbsp; if MessageDlg('找不到运行中的' + WindowText + '窗口,可能该系统已损坏!是否继续运行?', mtConfirmation, [mbOK, mbCancel], 0) = mrOK then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //请用户选择是否继续查找//如用户选择继续查找,则<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cnt := 1; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //循环计数器重置初值=1<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Continue; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //开始新一轮查找<br>&nbsp; &nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; &nbsp; &nbsp; else exit; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//如用户放弃查找,则退出<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>&nbsp; Result := hCurrentWindow; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //返回值为找到的窗口句柄<br>end;<br><br>procedure TForm1.Button1Click(sender:TObject);<br>var <br>&nbsp; h:THandle;<br>begin<br>&nbsp; h:=FindWindowThroughWindowText(xxx);<br>&nbsp; if h &lt;&gt; INVALID_HANDLE_VALUE &nbsp;then<br>&nbsp; begin<br>&nbsp; &nbsp; SendMessage(h,WM_某某消息,参数1,参数2); <br>&nbsp; end;<br>end;<br>好了,先写这么多了,具体的你自己琢磨吧
 
查找窗口不应该由用户来干预,所以不应该加一个找不到窗口,让用户选择是否继续查找<br>,应该直接返回一个错误信号就可以了,消息中的参数写的不完整。
 
后退
顶部