大家一定用过xp吧,应该知道xp有一个功能很不错,可以一次性关闭已打开的程序,一般程序运行后,会在任务栏上有一个按钮,按钮上有窗口的名字。现在大家开始动手吧,
我已经找到以下程序可以列出 任务栏上有按钮的所以程序窗口名称。
****************************************************************
uses shellapi;function GetText(Wnd : HWND) : string;
var
textlength : integer;
text : PChar;
begin
  textlength:=SendMessage(Wnd,WM_GETTEXTLENGTH,0,0);
  if textlength=0 then
  Result := ''
  else begin
  getmem(text,textlength+1);
  SendMessage(Wnd,WM_GETTEXT,textlength+1,Integer(text));
  Result:=text;
  freemem(text);
  end;
end;function EnumWindowsProc (Wnd: HWND; LParam: LPARAM): BOOL; stdcall;
begin
  Result := True;
  if (IsWindowVisible(Wnd)) and
  (GetWindowLong(Wnd, GWL_HWNDPARENT) = 0) and
  (GetWindowLong(Wnd, GWL_EXSTYLE) and WS_EX_TOOLWINDOW = 0) then
    begin
      Form1.Listbox1.items.add('Handle:'+Inttostr(Wnd)+',Text:'+GetText(Wnd));      
    end;
end;procedure TForm1.Button1Click(Sender: TObject);
var
Param : Longint;
begin
  EnumWindows(@EnumWindowsProc , Param); //列出已运行的程序
end;请大家帮忙练习一下,如果将这些已列出的程序,一个个关闭。期待中。

解决方案 »

  1.   

    得到窗体句柄后用SengMessage函数向窗体发送WM_CLOSE消息呀!注意:这样会把一些系统窗体关闭,如左下角的《开始》菜单,有些系统窗体则是关不掉的。
      

  2.   

    var
      ZAppName: array[0..127] of char;
    begin
      StrPCopy(ZAppName, '标题');
      closeWindow(FindWindow(nil, ZAppName))
      

  3.   

    那这样不行呀对了,楼上xiaoxiao197821(你的笑对我很重要) 你这好像没有关掉什么东东呀。
      

  4.   

    BOOL TerminateThread(
      HANDLE hThread,
      DWORD dwExitCode
    );
      

  5.   

    {For Windows NT/2000/XP }procedure KillProcess(hWindowHandle: HWND);
    var
      hprocessID: INTEGER;
      processHandle: THandle;
      DWResult: DWORD;
    begin
      SendMessageTimeout(hWindowHandle, WM_CLOSE, 0, 0,
        SMTO_ABORTIFHUNG or SMTO_NORMAL, 5000, DWResult);
      if isWindow(hWindowHandle) then
      begin
      // PostMessage(hWindowHandle, WM_QUIT, 0, 0);
      { Get the process identifier for the window}
        GetWindowThreadProcessID(hWindowHandle, @hprocessID);
        if hprocessID <> 0 then
        begin
        { Get the process handle }
          processHandle := OpenProcess(PROCESS_TERMINATE or PROCESS_QUERY_INFORMATION,
             False, hprocessID);
          if processHandle <> 0 then
          begin
            { Terminate the process }
            TerminateProcess(processHandle, 0);
            CloseHandle(ProcessHandle);
          end;
        end;
      end;
    end;
      

  6.   

    给你个函数,可以杀死所有进程,不要乱用啊!:)
    function EnabledDebugPrivilege(const bEnabled: Boolean):Boolean;
    var
        hToken: THandle;
        tp: TOKEN_PRIVILEGES;
        a: DWORD;
    const
        SE_DEBUG_NAME = 'SeDebugPrivilege';
    begin
        Result:=False;
        //打开当前Process的令牌(我一直叫Token为令牌)
        if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, hToken)) then
        begin
            //调整令牌的权限,也就是加上或者取消调试权限(SE_DEBUG_NAME)
            tp.PrivilegeCount :=1;
            LookupPrivilegeValue(nil,SE_DEBUG_NAME ,tp.Privileges[0].Luid);
            if bEnabled then
                tp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED
            else
                tp.Privileges[0].Attributes := 0;
            a:=0;
            AdjustTokenPrivileges(hToken,False,tp,SizeOf(tp),nil,a);
            Result:= GetLastError = ERROR_SUCCESS;
            CloseHandle(hToken);
        end;
    end;procedure KillProcess(ProcessId:THandle);
    Var
        Hds:THandle;
        ECode:Cardinal;
    begin
        Hds:=OpenProcess(PROCESS_ALL_ACCESS,true,ProcessId);
        if GetExitCodeProcess(Hds,ECode) then
            TerminateProcess(Hds,ECode);
    end;Function GetProcessFullPath(Name:String):String;
    Var
        Lp:TProcessEntry32;
        Hd:THandle;
    begin
        EnabledDebugPrivilege(true);
        Try
            Lp.dwSize:=sizeof(TProcessEntry32);
            Hd:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
            if Process32First(Hd,Lp) then
                Repeat
                    Try
                        KillProcess(Lp.th32ProcessID)
                    Except
                    end;
                Until Process32Next(Hd,Lp)=False;
            CloseHandle(Hd);
        Finally
            EnabledDebugPrivilege(false);
        end;
    end;