在CSDN論壇看到有網友提問如何把程序釘到Windows7的任務欄,ccrun(妖哥)對這個問題很感興趣,因而google了一下,沒有找到相關的API資料,可是在國外的一個站點看到用FolderItemVerb對象來實現的方法,關於具體的資料,能夠查閱MSDN:
http://msdn.microsoft.com/en-us/library/windows/desktop/bb774172(v=vs.85).aspxwindows
在Delphi中實現的代碼以下。編譯環境:Delphi7和XE2,測試操做系統爲中文和英文Windows7ide
- uses ComObj;
-
- procedure CrnPinAppToWin7Taskbar(strPath, strApp: string);
- var
- vShell, vFolder, vFolderItem, vItemVerbs: Variant;
- vPath, vApp: Variant;
- i: Integer;
- str: String;
- h: HINST;
- szPinName: array[0..255] of Char;
- begin
- vShell := CreateOleObject('Shell.Application');
- vPath := strPath;
- vFolder := vShell.NameSpace(vPath);
- vApp := strApp;
- vFolderItem := vFolder.ParseName(vApp);
- vItemVerbs := vFolderItem.Verbs;
-
- h := LoadLibrary('Shell32.dll');
- LoadString(h, 5386, szPinName, 256);
- FreeLibrary(h);
-
- for i := 1 to vItemVerbs.Count do
- begin
- str := vItemVerbs.Item(i).Name;
-
- if SameText(str, szPinName) then
- begin
- // 63 63 72 75 6E 2E 63 6F 6D
- vItemVerbs.Item(i).DoIt;
- end;
- end;
- end;
-
- procedure TForm1.Button1Click(Sender: TObject);
- begin
- CrnPinAppToWin7Taskbar('C:\windows', 'regedit.exe');
- end;
在C++Builder中的實現代碼以下。編譯環境:C++Builder6和XE2,測試操做系統:中文和英文Windows7測試
- #include <comobj.hpp>
-
- void __fastcall CrnPinAppToWin7Taskbar(String strPath, String strApp)
- {
- Variant vShell = CreateOleObject("Shell.Application");
- Variant vFolder = vShell.OleFunction("NameSpace", WideString(strPath));
- Variant vFolderItem = vFolder.OleFunction("ParseName", WideString(strApp));
- Variant vItemVerbs = vFolderItem.OleFunction("Verbs");
-
- // 獲取Pin to Taskbar的Verb字符串, 感謝titilima
- HINSTANCE hInst = ::LoadLibrary("Shell32.dll");
-
- TCHAR szPinName[256] = { 0 };
- ::LoadString(hInst, 5386, szPinName, 256);
-
- ::FreeLibrary(hInst);
-
- String str;
-
- int nCount = vItemVerbs.OlePropertyGet("Count");
- for (int i = 0; i < nCount; i++)
- {
- str = vItemVerbs.OleFunction("Item", i).OlePropertyGet("Name");
-
- if (SameText(str, szPinName))
- {
- // 63 63 72 75 6E 2E 63 6F 6D
- vItemVerbs.OleFunction("Item", i).OleFunction("DoIt");
- }
- }
- }
-
- void __fastcall TForm1::Button1Click(TObject *Sender)
- {
- CrnPinAppToWin7Taskbar("C:\\windows\\", "notepad.exe");
- }
另外,感謝titilima大牛,針對.lnk文件釘到Win7任務欄,有更簡便的方法:ui
C++代碼:google
- ::ShellExecute(NULL, TEXT("TaskbarPin"), TEXT("E:\\Temp\\Notepad.lnk"), NULL, NULL, SW_SHOW);
Delphi代碼:spa
- ShellExecute(nil, 'TaskbarPin', 'E:\Temp\Notepad.lnk'), nil, nil, SW_SHOW);