【温故Delphi】双击工程文件打开软件

问题描述

  大部分软件都有鼠标双击文件,就能打开所关联的软件并打开所选工程,这是如何做到的呢?

  把文件关联到一个程序中,双击文件来启动程序,那么这个文件名称就是这个程序的命令行的一个参数

  所以要想实现双击工程文件打开软件,在程序运行入口必须要处理命令行。

代码说明  

 1 begin
 2   Application.Initialize;
 3   CreateMainForm();
 4   //GetCommandLine为Win32 API,用于获取命令行内容
 5   HandleCommandLine(GetCommandLine);
 6   if System.ParamCount = 0 then
 7     ExecuteApplication();
 8   Application.Run;
 9 end.
10 
11 procedure HandleCommandLine(const ACommandLine: string);
12 begin
13   // 解析参数个数:没有参数,直接退出
14   if ParamCount(ACommandLine) = 0 then Exit;
15 
16   S := ParamStr(ACommandLine, 1);
17 if IsOpenFileMode(S) then 19 begin 20 // 判断当前是否能够打开新工程 21 if not AppInModelState then 22 OpenProjFile(GetLongFileName(S)) 23 else 24 ShowPrompt('请关闭当前窗体,再重新双击打开工程'); 25 end; 26 end;