自动关闭弹出新闻广告窗口的一个实际例子,在Windows7+Delphi7中占用内存约为932K左右

program KillADProject1;

{由于用枚举窗口和模糊查询窗口的方法有点烦、占用内存有点大EnumWindows(@EnumWindowsProc, 0);}

{ if Pos(StrWindowPosTitle, buf) <> 0 then  通过窗口的某一部分标题查找窗口句柄}
{自动关闭弹出新闻广告窗口的一个实际例子,在Windows7+Delphi7中占用内存约为932K左右}
{而DelphiXE5( Release )中占用内存要比Delphi7多一点,并且FreeConsole控制台窗口会闪一下}
{窗体侦探 Window Spy 可以查看弹出广告窗口的标题或者类名}
{
Config.txt文件的内容
[窗口标题1]
[窗口标题2]
[无标题 - 记事本]
}
//删除 APPTYPE_CONSOLE 不显示DOS黑色窗口

{$R *.res}

uses
  SysUtils,
  Windows,
  Messages,
  Classes,
  Dialogs,
  IniFiles; {uses 包含 TIniFile 的单元}

var
  i: Integer;
  h: HWND; //窗口句柄变量,窗口内核对象的指针所在句柄表的下标索引。
  ini: TIniFile;
  path: string; {ini 文件路径}
  Section, Key: string; {分别表示 ini 文件的小节与关键字}
  List: TStrings;
{
ini文件结构:
;注释
[小节名]
关键字=值
INI文件支持: string、integer、boolean、Date、Time、DateTime、Double 与二进制类型
string 值没有引号
boolean 的真假用 1、0 表示
}
begin
  ini := TIniFile.Create(ExtractFilePath(ParamStr(0)) + '\Config.txt'); {ini 对象建立需要文件路径参数, 如果缺少路径会默认Windows目录}
  List := TStringList.Create;
  ini.ReadSections(List);
    //ShowMessage(list[i]);
  try
    { TODO -oUser -cConsole Main : Insert code here }
    FreeConsole;
    while True do //一直循环
    begin
      for i := 0 to List.Count - 1 do
      begin
        h := FindWindow(nil, PAnsiChar(AnsiString(List[i]))); //WinAPI函数,按标题查找窗口
        SendMessage(h, WM_CLOSE, 0, 0);

        Sleep(1000);
      end;
    end;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
  ini.Free;
end.

posted @ 2013-12-02 15:31  delphichm  阅读(393)  评论(0编辑  收藏  举报