SendMessage在结束进程上的使用


本文整理自网络,侵删。

 
 今天在看雪论坛注册了一个账号,需要100kx才能转为正式会员,故想编写一个可以在线刷kx的工具。

思路就是第一次运行程序的时候显示bbs主页,然后手动登陆论坛(其实可以用Au3实现自动登陆),然后每个指定时间(我设置为6分钟)再次访问论坛就可以了。

主要练习delphi控制台程序,一是如何调用外部程序,二是如何结束指定程序。

关于调用外部程序,无非就是ShellExecute或WinExec,我选的后者,因为参数少,简单些,不过貌似前者是现在的主流,而且可以直接访问网站。

关于结束进程,上网查了很多资料,比较多的方法是用正统的terminateprocess但使用起来比较麻烦,偶然发现有人说SendMessage也可以,就关注了一下后者。

没想到用SendMessage非常简便,所以就用它了。下面是源码:有不足之处希望批评指正!另外我的系统是Win7 32旗舰版 IE8.0


program Refresh;
 
{$APPTYPE CONSOLE}
 
uses
  SysUtils,
  Messages,
  WinProcs;
var
IsFirstRun,ErrorOccur:Boolean;
 
 
const UrlKanXue='C:\Program Files\Internet Explorer\IEXPLORE.EXE http://bbs.pediy.com/';
const SleepTime=360000;//360000;//6分钟
begin
  try
    { TODO -oUser -cConsole Main : Insert code here }
    while not ErrorOccur do
    begin
      if not IsFirstRun then
      begin
        IsFirstRun:=True;
        if WinExec(PAnsiChar(UrlKanXue),SW_NORMAL)>31 then //第一次运行时先访问主页,登陆一下。
        begin
          Writeln(FormatDateTime('hh:mm:ss',GetTime)+' 成功访问主页');
          Sleep(SleepTime);
          SendMessage(FindWindow('IEFrame','看雪软件安全论坛 - www.pediy.com - Windows Internet Explorer'),WM_CLOSE,0,0);
        end
        else
        ErrorOccur:=True;
      end
      else
      begin
        if WinExec(PAnsiChar(UrlKanXue),SW_HIDE)>31 then
        begin
          Writeln(FormatDateTime('hh:mm:ss',GetTime)+' 成功刷新页面');
          Sleep(SleepTime);
          SendMessage(FindWindow('IEFrame','看雪软件安全论坛 - www.pediy.com - Windows Internet Explorer'),WM_CLOSE,0,0);
        end
        else
        ErrorOccur:=True;
      end
    end;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

相关阅读 >>

Delphi webbroker 输出图片的问题

Delphi 建立access数据库

Delphi��image存入读取mysql数据库

Delphi 获取计算机名称3

Delphi 路径有空格调用批处理

Delphi 正则表达式的匹配模式

Delphi 调用外部程序获取程序id,并能关闭该程序

Delphi antiemupeb

Delphi string.create(char,count)

Delphi firedac,ado性能测试

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...