如何创建.lnk文件? |
答題得分者是:Cooky Kid
|
I_Love_You
一般會員 發表:18 回覆:87 積分:21 註冊:2002-07-24 發送簡訊給我 |
|
RaynorPao
版主 發表:139 回覆:3622 積分:7025 註冊:2002-08-12 發送簡訊給我 |
引言: 各位先进,我想问一下,在BCB中如何来创建快捷方式 (.lnk档案)?可否举个例子?谢谢!!!以下資料「轉貼」自 MSDN 及 Platform SDK 關鍵字「Shell Links」希望對你有幫助~~ IsShortcut = "">SetPath(lpszPathObj); psl->SetDescription(lpszDesc); // Query IShellLink for the IPersistFile interface for saving the // shortcut in persistent storage. hres = psl->QueryInterface(&IID_IPersistFile, (LPVOID*)&ppf); if (SUCCEEDED(hres)) { WCHAR wsz[MAX_PATH]; // Ensure that the string is Unicode. MultiByteToWideChar(CP_ACP, 0, lpszPathLink, -1, wsz, MAX_PATH); // Save the link by calling IPersistFile::Save. hres = ppf->Save(wsz, TRUE); ppf->Release(); } psl->Release(); } return hres; } Resolving a Shortcut An application may need to access and manipulate a shortcut that was previously created. This operation is referred to as resolving the shortcut. The application-defined ResolveIt function in the following example resolves a shortcut. Its parameters include a window handle, a pointer to the path of the shortcut, and the address of a buffer that receives the new path to the object. The window handle identifies the parent window for any message boxes that the Shell may need to display. For example, the Shell can display a message box if the link is on unshared media, if network problems occur, if the user needs to insert a floppy disk, and so on. The ResolveIt function calls the CoCreateInstance function and assumes that the CoInitialize function has already been called. Note that ResolveIt needs to use the IPersistFile interface to store the link information. IPersistFile is implemented by the IShellLink object. The link information must be loaded before the path information is retrieved, which is shown later in the example. Failing to load the link information causes the calls to the IShellLink::GetPath and IShellLink::GetDescription member functions to fail. HRESULT ResolveIt(HWND hwnd, LPCSTR lpszLinkFile, LPSTR lpszPath) { HRESULT hres; IShellLink* psl; char szGotPath[MAX_PATH]; char szDescription[MAX_PATH]; WIN32_FIND_DATA wfd; *lpszPath = 0; // assume failure // Get a pointer to the IShellLink interface. hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID *) &psl); if (SUCCEEDED(hres)) { IPersistFile* ppf; // Get a pointer to the IPersistFile interface. hres = psl->QueryInterface(IID_IPersistFile, (void**)&ppf); if (SUCCEEDED(hres)) { WCHAR wsz[MAX_PATH]; // Ensure that the string is Unicode. MultiByteToWideChar(CP_ACP, 0, lpszLinkFile, -1, wsz, MAX_PATH); // Load the shortcut. hres = ppf->Load(wsz, STGM_READ); if (SUCCEEDED(hres)) { // Resolve the link. hres = psl->Resolve(hwnd, 0); if (SUCCEEDED(hres)) { // Get the path to the link target. hres = psl->GetPath(szGotPath, MAX_PATH, (WIN32_FIND_DATA *)&wfd, SLGP_SHORTPATH ); if (FAILED(hres)) HandleErr(hres); // application-defined function // Get the description of the target. hres = psl->GetDescription(szDescription, MAX_PATH); if (FAILED(hres)) HandleErr(hres); lstrcpy(lpszPath, szGotPath); } } // Release the pointer to the IPersistFile interface. ppf->Release(); } // Release the pointer to the IShellLink interface. psl->Release(); } return hres; } Creating a Shortcut to a Nonfile Object Creating a shortcut to a nonfile object, such as a printer, is similar to creating a shortcut to a file except that rather than setting the path to the file, you must set the identifier list to the printer. To set the identifier list, call the IShellLink::SetIDList method, specifying the address of an identifier list. Each object within the Shell's namespace has an item identifier. The Shell often concatenates item identifiers into null-terminated lists consisting of any number of item identifiers. For more information about item identifiers, see Item Identifiers and Identifier Lists. In general, if you need to set a shortcut to an item that does not have a file name, such as a printer, you will already have a pointer to the object's IShellFolder interface. IShellFolder is used to create namespace extensions. Once you have the class identifier for IShellFolder, you can call the CoCreateInstance function to retrieve the address of the interface. Then you can call the interface to enumerate the objects in the folder and retrieve the address of the item identifier for the object that you are searching for. Finally, you can use the address in a call to the IShellLink::SetIDList member function to create a shortcut to the object.
------
-- 若您已經得到滿意的答覆,請適時結案!! -- -- 欲知前世因,今生受者是;欲知來世果,今生做者是 -- -- 一切有為法,如夢幻泡影,如露亦如電,應作如是觀 -- |
I_Love_You
一般會員 發表:18 回覆:87 積分:21 註冊:2002-07-24 發送簡訊給我 |
|
RaynorPao
版主 發表:139 回覆:3622 積分:7025 註冊:2002-08-12 發送簡訊給我 |
|
Cooky Kid
初階會員 發表:5 回覆:68 積分:44 註冊:2002-07-11 發送簡訊給我 |
應該可以解你問題吧?!
也歡迎你將改寫的程式碼post出來,OK!
enjoy it!!! //---------------------------------------------------------------------------
//此行定義,詳情請見 http://www.bcbdev.com/faqs/faq91.htm
#define NO_WIN32_LEAN_AND_MEAN
#include <vcl.h>
#pragma hdrstop //---------------------------------------------------------------------------
#include <shellapi.h>
//---------------------------------------------------------------------------
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
/*
char sysDir[MAX_PATH];
//將系統路徑填入此字元陣列中
::GetSystemDirectory(sysDir, sizeof(sysDir));
*/
HRESULT hres;
IShellLink* psl;
//取得 IShellLink 介面的指標
hres = CoCreateInstance(CLSID_ShellLink, NULL,
CLSCTX_INPROC_SERVER,
IID_IShellLink, (void**)(&psl));
//若成功取得,則進行 IShellLink 各項資料設定
if(SUCCEEDED(hres))
{
//取得 IPersistFile 介面的指標
IPersistFile* ppf;
hres = psl->QueryInterface(IID_IPersistFile, (void**)(&ppf));
//若成功取得,則進行 IShellLink 各項資料設定
if(SUCCEEDED(hres))
{
//此範例連接檔的路徑,即是此執行檔路徑
hres = psl->SetPath(Application->ExeName.c_str());
//設定此連接檔的類型
hres = psl->SetDescription("my test");
//設定此連接檔放置何處,本範例的連接檔放置在桌面上
hres = ppf->Save(WideString("C:\\win98\\Desktop\\MyLinkFile.lnk"), TRUE);
//完成後,將IPersistFile介面釋放
ppf->Release();
}
//完成後,將IShellLink介面釋放
psl->Release();
}
}
//---------------------------------------------------------------------------
|
RaynorPao
版主 發表:139 回覆:3622 積分:7025 註冊:2002-08-12 發送簡訊給我 |
|
I_Love_You
一般會員 發表:18 回覆:87 積分:21 註冊:2002-07-24 發送簡訊給我 |
|
Cooky Kid
初階會員 發表:5 回覆:68 積分:44 註冊:2002-07-11 發送簡訊給我 |
|
I_Love_You
一般會員 發表:18 回覆:87 積分:21 註冊:2002-07-24 發送簡訊給我 |
|
lcsboy
版主 發表:87 回覆:622 積分:394 註冊:2002-06-18 發送簡訊給我 |
|
davidchang
一般會員 發表:4 回覆:10 積分:2 註冊:2002-08-08 發送簡訊給我 |
|
lcsboy
版主 發表:87 回覆:622 積分:394 註冊:2002-06-18 發送簡訊給我 |
修正一下我之前的說法! 把VCL.h點掉是一個方法! 但是用了ShowMessage時找不到定義! 後來發現正解:
1. 就是shlobj擺最上方!
2. 加入#define NO_WIN32_LEAN_AND_MEAN在下一行
3. 接著就是vcl.h 這樣子Complier一定會過, 而且所有的function都可以正常work 原先放的Demo已經修正, 有興趣的人可以重新下載!
http://delphi.ktop.com.tw/topic.php?TOPIC_ID=22716
|
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |