在主窗体的OnCreate事件中:
procedure TmainForm.Oncreate(....);
begin
if Not Login(...) then
Halt(1)
else
......
end;
或者将Halt(1)该为Application.terminate,
uses ShellAPI; ShellExecute( hwmd, //父窗口句柄 LPCSTR lpszOp;//要执行操作窜的地址 可以为open ,print默认为open LPCSTR lpszFile;//文件名窜的地址 LPCSTR lpszParams;//执行文件串的地址 LPCSTR lpszDir;//默认目录串的地址 int fsShowCmd;//打开时文件是否显示 ); ---------------------- WinExec( LPCSTR lpCmdLine, // address of command line UINT uCmdShow // window style for new application ); ----------------------- ShellExecute(handle, "open", path_to_folder, NULL, NULL, SW_SHOWNORMAL); 调用的时候 ShellExecute(Handle,"Open",FileName(文件名),Param(参数),Dirction(路径),SW_shownormal); |
gsflove (2002-10-24 10:26) |
[b]启动外部程序并等待它结束 [/b] 如果在你的软件中留下了你的网页地址和邮件地址,你肯定希望人们点击它就会启动浏览器或者电子邮件软件。这其实就是如何启动外部软件的问题,很简单,不是吗?不过,如果我问你,如何启动外部程序并等待它结束,你还能告诉我吗? 其实,这是一个“古老”的话题,在WIN95时代就被讨论过了。不过,既然这么多人不知道,我感觉还是有必要再讨论一下。 一、为什么要启动外部程序 也许,你想让你的程序完成全部的功能。不过,无论从物力还是人力上,你都应养成资源共享的习惯。更好的考虑是,充分利用已有的程序,而让你的程序专注于某一方面的功能。比如说,浏览器负责打开网页,让人们浏览,当遇到下载的任务时,可以交给更专业的下载软件去做。你也可能在你的程序里留下了你的主页和邮箱地址,你希望有人点击它们时就分别启动浏览器和电子邮件。在某些情况下,你需要外部程序处理后,再进行下一步的工作,这时就会遇到启动外部程序并等待它结束的问题。 二、预备知识 启动外部程序我们可以使用函数Winexec、ShellExecute和ShellExecuteEx。我推荐大家使用函数ShellExecute,因为它既灵活,又简单。看看下面的例子,用法就清楚了: *: 启动一个程序 ShellExecute(Handle,'open',PChar('c:/test/app.exe'), nil,nil,SW_SHOW); * 启动记事本 (因为记事本在系统路径下,所以不必写完整的路径名了): ShellExecute(Handle, 'open', PChar('notepad'), nil, nil, SW_SHOW); * 启动记事本并加载一个纯文本文件: ShellExecute(Handle, 'open', PChar('notepad'), PChar('c:/test/readme.txt', nil, SW_SHOW); * 使用记事本打开一个纯文本文件 (请确定*.txt文件被关联到记事本): ShellExecute(Handle, 'open', PChar('c:/test/readme.txt'), nil, nil, SW_SHOW); * 使用默认浏览器打开网址: ShellExecute(Handle, 'open', PChar('http://www.festra.com/'), nil, nil, SW_SHOW); * 打印一个文件: ShellExecute(Handle, 'print', PChar('c:/test/readme.txt'), nil, nil, SW_SHOW); * 用Windows Explorer打开一个文件夹: ShellExecute(Handle, 'explore', PChar('c:/windows)', nil, nil, SW_SHOW); * 运行一个DOS命令并立即返回: ShellExecute(Handle, 'open', PChar('command.com'), PChar('/c copy file1.txt file2.txt'), nil, SW_SHOW); * 运行一个DOS命令并保持DOS窗口打开 ("stay in DOS"): ShellExecute(Handle, 'open', PChar('command.com'), PChar('/k dir'), nil, SW_SHOW); 启动一个外部程序并不难吧?不过,我们如何知道它是否运行结束了呢?我们的程序又怎样等待它结束呢? 三、启动外部程序并等待它结束的函数 我们可以通过进程句柄(process handle)来查看进程(程序)是否结束。为了得到进程句柄,有两个Win32 API函数可以利用:ShellExecuteEx 或者CreateProces。解决这个问题最简单的方法是,使用ShellExecuteEx启动一个外部程序,然后使用WaitForSingleObject管理这个外部程序的进程句柄。我们可以这样定义一个函数: …… { ExecAppWait:功能:运行外部程序并等待它结束。。 运行外部程序APPNAME,参数PARAMS; Returns:如果外部程序出错返回 FASLE } function ExecAppWait(AppName, Params: string): Boolean ; …… function ExecAppWait(AppName, Params: string): Boolean; var // Structure containing and receiving info about application to start ShellExInfo: TShellExecuteInfo; begin FillChar(ShellExInfo, SizeOf(ShellExInfo), 0); with ShellExInfo do begin cbSize := SizeOf(ShellExInfo); fMask := see_Mask_NoCloseProcess; Wnd := Application.Handle; lpFile := PChar(AppName); lpParameters := PChar(Params); nShow := sw_ShowNormal; end; Result := ShellExecuteEx(@ShellExInfo); if Result then while WaitForSingleObject(ShellExInfo.HProcess, 100) = WAIT_TIMEOUT do begin Application.ProcessMessages; if Application.Terminated then Break; end; end; …… 不难理解吧? |