📄 unit1.cpp
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include <shlobj.h>
#include <registry.hpp>
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
enum TShellFolder {sfDesktop, sfFavorites, sfFonts, sfPersonal, sfPrograms,
sfRecent, sfSendTo, sfStartMenu, sfStartup, sfTemplates};
const AnsiString ShellFolderKeys[10 /* TShellFolder */] =
{"Desktop", "Favorites", "Fonts", "Personal", "Programs",
"Recent", "SendTo", "Start Menu", "Startup", "Templates"};
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
CoInitialize(NULL);
for (TShellFolder sf = sfDesktop; sf <= sfTemplates; sf = sf + 1)
cbxFolders->Items->Add(ShellFolderKeys[sf]);
cbxFolders->ItemIndex = 0;
cbxWindowStates->ItemIndex = 0;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnExitClick(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
CoUninitialize();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnCreateShellLinkClick(TObject *Sender)
{
const int WindowStates[3 /* TWindowState */] =
{SW_SHOWNORMAL, SW_SHOWMINNOACTIVE, SW_SHOWMAXIMIZED};
if (txtFilePath->Text == "") return;
// ˙艼 1
IShellLink* Psl;
if FAILED(CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLinkA, (void**)&Psl))
throw new Exception("Error in create instance");
// ˙艼 2
Psl->SetPath(txtFilePath->Text.c_str());
Psl->SetDescription(txtDescription->Text.c_str());
Psl->SetWorkingDirectory(txtWorkingDirectory->Text.c_str());
Psl->SetArguments(txtArguments->Text.c_str());
Psl->SetHotkey(HotKey->HotKey);
Psl->SetShowCmd(WindowStates[cbxWindowStates->ItemIndex]);
Psl->SetIconLocation(txtIconLocation->Text.c_str(), 0);
// ˙艼 3
IPersistFile* Ppf;
if FAILED(Psl->QueryInterface(IID_IPersistFile, (void**)&Ppf))
throw new Exception("Error in query instance");
// ˙艼 4
AnsiString sFileName = ChangeFileExt(ExtractFileName(txtFilePath->Text), ".LNK");
TRegIniFile* r = new TRegIniFile("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer");
try {
sFileName = r->ReadString("Shell Folders", ShellFolderKeys[cbxFolders->ItemIndex], "") + "\\" + sFileName;
} __finally {
delete r;
}
// ˙艼 5
WideChar wFileName[MAX_PATH];
// convert AnsiString to Unicode string
sFileName.WideChar(wFileName, sizeof(wFileName));
if FAILED(Ppf->Save(wFileName, true))
throw new Exception("Error in save LNK file");
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -