在程序中添加(开始菜单和桌面的快接方式)(50分)

  • 主题发起人 主题发起人 confidentbb
  • 开始时间 开始时间
C

confidentbb

Unregistered / Unconfirmed
GUEST, unregistred user!
在程序中添加(开始菜单和桌面的快接方式)<br>谢谢<br>
 
var<br>tmpObject : IUnknown;<br>tmpSLink : IShellLink;<br>tmpPFile : IPersistFile;<br>PIDL : PItemIDList;<br>StartupDirectory : array[0..MAX_PATH] of Char;<br>StartupFilename : String;<br>LinkFilename : WideString;<br>begin<br>StartupFilename := 'c:/windows/notepad.exe';<br>tmpObject := CreateComObject(CLSID_ShellLink);//创建建立快捷方式的外壳扩展<br>tmpSLink := tmpObject as IShellLink;//取得接口<br>tmpPFile := tmpObject as IPersistFile;//用来储存*.lnk文件的接口<br>tmpSLink.SetPath(pChar(StartupFilename));//设定notepad.exe所在路径<br>tmpSLink.SetWorkingDirectory(pChar(ExtractFilePath(StartupFilename)));//设定工作目录<br>SHGetSpecialFolderLocation(0,<br>CSIDL_DESKTOPDIRECTORY,<br>PIDL);//获得桌面的Itemidlist<br>SHGetPathFromIDList(PIDL,<br>StartupDirectory);//获得桌面路径<br>LinkFilename := StartupDirectory + '/MyNotepad.lnk';<br>tmpPFile.Save(pWChar(LinkFilename),FALSE);//保存*.lnk文件<br>end; <br>
 
//引用ShlObj,ComObj,ActiveX;<br>function CreateShellLink(SAppName:string;SLinkName:string='';IFolder:Integer=0;<br>&nbsp; &nbsp; SSubFolder:string='';SDescription:string=''):string;<br>//创建快捷方式<br>{快捷方式文件夹<br>&nbsp; CSIDL_DESKTOP &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = $0000;<br>&nbsp; CSIDL_PROGRAMS &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= $0002;<br>&nbsp; CSIDL_CONTROLS &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= $0003;<br>&nbsp; CSIDL_PRINTERS &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= $0004;<br>&nbsp; CSIDL_PERSONAL &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= $0005;<br>&nbsp; CSIDL_FAVORITES &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = $0006;<br>&nbsp; CSIDL_STARTUP &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = $0007;<br>&nbsp; CSIDL_RECENT &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= $0008;<br>&nbsp; CSIDL_SENDTO &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= $0009;<br>&nbsp; CSIDL_STARTMENU &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = $000b;<br>&nbsp; CSIDL_DESKTOPDIRECTORY &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= $0010;<br>&nbsp; CSIDL_FONTS &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = $0014;<br>&nbsp; CSIDL_TEMPLATES &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = $0015;<br>&nbsp; CSIDL_COMMON_STARTMENU &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= $0016;<br>&nbsp; CSIDL_COMMON_PROGRAMS &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = $0017;<br>&nbsp; CSIDL_COMMON_STARTUP &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= $0018;<br>&nbsp; CSIDL_COMMON_DESKTOPDIRECTORY &nbsp; &nbsp; &nbsp; = $0019;<br>}<br>var<br>&nbsp; SL: IShellLink;<br>&nbsp; PF: IPersistFile;<br>&nbsp; FileName: WideString;<br>&nbsp; FilePath: array[0..MAX_PATH] of char;<br>begin<br>&nbsp; try<br>&nbsp; &nbsp; OleCheck(CoCreateInstance(CLSID_ShellLink, nil,<br>&nbsp; &nbsp; &nbsp; &nbsp; CLSCTX_INPROC_SERVER,IShellLink, SL));<br>&nbsp; &nbsp; PF:=SL as IPersistFile;<br>&nbsp; &nbsp; OleCheck(SL.SetPath(PChar(SAppName)));<br>&nbsp; &nbsp; if SDescription &lt;&gt; '' then<br>&nbsp; &nbsp; &nbsp; SL.SetDescription(PChar(SDescription));<br>&nbsp; &nbsp; if SLinkName='' then<br>&nbsp; &nbsp; &nbsp; SLinkName:=ExtractFileName(SAppName);<br>&nbsp; &nbsp; SHGetSpecialFolderPath(0,FilePath,IFolder,True);<br>&nbsp; &nbsp; FileName:=string(FilePath);<br>&nbsp; &nbsp; if SSubFolder&lt;&gt;'' then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; FileName:=FileName+'/'+SSubFolder;<br>&nbsp; &nbsp; &nbsp; if not DirectoryExists(FileName) then<br>&nbsp; &nbsp; &nbsp; CreateDir(FileName);<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; FileName:=FileName+'/'+SLinkName+'.lnk';<br>&nbsp; &nbsp; PF.Save(PWideChar(FileName), True);<br>&nbsp; &nbsp; Result:=FileName;<br>&nbsp; except<br>&nbsp; &nbsp; Result:='';<br>&nbsp; end;<br>end;<br>//在桌面创建<br>&nbsp; CreateShellLink(application.ExeName,'我的APP',$0019);<br>
 
to delphiland <br>IPersistFile 应包含什么文件<br>开始菜单怎获取路径及使用
 
IPersistFile 在 ACTIVEX 单元;<br>
 
在桌面创建无小箭头的快捷方式<br>我们如果要在桌面上建立一个快捷方式,我们通常是用IShellLink接口来生成一个<br>.lnk文件。用这种方式创建,我们在桌面上得到的是一个左下方还含有小箭头的图标,<br>右击它,你会看到正常的弹出菜单(含有复制、粘贴等)。但是你看桌面上IE的快捷<br>方式,你会看到它是一个正常的图标(没有小箭头),而且弹出菜单也不同。下面我<br>们来建立一个这种风格的快捷方式。<br><br>执行这段代码,你会在桌面上获得一个名叫"Netlinker"的图标,右击它,你会看到一个<br>定制的菜单(没有剪切、复制、粘贴、重命名等菜单祥)。<br>你甚至不能把它从桌面上删除,如果你选“属性”,它会打开IE的属性对话框选一个图<br>标文件<br>CString shtct_ico=_T("C://32x32.ico");<br>显示IE属性对话框.<br>CString shtct_prop=_T("rundll32.exe shell32.dll,Control_RunDLL inetcpl.cpl,,0");<br>快捷方式的名字<br>CString shtct_name=_T("Netlinker");<br>找到IE的路径并保存它.<br>CRegKey m_Kiepath;<br>CString ie_path;<br>DWORD dwval;<br>m_Kiepath.Open(HKEY_LOCAL_MACHINE,<br>&nbsp; "SOFTWARE//Microsoft//Windows//"<br>&nbsp; "CurrentVersion//App Paths//IEXPLORE.EXE");<br>m_Kiepath.QueryValue(ie_path.GetBuffer(1000),<br>&nbsp; NULL,<br>&nbsp; &amp;dwval);<br>m_Kiepath.Close();<br>CString shtct_to=ie_path;<br>用guidgen.exe创建一个GUID并复制粘贴到这里。这是用来表现我们的快捷方式和它的<br>菜单项的。我用guidgen.ext创建的GUID是6270AEE4-AA41-11d4-A25D-008048B63F94。<br>下面的程序在HKCR/CLSID创建这个 GUID 键,并且将快捷方式的值设为它。<br>CRegKey m_kdsktp;<br>m_kdsktp.Create(HKEY_CLASSES_ROOT,<br>&nbsp; "CLSID//{6270AEE4-AA41-11d4-A25D-"<br>&nbsp; "008048B63F94}");<br>m_kdsktp.SetValue(shtct_name);<br>m_kdsktp.Close();<br>在这个GUID下创建DefaultIcon 键, 它是用来表现快捷方式的图标的并把他的值设为<br>相应的图标文件的路径.<br>m_kdsktp.Create(HKEY_CLASSES_ROOT,<br>&nbsp; "CLSID//{6270AEE4-AA41-11d4-A25D"<br>&nbsp; "-008048B63F94}//DefaultIcon");<br>m_kdsktp.SetValue(shtct_ico);<br>m_kdsktp.Close();<br>设置右击菜单的菜单项.<br>设置打开菜单项<br>m_kdsktp.Create(HKEY_CLASSES_ROOT,<br>&nbsp; "CLSID//{6270AEE4-AA41-11d4-A25D-"<br>&nbsp; "008048B63F94}//Shell//Open//Command");<br>m_kdsktp.SetValue(shtct_to);<br>m_kdsktp.Close();<br>设置属性菜单项.<br>m_kdsktp.Create(HKEY_CLASSES_ROOT,<br>&nbsp; "CLSID//{6270AEE4-AA41-11d4-A25D-"<br>&nbsp; "008048B63F94}//Shell//Properties//Command");<br>m_kdsktp.SetValue(shtct_prop);<br>m_kdsktp.Close();<br>把原来的缺省菜单项(如复制粘贴等)去掉.<br>BYTE *b;<br>HANDLE heap;<br>char a[20];<br>m_kdsktp.Create(HKEY_CLASSES_ROOT,<br>&nbsp; "CLSID//{6270AEE4-AA41-11d4-A25D-"<br>&nbsp; "008048B63F94}//ShellFolder");<br>strcpy(a,"00.00.00.00");<br>heap=HeapCreate(0,0,0);<br>b=(BYTE*)HeapAlloc(heap,0,30);<br>sscanf(a,<br>&nbsp; "%x.%x.%x.%x",<br>&nbsp; &amp;b[0],<br>&nbsp; &amp;b[1],<br>&nbsp; &amp;b[2],<br>&nbsp; &amp;b[3]);<br>RegSetValueEx(m_kdsktp.m_hKey, &nbsp;"Attributes",<br>&nbsp; 0,REG_BINARY,b,4);<br>HeapFree(heap, 0, b);<br>HeapDestroy(heap);<br>m_kdsktp.Close();<br>接下来我们添加一个引用到<br>HKLM/SOFTWARE/Microsoft/Windows/CurrentVersion/Explorer/Desktop/Namespace<br>这样它将只显示在桌面上。<br>m_kdsktp.Create(HKEY_LOCAL_MACHINE,<br>&nbsp; "SOFTWARE//Microsoft//Windows//"<br>&nbsp; "CurrentVersion//Explorer//Desktop//"<br>&nbsp; "NameSpace//{6270AEE4-AA41-11d4-A25D-"<br>&nbsp; "008048B63F94}");<br>m_kdsktp.SetValue("Netlink");<br>m_kdsktp.Close();<br>我们对Shell作了改动,我们要通知系统,这个改动才会马上表现出来.<br>SHChangeNotify(SHCNE_ASSOCCHANGED,SHCNF_FLUSHNOWAIT, 0, 0);
 
没仔细看,上面的应该都不错。
 
unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs, Registry, ActiveX, ShlObj, ComObj, StdCtrls;<br><br>type<br><br>&nbsp; ShortcutType = (_DESKTOP, _QUICKLAUNCH, _SENDTO, _STARTMENU, _PROGRAMS);<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br>procedure CreateShortcut(SourceFileName: string; Location: ShortcutType; SubDirectory: string);<br><br>implementation<br><br>{$R *.dfm}<br><br>procedure CreateShortcut(SourceFileName: string; Location: ShortcutType; SubDirectory: string);<br><br>var<br>&nbsp; MyObject: IUnknown;<br>&nbsp; MySLink: IShellLink;<br>&nbsp; MyPFile: IPersistFile;<br>&nbsp; Directory, LinkName, TempStr: string;<br>&nbsp; WFileName: WideString;<br>&nbsp; MyReg, QuickLaunchReg: TRegIniFile;<br><br>begin<br>&nbsp; MyObject := CreateComObject(CLSID_ShellLink);<br>&nbsp; MySLink := MyObject as IShellLink;<br>&nbsp; MyPFile := MyObject as IPersistFile;<br>&nbsp; MySLink.SetPath(PChar(SourceFileName));<br>&nbsp; MyReg := TRegIniFile.Create('Software/MicroSoft/Windows/CurrentVersion/Explorer');<br>&nbsp; try<br>&nbsp; &nbsp; LinkName := ChangeFileExt(SourceFileName, '.lnk');<br>&nbsp; &nbsp; LinkName := ExtractFileName(LinkName);<br>&nbsp; &nbsp; case Location of<br>&nbsp; &nbsp; &nbsp; _DESKTOP: Directory := MyReg.ReadString('Shell Folders', 'Desktop', '');<br>&nbsp; &nbsp; &nbsp; _STARTMENU: Directory := MyReg.ReadString('Shell Folders', 'Start Menu', '');<br>&nbsp; &nbsp; &nbsp; _SENDTO: Directory := MyReg.ReadString('Shell Folders', 'SendTo', '');<br>&nbsp; &nbsp; &nbsp; _PROGRAMS: Directory := MyReg.ReadString('Shell Folders', 'Programs', '');<br>&nbsp; &nbsp; &nbsp; _QUICKLAUNCH:<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; QuickLaunchReg := TRegIniFile.Create('Software/MicroSoft/Windows/CurrentVersion/GrpConv');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Directory := QuickLaunchReg.ReadString('MapGroups', 'Quick Launch', '');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; finally<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; QuickLaunchReg.Free;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; TempStr := Directory;<br>&nbsp; &nbsp; if Location = _PROGRAMS then Directory := Directory + '/' + SubDirectory;<br>&nbsp; &nbsp; if (Location = _PROGRAMS) and (TempStr &lt;&gt; Directory) then<br>&nbsp; &nbsp; &nbsp;if not DirectoryExists(Directory) then<br>&nbsp; &nbsp; &nbsp; if not ForceDirectories(Directory) then Directory := TempStr;<br>&nbsp; &nbsp; &nbsp; WFileName := Directory + '/' + LinkName;<br>&nbsp; &nbsp; &nbsp; MyPFile.Save(PWChar(WFileName), False);<br>&nbsp; finally<br>&nbsp; &nbsp; MyReg.Free;<br>&nbsp; end;<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>//CreateShortcut('C:/VipProgram/VipBrowserWeb_OCR/VipBrowser.exe',_STARTMENU,'');<br>//CreateShortcut('C:/VipProgram/VipBrowserWeb_OCR/VipBrowser.exe',_DESKTOP,'');<br>//CreateShortcut('C:/VipProgram/VipBrowserWeb_OCR/VipBrowser.exe',_SENDTO,'');<br>//CreateShortcut('C:/VipProgram/VipBrowserWeb_OCR/VipBrowser.exe',_QUICKLAUNCH,'');<br>CreateShortcut('C:/VipProgram/VipBrowserWeb_OCR/VipBrowser.exe', _PROGRAMS, 'Folder/SubFolder');<br>end;<br><br>end.
 
后退
顶部