判断一个程序是否出现在任务
当我启动程序的时候,我用FindWindow可以先找到程序窗口的句柄,但是程序在任务还没有出现,我该怎么样判断程序是否已经在任务栏出现呢?
[解决办法]
找进程名。。
[解决办法]
在项目unit 中,判断程序是否已经执行。
var
handle:integer; {变量}
begin
{-----------------主要为该判断部分,以免重复执行程序 ----------------------}
handle:=findwindow('Tmain',nil);{查找是否有此类的窗体}
if handle<>0 then {不为0则程序已运行}
begin
Application.MessageBox('该系统已经运行。','提示',MB_ICONINFORMATION);
//halt; {退出程序}
end;
//下面代码实现图标在任务栏隐藏,并把图标添加到最右侧时钟显示区域。即系统托盘添加图标
procedure TMain.FormCreate(Sender: TObject);
var
re_id:integer;
registerTemp : TRegistry;
inputstr,get_id:string;
dy,clickedok:boolean;
lpData: PNotifyIconData;
begin
{windows.SetParent(Self.Handle,FindWindowEx(FindWindow('Progman',nil),0,'shelldll_defview',nil));// 将窗口设置为屏幕的子窗口
keybd_event(91,0,0,0);//显示桌面
keybd_event(77,0,0,0);
keybd_event(77,0,KEYEVENTF_KEYUP,0);
keybd_event(91,0,KEYEVENTF_KEYUP,0);
//如果用户最小化窗口则将窗口隐藏并在任务栏上添加图标}
lpData := new(PNotifyIconDataA);
lpData.cbSize := 88;
//SizeOf(PNotifyIconDataA);
lpData.Wnd := Main.Handle;
lpData.hIcon := Application.Icon.Handle;
lpData.uCallbackMessage := WM_BARICON;
lpData.uID := 0;
lpData.szTip := '信息管理系统';
lpData.uFlags := NIF_ICON
or NIF_MESSAGE or NIF_TIP;
Shell_NotifyIcon(NIM_ADD, lpData);
dispose(lpData);
SetWindowLong(Application.Handle,GWL_EXSTYLE,WS_EX_TOOLWINDOW); //在任务栏不可见程序
end;
[解决办法]
不能基于窗口寻找,要基于进程寻找
[解决办法]
if FindProcess('QQ.exe') then
ShowMessage('OK');
function FindProcess(AFileName: string): Boolean;
var
hSnapshot: THandle;
lppe: TProcessEntry32;
Found: Boolean;
begin
Result := False;
hSnapshot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
lppe.dwSize := SizeOf(TProcessEntry32);
Found := Process32First(hSnapshot, lppe);
while Found do
begin
if ((UpperCase(ExtractFileName(lppe.szExeFile)) = UpperCase(AFileName)) or
(UpperCase(lppe.szExeFile ) = UpperCase(AFileName))) then
begin
Result := True;
Break;
end;
Found := Process32Next(hSnapshot, lppe);
end;
end;
[解决办法]
4楼办法是对的,不能用窗口标题去查找,即使找到也不一定是对的。可能有重名的窗体,找进程肯定不会出错了!