delphi 定时关闭其他应用程序

delphi 定时关闭其他应用程序,第1张

只是让浏览器空闲一分钟这不好编写,如果是让计算机空闲一分钟这还好办.

进程名返回进程ID函数:

function FindProcessID(s:string):integer

var

found,find:boolean

FSnapshotHandle:tHANDLE

lppe:TProcessEntry32

begin

FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)//CreateToolhelp32Snapshot函数得到进程快照

Find:=False

lppe.dwSize := Sizeof(lppe)//初始化

found := Process32First(FSnapshotHandle, lppe)//Process32First 得到一个系统快照里第一个进程的信息

while found do

begin

if LowerCase(ExtractFileName(lppe.szExeFile))=LowerCase(s) then

begin

Result:=lppe.th32ProcessID//找到进程返回ID

find:=true

CloseHandle(FSnapshotHandle)

exit

end

found := Process32Next(FSnapshotHandle, lppe)

end

CloseHandle(FSnapshotHandle)

if find=False then

Result:=0//找不到进程返回0

end

关闭进程:

procedure TForm1.Button1Click(Sender: TObject)

var

id: Cardinal

ph: THandle

ExitCode: DWORD

begin

GetWindowThreadProcessId(FindProcessID('360SE.exe'), id)

ph := OpenProcess(PROCESS_TERMINATE, False, id)

GetExitCodeProcess(ph, ExitCode)

TerminateProcess(ph, ExitCode)

end

不过可以检测浏览器的标题,如果浏览器在浏览网页那么标题会变动那么表示浏览器处于使用状态带知.

这样就要用返回应用程序主窗口标题的函数了,这里我又编了一个枚举窗体的回调函数:

function EnumWindowsProc(hwnd:HWNDlParam:DWORD):booleanstdcall

var

szCaption: array[0..256] of Char

c,h:integer

begin

GetWindowText(hwnd,szCaption,127)

if length(szCaption)>0 then

begin

if trim(szCaption)<>'' then

if pos('这里写浏览器的几个相同标题',LowerCase(szCaption))>0 then

在这里可以记录下该窗体的标题用于下次判断窗口标题是否渗旅变化. szCaption就丛行凳是标题内容.

end

result:=TRUE

end

开始枚举窗体:这里你可以把代码写到Timer中

procedure TForm1.Button2Click(Sender: TObject)

begin

EnumWindows(@EnumWindowsProc,0)

end

---------------

我回答这么详细楼主再不给分那就太不厚道了

用下面这个函数可以解决你的问题:

function  WinExecAndWait32(FileName:String  Visibility  :  integer):  DWORD  

var  

        zAppName:array[0..512]  of  char

        zCurDir:array[0..255]  of  char

        WorkDir:String

    租携山    StartupInfo:TStartupInfo

        ProcessInfo:TProcessInformation

begin

        StrPCopy(zAppName,FileName)

        GetDir(0,WorkDir)

        StrPCopy(zCurDir,WorkDir)

        FillChar(StartupInfo,Sizeof(StartupInfo),#0)

        StartupInfo.cb  :=  Sizeof(StartupInfo)

        StartupInfo.dwFlags  :=  STARTF_USESHOWWINDOW

        StartupInfo.wShowWindow  :=  Visibility

        if  not  CreateProcess(

        nil,

        zAppName,  {  pointer  to  command  line  string  }

  隐手      nil,  {  pointer  to  process  security  attributes  }

        nil,  {  pointer  to  thread  security  attributes  }

        false,  {  handle  inheritance  flag  }

        CREATE_NEW_CONSOLE  or  {  creation  flags  }

        NORMAL_PRIORITY_CLASS,

        nil,  {  pointer  to  new  environment  block  }

        nil,  {  pointer  to  current  directory 弊中 name  }

        StartupInfo,  {  pointer  to  STARTUPINFO  }

        ProcessInfo  {  pointer  to  PROCESS_INF  }

        )

        then  Result  :=  $FFFFFFFF  else  begin

                WaitforSingleObject(ProcessInfo.hProcess,INFINITE)

                GetExitCodeProcess(ProcessInfo.hProcess,Result)

        end

end


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/8193174.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-14
下一篇2023-04-14

发表评论

登录后才能评论

评论列表(0条)

    保存