如何在桌面上建立快捷方式,并指定快捷键。 ( 积分: 100 )

W

warket

Unregistered / Unconfirmed
GUEST, unregistred user!
如何在桌面上建立快捷方式,并指定快捷键。建立快捷方式我到知道,但是如何在程序里面指定其运行的快捷键呢?
建立快捷方式如下。
uses … , ShlObj, ActiveX, ComObj;
procedure CreateShellLink(const DestPath, LinkName, LinkAppPath,
LinkArgs, Description: String);
Var
aObj: IUnknown;
WFileName: WideString;
Begin
aObj := CreateComObject(CLSID_ShellLink);
With aObj as IShellLink do begin
{对MS-DOS程序,一般建议使用SetShowCmd(SW_SHOWMAXIMIZED);}
SetShowCmd(SW_NORMAL);
SetArguments(Pchar(LinkArgs));
SetDescription(Pchar(Description));
SetPath(Pchar(LinkAppPath));
SetWorkingDirectory(Pchar(ExtractFilePath(LinkAppPath)));
End;
{将一个String赋给WideString,转换过程由Delphi自动完成}
WFileName := DestPath + '/' + LinkName;
(aObj as IPersistFile).Save(PWChar(WFileName), False);
End;
使用CreateShellLink过程要保证路径文件名参数正确,如下:
CreateShellLink('C:/Pwin98/Desktop', '快捷方式名', 'C:/Command.com',
'', '简短描述');
CreateShellLink('C:/Pwin98/Desktop', 'Win32程序.Lnk', 'D:/Setup.exe', '', '');
 
自己用的,暂时没出错过

uses
Variants, SysUtils, ComObj;

{ ShortCut }

{*
* 函数名: ResolveShortCut
* 功 能: 创建文件快捷方式至指定位置
* 参 数:
* WindowStyle: Byte 运行方式,有效值:1, 3, 7;
* FullName: string 快捷方式文件名
* * 如果RelativePath是Special Forder 常数,则取代FullName的路径
* RelativePath: string 快捷方式的相对路径
* TargetPath: string 目标(要创建快捷方式的文件名)
* IconLocation: string 图标文件,也可以是exe文件
* WorkDir: string 起始位置
* Description: string 备注
* HotKey: Char 快捷键,不区分大小写
* 返回值:如果成功返回快捷方式的完整路径文件,否则返回错误信息
*}

function ResolveShortCut(const WindowStyle: Byte;
const FullName, RelativePath, TargetPath, IconLocation,
WorkDir, Description: string; const HotKey: Char): string;
var
ShellObj, ShortCutObj: Variant;
begin
if not FileExists(TargetPath) then
begin
Result := '未找到快捷方式的目标文件'#13 + TargetPath;
Exit;
end;

ShellObj := CreateOleObject('WScript.Shell');
Result := ShellObj.SpecialFolders.Item(RelativePath);
if Result <> '' then
Result := Result + '/' + ExtractFileName(FullName)
else Result := FullName;

ShortCutObj := ShellObj.CreateShortCut(ChangeFileExt(Result, '.lnk'));
ShortCutObj.WindowStyle := WindowStyle;
ShortCutObj.TargetPath := TargetPath;
if IconLocation <> '' then
ShortCutObj.IconLocation := IconLocation;
ShortCutObj.WorkingDirectory := WorkDir;
ShortCutObj.Description := Description;
if HotKey <> #0 then
ShortCutObj.HotKey := 'Ctrl+Alt+' + HotKey;
ShortCutObj.Save;

Result := ShortCutObj.FullName;
ShortCutObj := UnAssigned;
ShellObj := UnAssigned;
end;
 
不错,谢谢。就是参数太多了:)
 
顶部