SendMessage在结束进程上的使用

 

  Refresh  

     今天在看雪论坛注册了一个账号,需要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.

posted on 2010-10-31 22:12  Delphi7456  阅读(1545)  评论(0编辑  收藏  举报

导航