在Delphi中操作快捷方式
快捷方式减少了系统的重复文件,是快速启动程序或打开文件或文件夹的方法,快捷方式对经常使用的程序、文件和文件夹非常有用。在Windows系统中,充斥着大量的快捷方式,那么如何操作这些快捷方式就是一个很头疼的问题,在Windows的编程中,无疑会经常碰到操作快捷方式文件的问题,例如为程序创建快捷方式,修改程序的快捷方式等等。为了操作快捷方式,本人封装了两个函数,而且给出了一个详细的例子。
1. 快捷方式文件的基本信息
快捷方式包含的信息有:目标文件名、程序运行时的参数、快捷键、运行窗口的状态、描述、工作目录(起始位置)、图标文件名和图标索引等等。我们在操作快捷方式时,就要考虑到这些信息。
2. 数据结构
为了方便快捷地进行操作,有必要定义一个数据结构,以便在函数调用时传递必要的信息:
const
CCH_MAXNAME=255; //描述的缓冲区的大小
LNK_RUN_MIN=7; //运行时最小化
LNK_RUN_MAX=3; //运行是最大化
LNK_RUN_NORMAL=1; //正常窗口
type LINK_FILE_INFO=record
FileName:array[0..MAX_PATH] of char; //目标文件名
WorkDirectory:array[0..MAX_PATH] of char; //工作目录或者起始目录
IconLocation:array[0..MAX_PATH] of char; //图标文件名
IconIndex:integer; //图标索引
Arguments:array[0..MAX_PATH] of char; //程序运行的参数
Description:array[0..CCH_MAXNAME] of char; //快捷方式的描述
ItemIDList:PItemIDList; //只供读取使用
RelativePath:array[0..255] of char; //相对目录,只能设置
ShowState:integer; //运行时的窗口状态
HotKey:word; //快捷键
end;
3. 函数
1)取得快捷方式或者修改信息函数LinkFileInfo
要注意的是,需要在Uses部分添加comobj,activex,shlobj这三个单元,但是在本文例子中因为要转化热键为文本和使用Windows的API ShellExecute,所以还要添加Menus 和ShellApi单元。同时对异常处理不够完美,在使用时大家可以根据实际情况进行修改。
说明:函数有三个参数,其中第一个参数为要进行处理的快捷方式的文件名,第二个为LINK_FILE_INFO的结构,用于接收信息或者输入信息对快捷方式进行修改,第三个参数表明是读取快捷方式的信息还是设置快捷方式的信息。函数成功时返回true,否则为false。
原型:
function LinkFileInfo(const lnkFileName:string;var info:LINK_FILE_INFO;const bSet:boolean):boolean;
var
hr:hresult;
psl:IShelllink;
wfd:win32_find_data;
ppf:IPersistFile;
lpw:pwidechar;
buf:pwidechar;
begin
result:=false;
getmem(buf,MAX_PATH);
try
if SUCCEEDED(CoInitialize(nil)) then
if (succeeded(cocreateinstance(clsid_shelllink,nil,clsctx_inproc_server,IID_IShellLinkA,psl))) then
begin
hr:=psl.QueryInterface(iPersistFile,ppf);
if succeeded(hr) then
begin
lpw:=stringtowidechar(lnkfilename,buf,MAX_PATH);
hr := ppf.Load(lpw, STGM_READ);
if succeeded(hr) then
begin
hr := psl.Resolve(0, SLR_NO_UI);
if succeeded(hr) then
begin
if bSet then
begin
psl.SetArguments(info.Arguments);
psl.SetDescription(info.Description);
psl.SetHotkey(info.HotKey);
psl.SetIconLocation(info.IconLocation,info.IconIndex);
psl.SetIDList(info.ItemIDList);
psl.SetPath(info.FileName);
psl.SetShowCmd(info.ShowState);
psl.SetRelativePath(info.RelativePath,0);
psl.SetWorkingDirectory(info.WorkDirectory);
result:=succeeded(psl.Resolve(0,SLR_UPDATE));
end
else
begin
psl.GetPath(info.FileName,MAX_PATH, wfd,SLGP_SHORTPATH );
psl.GetIconLocation(info.IconLocation,MAX_PATH,info.IconIndex);
psl.GetWorkingDirectory(info.WorkDirectory,MAX_PATH);
psl.GetDescription(info.Description,CCH_MAXNAME);
psl.GetArguments(info.Arguments,MAX_PATH);
psl.GetHotkey(info.HotKey);
psl.GetIDList(info.ItemIDList);
psl.GetShowCmd(info.ShowState);
result:=true;
end;
end;
end;
end;
end;
finally
freemem(buf);
end;
end;
2)建立快捷方式函数CreateLinkFile
说明:第一个参数为一个LINK_FILE_INFO结构,你必须进行初始化,第二个参数为要保存快捷方式的文件名,默认为相同目录下的同名的LNK文件。
原型:
function CreateLinkFile(const info:LINK_FILE_INFO;const DestFileName:string=''):boolean;
var
anobj:IUnknown;
shlink:IShellLink;
pFile:IPersistFile;
wFileName:widestring;
begin
wFileName:=destfilename;
anobj:=CreateComObject(CLSID_SHELLLINK);
shlink:=anobj as IShellLink;
pFile:=anobj as IPersistFile;
shlink.SetPath(info.FileName);
shlink.SetWorkingDirectory(info.WorkDirectory);
shlink.SetDescription(info.Description);
shlink.SetArguments(info.Arguments);
shlink.SetIconLocation(info.IconLocation,info.IconIndex);
shlink.SetHotkey(info.HotKey);
shlink.SetShowCmd(info.ShowState);
shlink.SetRelativePath(info.RelativePath,0);
if DestFileName='' then
wFileName:=ChangeFileExt(info.FileName,'lnk');
result:=succeeded(pFile.Save(pwchar(wFileName),false));
end;
3)函数ShortCutToString用于将快捷方式中的热键转化成字符串,以便于显示,Delphi本身没有提供将相关的函数。
function ShortCutToString(const HotKey:word):string;
var
shift:tshiftstate;
begin
shift:=[];
if ((wordrec(HotKey).hi shr 0) and 1)<>0 then
include(shift,ssshift);
if ((wordrec(HotKey).hi shr 1) and 1)<>0 then
include(shift,ssctrl);
if ((wordrec(HotKey).hi shr 2) and 1)<>0 then
include(shift,ssalt);
result:=shortcuttotext(shortcut(wordrec(hotkey).lo,shift));
end;
4. 调用实例:
新建一个Application,在窗体上放置一个OpenDialog,三个Button,一个Edit,设置OpenDialog的属性Option部分的ofNoDereferenceLinks为true,Filter属性为lnk file|*.lnk;然后为Button1、Button2和Button3分别添加如下代码(注意修改相应的文件名称和路径):
procedure TForm1.Button1Click(Sender: TObject);
var
info:LINK_FILE_INFO;
begin
if opendialog1.Execute then
if LinkFileInfo(opendialog1.FileName,info) then
begin showmessage('FileName:'+info.filename+#13+'Description:'+info.Description+#13+'IconFilename:'+info.IconLocation+','+inttostr(info.IconIndex)+#13+'WorkDir:'+info.WorkDirectory+#13+'Arguments:'+info.Arguments+#13+'ShorCuts:'+shortcuttostring(info.HotKey)+#13+'WindowState:'+inttostr(info.ShowState)+#13+'ItemIDList:('+inttostr(info.itemidlist.mkid.cb)+',('+inttostr(info.itemidlist.mkid.abid[0])+'))');
info.WorkDirectory:=edit1.text;
linkfileinfo(opendialog1.filename,info,true);
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
shellexecute(handle,'open','start.exe','c:\windows\desktop\aaa.lnk','',sw_hide);
end;
procedure TForm1.Button3Click(Sender: TObject);
var
info:LINK_FILE_INFO;
begin
strpcopy(info.filename,paramstr(0)); //快捷方式的目标文件名
info.Arguments:='/paramstr'; //设置程序运行的参数
info.Description:='Test For Link File'; //描述
info.HotKey:=0; //没有热键
info.IconIndex:=0; //第一个图标
info.IconLocation:=''; //设置图标文件为本身
info.RelativePath:='windows'; //相对路径
info.ShowState:=LNK_RUN_MAX; //显示最大化窗口
info.ItemIDList:=nil; //保留未用,可以不设置
strpcopy(info.WorkDirectory,extractfilepath(paramstr(0))); //设置工作目录
createLinkFile(info,'c:\windows\desktop\project.lnk'); //建立快捷方式
end;
5. 快捷方式的运行
有两种方法:
第一种比较简单,直接调用即可,但是受到一些制约—系统中必须存在start.exe文件:
ShellExecute(handle,'open','start.exe','c:\windows\desktop\aaa.lnk','',sw_hide);
第二种就是取得LNK的信息之后用ShellExecute进行调用!为简单起见,不给出详细的例子了。
在Delphi中操作快捷方式的更多相关文章
- DELPHI中的快捷方式一览(完全版)
1.SHIFT+鼠标左键 先选中任一控件,按键后可选中窗体(选中控件后按Esc效果一样)2.Shift+F8 调试时弹出CPU窗口.3.Shift+F10 等于 ...
- ACCESS-关于DELPHI中操作ACCESS数据库中单精度数据的问题
在近日几个帖子里面,和QQ群的讨论里面,我发现很多网友都遇到的问题都是因为不恰当地使用了单精度/双精度数值.因此想专门就这个话题谈一下. 单精度和双精度数值类型最早出现在C语言中(比较通用的语言里面) ...
- Delphi中exit、break、continue等跳出操作的区别
Delphi中表示跳出的有break,continue,abort,exit,halt,runerror等 1.break 强制退出最近的一层循环(注意:只能放在循环里:而且是只能跳出最近的一层循环) ...
- Delphi中文本文件的操作
Delphi中文本文件的操作 相关知识内容: 在对文本文件进行任何处理之前,首先要打开此文本文件.声明变量:通过此变量可以来引用一个文本文件. 打开一个文件需要两步:首先是 AssignFile(), ...
- delphi 线程教学第二节:在线程时空中操作界面(UI)
第二节:在线程时空中操作界面(UI) 1.为什么要用 TThread ? TThread 基于操作系统的线程函数封装,隐藏了诸多繁琐的细节. 适合于大部分情况多线程任务的实现.这个理由足够了吧 ...
- Delphi中的操作二进制文件的两个重要函数
Delphi中的操作二进制文件的两个重要函数 对于通过Byte数组进行文件操作的,在FTP中经常会使用到,我也是在Delphi调用Web Service进行文件的上传和下载时找到这两个函数的,挺好用的 ...
- delphi中我用定时器每隔一段时间执行操作
delphi中,我用定时器每隔一段时间执行数据库插入及更新工作!adoquery.close;adoquery.sql.cleare;adoquery.connection:=con1;adoquer ...
- 在Delphi中如何控制其它应用程序窗口
在编写Delphi的应用程序中,常常涉及对其它Windows应用程序的操作.例如,在数据库的管理系统中,财务人员需要使用计算器,即可调用Windows内含的计算器功能,若每次使用,均通过“开始/程序/ ...
- [转]Delphi中ShellExecute的妙用
Delphi中ShellExecute的妙用 ShellExecute的功能是运行一个外部程序(或者是打开一个已注册的文件.打开一个目录.打印一个文件等等),并对外部程序有一定的控制. ...
随机推荐
- 编程入门python之定义函数【转】
编程入门python之定义函数 零基础学编程by学哥 2017-02-06 10:51 今天讲python函数. 输入参数求三角形或圆形或长方形的面积 先输入1个参数:形状类型 1=三角形 2=圆形 ...
- web.js
var page = require('webpage').create(), system = require('system'), address,output,csvPath,nodePathF ...
- ASP.NET MVC3 Model的常用验证示例
1.金额(10位整数,2位小数) #region 余额 /// <summary> /// 余额 /// </summary> [DisplayName("余额&qu ...
- Linux umount的device is busy问题
现象: [root@dbserver ~]# df -h文件系统 容量 已用 可用 已用%% 挂载点/dev/vda1 9.9G 3.9G 5.6G 41% /tmpfs 3.9G 100K 3.9G ...
- pwd、ln和重定向命令
pwd命令 命令功能: 使用pwd命令可以显示当前的工作目录,该命令很简单,直接输入pwd即可,后面不带参数. pwd命令以绝对路径的方式显示用户当前工作目录.命令将当前目录的全路径名称(从根 ...
- JavaEE之JavaWeb简介
- wpf 自定义控件展开popup,点击popup之外的部分,popup不能自动关闭
比如textbox点击展开popup,这样popup也是不能自动关闭的.可能是textbox获得了焦点. 可是使用textblock,或者ToggleButton来代替textbox点击展开popup ...
- TcxGrid 选中 整行
- SCU 4438:Censor
Censor frog is now a editor to censor so-called sensitive words (敏感词). She has a long text p . Her j ...
- python强大的数据类型转换
# 原始的二维表数据集 jsonObj=[] # 添加模拟的数据 for i in range(1001,1004): for j in range(1,34): jsonObj.append({&q ...