笔记本电脑键盘不能用了-常用杀毒软件推荐
2023年4月2日发(作者:cad2006免费下载)
⽤代码实现为程序创建快捷⽅式的⼆种⽅法
有时,我们会编⼀些⼩的⼯具软件,不需要安装就能直接使⽤的那种所谓的绿⾊软件。这种软件不需要安装,但有时我们⼜需要他能在桌⾯或Windows开始菜单的程
序组⾥能有⼀个快捷⽅式。那么就可能需要⼯具软件本⾝来直接为程序创建⼀个桌⾯或程序组的快捷⽅式。
在Windows操作系统⾥,桌⾯的快捷⽅式有两种,⼀种是以.lnk为后缀名的⽂件快捷⽅式。另⼀种是保存在注册表⾥的快捷⽅式,如MSWord的快捷⽅式。这种
⽅式有定的封密性,可以防⽌⽤户修改或删除快捷⽅式⾥的内容。这⾥我将⼆种⽅法分别给⼤家说明⼀下,并贴出在DELPHI中的实现代码。⽅便有⽤的朋友直接
使⽤。
//以下代码需要引⽤的单元⽂件。
usesRegistry,ShlObj,ComObj,ShellAPI,ActiveX;
第⼀种:快捷⽅式⽂件的⽅法。
函数CreateLinkFile通过使⽤Windows的COM对象,调⽤CreateComObject(CLSID_ShellLink)来创建⼀个⼦IShellLink的COM接⼝对象。现使⽤IPersistFile来保
存创建的⽂件。
IShellLink与IPersistFile在MSDN的类描述如下,如果我们掌握了这两个类,也可以⽅便地实现快捷⽅式⽂件的读取。
IShellLink
TheIShellLinkinterfaceallowsShelllinkstobecreated,modified,andresolved.
Methods
IShellLinksupportsthefollowingmethods:
GetArgumentsRetrievesthecommand-lineargumentsassociatedwithaShelllinkobject.
GetDescriptionRetrievesthedescriptionstringforaShelllinkobject.
GetHotkeyRetrievesthehotkeyforaShelllinkobject.
GetIconLocationRetrievesthelocation(pathandindex)oftheiconforaShelllinkobject.
GetIDListRetrievesthelistofitemidentifiersforaShelllinkobject.
GetPathRetrievesthepathandfilenameofaShelllinkobject.
GetShowCmdRetrievestheshow(SW_)commandforaShelllinkobject.
GetWorkingDirectoryRetrievesthenameoftheworkingdirectoryforaShelllinkobject.
ResolveResolvesaShelllinkbysearchingfortheShelllinkobjectandupdatingtheShelllinkpathanditslistofidentifiers(ifnecessary).
SetArgumentsSetsthecommand-lineargumentsassociatedwithaShelllinkobject.
SetDescriptionSetsthedescriptionstringforaShelllinkobject.
SetHotkeySetsthehotkeyforaShelllinkobject.
SetIconLocationSetsthelocation(pathandindex)oftheiconforaShelllinkobject.
SetIDListSetsthelistofitemidentifiersforaShelllinkobject.
SetPathSetsthepathandfilenameofaShelllinkobject.
SetRelativePathSetstherelativepathforaShelllinkobject.
SetShowCmdSetstheshow(SW_)commandforaShelllinkobject.
SetWorkingDirectorySetsthenameoftheworkingdirectoryforaShelllinkobject.
Remarks
Note:TheIShellLinkinterfacehasanANSIversion(IShellLinkA)andaUnicodeversion(IShellLinkW).Theversionthatwillbeuseddependsonwhetheryou
r,Microsoft®Windows95andMicrosoft®Windows98onlysupportIShellLinkA.
IPersistFile
TheIPersistFileinterfaceprovidesmethodsthatpermitanobjecttobeloadedfromorsavedtoadiskfile,ethe
informationneededtoopenafilevariesgreatlyfromoneapplicationtoanother,theimplementationofIPersistFile::Loadontheobjectmustalsoopenitsdiskfile.
TheIPersistFileinterfaceinheritsitsdefinitionfromIPersist,soallimplementationsmustalsoincludetheGetClassIDmethodofIPersist.
WhentoImplement
ImplementIPersistFilewhenyouwanttoreadorwriteinformationfromaseparatefile,whichcouldbeofanyfileformat.
Thisinterfaceshouldbeimplementedonanyobjectsthatsupportlinkingthroughafilemoniker,includingthefollowing:
Anyobjectthatsupportslinkstoitsfilesortopseudo-objectswithinitsfiles.
Acontainerapplicationthatsupportslinkstoobjectswithinitscompoundfile.
Typically,youimplementtheIPersistFileinterfaceaspartofanaggregateobjectthatincludesotherinterfacesthatareappropriateforthetypeofmonikerbinding
thatissupported.
Forexample,ineitherofthecasesmentionedabove,irstcase,acompositemonikeridentifies
econdcase,acompositemer
caseofcompositemonikers,youmustimplementtheIPersistFileinterfaceaspartofthesameobjectonwhichtheIOleItemContainerinterfaceisimplemented.
Then,whentheapplicationforthelinkedobjectisrun,OLEqueriesfortheIOleItemContainerinterfacetolocatetheembeddedobjectorthepseudo-object
containedinthefile.
Asanotherexample,ifthemonikerisasimplefilemoniker(i.e.,thelinkistotheentirefile),OLEqueriesfortheinterfacethattheinitiatorofthebindoperation
lly,thisisoneofthecompounddocumentinterfaces,suchasIOleObject,IDataObject,orIPersistStorage.
WhentoUse
CallmethodsintheIPersistFileinterfacetoloadorsavealinkedobjectinaspecifiedfile.
WhenIPersistFileisimplementedonanobjectthatsupportslinkingthroughafilemonikerandtheapplicationforthelinkedobjectisrun,OLEcalls
IPersistFile::efileisloaded,OLEcallsIPersistFile::rsistFileinterfaceis
typicallypartofanaggregateobjectthatoffersotherinterfaces.
Inthiscase,theonlyIPersistFilemethodthatOLEcallsistheLoadmethodtoloadafilelinkedtoacontainer,
wouldalsobeunusualforapplicationstocallothermethodsinthiscase,lly,itislefttotheenduserandthe
ffersfromthesituationforanembeddedobject,inwhichthecontainerapplicationuses
theIPersistStorageinterfacetoprovidethestorageandtotelltheobjectwhentosaveitself.
MethodsinVtableOrder
IUnknownMethodsDescription
QueryInterface
Returnspointerstosupported
interfaces.
AddRefIncrementsthereferencecount.
ReleaseDecrementsthereferencecount.
IPersistMethodDescription
GetClassIDReturnstheclassidentifier(CLSID)forthecomponentobject.
IPersistFileMethodsDescription
IsDirtyChecksanobjectforchangessinceitwaslastsavedtoitscurrentfile.
LoadOpensthespecifiedfileandinitializesanobjectfromthefilecontents.
SaveSavestheobjectintothespecifiedfile.
SaveCompletedNotifiestheobjectthatitcanrevertfromNoScribblemodetoNormalmode.
GetCurFileGetsthecurrentnameofthefileassociatedwiththeobject.
CreateLinkFile的实现代码如下,熟悉DELPHI的朋友也可以根据MSDN中IShellLink的说明,实现为快捷⽅式设置快捷键,窗⼝运⾏⽅式等,CreateLinkFile只
是提供了最基本的快捷⽅式模式。
{CreateLinkFile}
procedureCreateLinkFile(SourceFile,ShortCutName,Description:String);
var
IUnk:IUnknown;
ShellLink:IShellLink;
IPFile:IPersistFile;
tmpShortCutName:string;
WideStr:WideString;
i:integer;
begin
ifSUCCEEDED(CoInitialize(nil))then
begin
IUnk:=CreateComObject(CLSID_ShellLink);
ShellLink:=IUnkasIShellLink;
IPFile:=IUnkasIPersistFile;
withShellLinkdo
begin
SetPath(PChar(SourceFile));//执⾏程序的⽂件名
SetDescription(PChar(Description));//提⽰说明⽂本
SetWorkingDirectory(PChar(ExtractFilePath(SourceFile)));//启动⽬录
end;
ShortCutName:=ChangeFileExt(ShortCutName,'.lnk');
ifFileExists(ShortCutName)then//如果⽂件名存在,就以数据序号来重新命名⼀个新的⽂件名
begin
ShortCutName:=Copy(ShortCutName,1,Length(ShortCutName)-4);
i:=1;
repeat
tmpShortCutName:=ShortCutName+'('+IntToStr(i)+').lnk';
Inc(i);
untilnotFileExists(tmpShortCutName);
WideStr:=tmpShortCutName;
end
elseWideStr:=ShortCutName;
(PWChar(WideStr),False);
CoUninitialize;
end;
end;
第⼆种:通过注册表创建快捷⽅式的⽅法。
函数:CreateShortCutOnDeskTop;这种⽅法主要是通过写注册表来实现。通过这种⽅式实现的快捷⽅式具有⼀定的封装性。⼀般⽤在桌⾯上的快捷⽅式。
这种快捷⽅式必须要有⼀个GUID,⼦GUID可以通过CoCreateGuid来取得,如
var
FShortCutID:string
AGuid:TGUID;
begin
if(CoCreateGuid(AGuid)=S_OK)then
FShortCutID:=GuidToString(AGuid);
end;
CreateShortCutOnDeskTop的实现代码如下:
const
REG_ROOTRUN_KEY='Software/Microsoft/Windows/CurrentVersion/Run';
REG_ROOTRUN_STR='我的快捷⽅式';//你可以修改这个名称为⾃⼰合适的名称
{CreateShortCutOnDeskTop}
{参数说明
aTitle:快捷⽅式名称
aInfoTip:快捷⽅式提⽰说明⽂本
aCmdLine:执⾏程序⽂件名
aIcon:图标⽂件
FShortCutID:快捷⽅式的GUID必须有
}
functionCreateShortCutOnDeskTop(aTitle,aInfoTip,aCmdLine,aIcon,FShortCutID:String):Boolean;
var
FRegFile:TRegIniFile;
RegStr:String;
TempKey:HKey;
Buffer,BufSize,Disposition:Integer;
begin
FRegFile:=('HKEY_LOCAL_MACHINE/');
RegStr:='.DEFAULT/Software/Classes/CLSID/'+FShortCutID;
y:=HKEY_USERS;
Key(RegStr);
tring(RegStr,'',aTitle);
RegStr:='Software/Microsoft/Windows/CurrentVersion/explorer/Desktop/NameSpace/'+FShortCutID;
y:=HKEY_LOCAL_MACHINE;
Key(RegStr);
tring(RegStr,'',aTitle);
RegStr:='Software/CLASSES/CLSID/'+FShortCutID;
Key(RegStr);
tring(RegStr,'',aTitle);
tring(RegStr,'InfoTip',aInfoTip);
Key(RegStr+'/DefaultIcon');
tring(RegStr+'/DefaultIcon','',aIcon);
Key(RegStr+'/shell/open/command');
tring(RegStr+'/shell/open/command','',aCmdLine);
Key(RegStr+'/ShellFolder');//'Attributes'
tring(RegStr+'/ShellFolder','Attributes','1000000');
;
TempKey:=0;
try
RegCreateKeyEx(HKEY_LOCAL_MACHINE,PChar(RegStr+'/ShellFolder'),0,nil,
REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,nil,TempKey,@Disposition);
Bufsize:=Sizeof(Integer);
Buffer:=311;{137}{0x20000137}
if(RegSetValueEx(TempKey,PChar('Attributes'),0,REG_DWORD,@Buffer,
BufSize)<>ERROR_SUCCESS)thenResult:=False
elseResult:=True;
finally
//更新桌⾯显⽰,将刚创建的快捷⽅式⽴即显⽰出来
SHChangeNotify(SHCNE_ASSOCCHANGED,SHCNF_IDLIST,nil,nil);
end;
end;
⽰范:
var
ShortcutId:string;
AGuid:TGUID;
begin
if(CoCreateGuid(AGuid)=S_OK)then
ShortcutId:=GuidToString(AGuid);
ifShortcutId<>''then
CreateShortCutOnDeskTop(n,,
e,e,ShortcutId);
end;
更多推荐
msdn i tell you
发布评论