建立一个快捷方式 Dim slnk As cShellLink Dim sfpath As String Set slnk = New cShellLink GetSystemFolderPath Me.hwnd, 0, sfpath slnk.CreateShellLink sfpath + "\文件.lnk", App.Path + "\" + App.EXEName, App.Path + "\", "", App.Path + "\" + App.EXEName + ".exe", 0, SHOWNORMAL Set slnk = Nothing模块中 Public Function GetSystemFolderPath(ByVal hwnd As Long, ByVal Id As Integer, sfpath As String) As Long Dim rc As Long Dim pidl As Long Dim cbPath As Long Dim szPath As String szPath = Space(MAX_PATH) rc = SHGetSpecialFolderLocation(hwnd, Id, pidl)
If (rc = 0) Then #If UNICODE Then rc = SHGetPathFromIDList(pidl, StrPtr(szPath)) #Else rc = SHGetPathFromIDList(pidl, szPath) #End If If (rc = 1) Then szPath = Trim$(szPath) cbPath = Len(szPath) If (Asc(Right(szPath, 1)) = 0) Then cbPath = cbPath - 1 If (cbPath > 0) Then sfpath = Left$(szPath, cbPath) GetSystemFolderPath = True End If End If End Function Public Declare Function SHGetSpecialFolderLocation Lib "Shell32" (ByVal hwndOwner As Long, ByVal nFolder As Integer, ppidl As Long) As Long #If UNICODE Then Public Declare Function SHGetPathFromIDList Lib "Shell32" Alias "SHGetPathFromIDListW" (ByVal pidl As Long, ByVal szPath As Long) As Long #Else Public Declare Function SHGetPathFromIDList Lib "Shell32" Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal szPath As String) As Long #End If Public Function CreateShellLink(LnkFile As String, ExeFile As String, WorkDir As String, _ ExeArgs As String, IconFile As String, IconIdx As Long, _ ShowCmd As SHOWCMDFLAGS) As Long Dim rc As Long Dim pidl As Long Dim dwReserved As Long Dim cShellLink As ShellLinkA Dim cPersistFile As IPersistFile If ((LnkFile = "") Or (ExeFile = "")) Then Exit Function On Error GoTo ErrHandler Set cShellLink = New ShellLinkA Set cPersistFile = cShellLink With cShellLink .SetPath ExeFile If (WorkDir <> "") Then .SetWorkingDirectory WorkDir
If (ExeArgs <> "") Then .SetArguments ExeArgs
If (IconFile <> "") Then .SetIconLocation IconFile, IconIdx ' Set shortcut icon location & index
.SetDescription "ShellLink Sample" & vbNullChar .SetShowCmd ShowCmd End With
cShellLink.Resolve 0, SLR_UPDATE cPersistFile.Save StrConv(LnkFile, vbUnicode), 0 CreateShellLink = True ErrHandler: Set cPersistFile = Nothing Set cShellLink = Nothing End Function
Dim slnk As cShellLink
Dim sfpath As String
Set slnk = New cShellLink
GetSystemFolderPath Me.hwnd, 0, sfpath
slnk.CreateShellLink sfpath + "\文件.lnk", App.Path + "\" + App.EXEName, App.Path + "\", "", App.Path + "\" + App.EXEName + ".exe", 0, SHOWNORMAL
Set slnk = Nothing模块中
Public Function GetSystemFolderPath(ByVal hwnd As Long, ByVal Id As Integer, sfpath As String) As Long
Dim rc As Long
Dim pidl As Long
Dim cbPath As Long
Dim szPath As String
szPath = Space(MAX_PATH)
rc = SHGetSpecialFolderLocation(hwnd, Id, pidl)
If (rc = 0) Then
#If UNICODE Then
rc = SHGetPathFromIDList(pidl, StrPtr(szPath))
#Else
rc = SHGetPathFromIDList(pidl, szPath)
#End If
If (rc = 1) Then
szPath = Trim$(szPath)
cbPath = Len(szPath)
If (Asc(Right(szPath, 1)) = 0) Then cbPath = cbPath - 1
If (cbPath > 0) Then sfpath = Left$(szPath, cbPath)
GetSystemFolderPath = True
End If
End If
End Function
Public Declare Function SHGetSpecialFolderLocation Lib "Shell32" (ByVal hwndOwner As Long, ByVal nFolder As Integer, ppidl As Long) As Long
#If UNICODE Then
Public Declare Function SHGetPathFromIDList Lib "Shell32" Alias "SHGetPathFromIDListW" (ByVal pidl As Long, ByVal szPath As Long) As Long
#Else
Public Declare Function SHGetPathFromIDList Lib "Shell32" Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal szPath As String) As Long
#End If
Public Function CreateShellLink(LnkFile As String, ExeFile As String, WorkDir As String, _
ExeArgs As String, IconFile As String, IconIdx As Long, _
ShowCmd As SHOWCMDFLAGS) As Long
Dim rc As Long
Dim pidl As Long
Dim dwReserved As Long
Dim cShellLink As ShellLinkA
Dim cPersistFile As IPersistFile
If ((LnkFile = "") Or (ExeFile = "")) Then Exit Function
On Error GoTo ErrHandler
Set cShellLink = New ShellLinkA
Set cPersistFile = cShellLink
With cShellLink
.SetPath ExeFile
If (WorkDir <> "") Then .SetWorkingDirectory WorkDir
If (ExeArgs <> "") Then .SetArguments ExeArgs
If (IconFile <> "") Then .SetIconLocation IconFile, IconIdx ' Set shortcut icon location & index
.SetDescription "ShellLink Sample" & vbNullChar
.SetShowCmd ShowCmd
End With
cShellLink.Resolve 0, SLR_UPDATE
cPersistFile.Save StrConv(LnkFile, vbUnicode), 0
CreateShellLink = True
ErrHandler:
Set cPersistFile = Nothing
Set cShellLink = Nothing
End Function
BOOL CSortCutApp::InitInstance()
{
......
CoInitialize (NULL);
CSortCutDlg dlg;
m_pMainWnd = &dlg;
......
CoUninitialize ();
return FALSE;
}
// SortCutDlg.cpp :
#include "stdafx.h"
#include "SortCut.h"
#include "SortCutDlg.h"#include "NameDlg.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif//起始文件夹的PIDL
int nBeginAt=CSIDL_DESKTOPDIRECTORY;
......
///浏览文件夹
BOOL BrowseForFolder(
LPITEMIDLIST pidlRoot,//浏览开始处的PIDL
LPITEMIDLIST *ppidlDestination,
//浏览结束时所选择的PIDL
LPCSTR lpszTitle)//浏览对话框中的提示文字
{ BROWSEINFO BrInfo ; ZeroMemory( &BrInfo, sizeof(BrInfo)) ;
BrInfo.hwndOwner = HWND_DESKTOP ;
BrInfo.pidlRoot = pidlRoot ;
BrInfo.lpszTitle = lpszTitle ; //浏览文件夹
*ppidlDestination= SHBrowseForFolder(&BrInfo);
//用户选择了取消按钮
if(NULL == *ppidlDestination)
return FALSE ;
return TRUE ;
}
//取得快捷方式的目标应用程序名
SelectMenuItem( LPSTR szFileName)
{
OPENFILENAME ofn ;
static CHAR szFilter[] = "Programs\0*.exe\0" ; ZeroMemory(&ofn, sizeof( OPENFILENAME)) ;
ofn.lStructSize = sizeof( OPENFILENAME) ;
ofn.hwndOwner = HWND_DESKTOP;
ofn.lpstrFilter = szFilter ;
ofn.nFilterIndex = 0 ;
ofn.nMaxFile = MAX_PATH ;
ofn.lpstrTitle = "请选择目标应用程序:" ;
ofn.lpstrFile = szFileName ;
ofn.Flags = OFN_FILEMUSTEXIST |
OFN_PATHMUSTEXIST | OFN_EXPLORER ;
//文件浏览
if(!GetOpenFileName( &ofn))//选择了取消按钮
return FALSE ;
return TRUE ;
}
//取得要创建的快捷方式的名字
BOOL GetShortcutCrt(LPSTR szPath)
{
LPITEMIDLIST pidlBeginAt, pidlDestination ;
// 取得开始菜单或桌面的PIDL
SHGetSpecialFolderLocation( HWND_DESKTOP,
nBeginAt, &pidlBeginAt) ;
// 取得要创建的快捷方式所在的位置
if( !BrowseForFolder(pidlBeginAt, &pidlDestination,
"请选择快捷方式所在的位置:"))
return FALSE ;
// 把PIDL转换为路径名
SHGetPathFromIDList( pidlDestination, szPath) ;
// 取得快捷方式名称
CNameDlg name_dlg;
if(name_dlg.DoModal() == IDCANCEL)
return FALSE ;
//把快捷方式名和扩展名.LNK添加到路径名后
//形成完整的快捷方式数据文件名
wsprintf(szPath+lstrlen(szPath),"\\%s.lnk",
name_dlg.m_strName) ;
return TRUE ;
}
//创建快捷方式
BOOL CreateLink (
LPSTR szPath,//快捷方式的目标应用程序名
LPSTR szLink)//快捷方式的数据文件名(*.lnk)
{
HRESULT hres ;
IShellLink * psl ;
IPersistFile* ppf ;
WORD wsz[ MAX_PATH] ;
//创建一个IShellLink实例
hres = CoCreateInstance( CLSID_ShellLink, NULL,
CLSCTX_INPROC_SERVER, IID_IShellLink,
(void **)&psl) ;
if( FAILED( hres))
return FALSE ;
//设置目标应用程序
psl -> SetPath( szPath) ;
//设置快捷键(此处设为Shift+Ctrl+'R')
psl -> SetHotkey( MAKEWORD( 'R',
HOTKEYF_SHIFT |HOTKEYF_CONTROL)) ;
//从IShellLink获取其IPersistFile接口
//用于保存快捷方式的数据文件 (*.lnk)
hres = psl -> QueryInterface( IID_IPersistFile,
(void**)&ppf) ;
if( FAILED( hres))
return FALSE ;
// 确保数据文件名为ANSI格式
MultiByteToWideChar( CP_ACP, 0, szLink, -1,
wsz, MAX_PATH) ;
//调用IPersistFile::Save
//保存快捷方式的数据文件 (*.lnk)
hres = ppf -> Save( wsz, STGM_READWRITE) ;
//释放IPersistFile和IShellLink接口
ppf -> Release( ) ;
psl -> Release( ) ;
return TRUE;
}
BOOL DeleteFolder( LPSTR pszFolder)
{
SHFILEOPSTRUCT fos ;
ZeroMemory( &fos, sizeof( fos)) ;
fos.hwnd = HWND_DESKTOP;
fos.wFunc = FO_DELETE ;
fos.fFlags = FOF_SILENT | FOF_ALLOWUNDO ;
fos.pFrom = pszFolder ; // 删除文件夹及其内容
if( 0 != SHFileOperation( &fos))
return FALSE ;
return TRUE;
}
//取得要删除的快捷方式
BOOL GetShortcutDel (
LPSTR lpszInitDir,//选择文件的开始目录
LPSTR lpszShortcut)//快捷方式名
{
OPENFILENAME ofn ;
char szFilter[] = "Shortcuts\0*.lnk\0" ; ZeroMemory(&ofn,sizeof( OPENFILENAME));
ofn.lStructSize = sizeof( OPENFILENAME) ;
ofn.hwndOwner = HWND_DESKTOP ;
ofn.lpstrFilter = szFilter ;
ofn.nFilterIndex = 0 ;
ofn.nMaxFile = MAX_PATH ;
ofn.lpstrTitle = "请选择要删除的快捷方式:" ;
ofn.lpstrFile = lpszShortcut;
ofn.lpstrInitialDir = lpszInitDir ;
ofn.Flags = OFN_FILEMUSTEXIST |
OFN_PATHMUSTEXIST | OFN_EXPLORER |
OFN_NODEREFERENCELINKS ;
//文件浏览
if(! GetOpenFileName( &ofn))//选择了取消按钮
return FALSE ;
return TRUE ;
}
//删除快捷方式的数据文件 (*.lnk)
BOOL DeleteLink( LPSTR lpszShortcut)
{
SHFILEOPSTRUCT fos ; ZeroMemory( &fos, sizeof(fos)) ;
fos.hwnd = HWND_DESKTOP ;
fos.wFunc = FO_DELETE ;
fos.pFrom = lpszShortcut;
fos.pTo = NULL ;
fos.fFlags = FOF_SILENT | FOF_ALLOWUNDO ;
//删除快捷方式(*.lnk)
if( 0 != SHFileOperation( &fos))
return FALSE ;
return TRUE ;
}
// 通知shell有关变化
void NotifyShell(LONG wEventId,//事件标志
LPSTR szPath)//路径
{
SHChangeNotify( wEventId,
SHCNF_FLUSH | SHCNF_PATH,
szPath,0);
//取得szPath的父目录
char* p;
for( p=szPath+lstrlen(szPath)-1;
*p != '\\';
p--);
*p='\0';
SHChangeNotify(SHCNE_UPDATEDIR
|SHCNE_INTERRUPT,
SHCNF_FLUSH | SHCNF_PATH,szPath,0);
}
///////////////////////////////////////////////////
// CSortCutDlg dialog
CSortCutDlg::CSortCutDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSortCutDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSortCutDlg)
//把圆形按钮“桌面”设为选中状态
m_nLocation = 0;
……
}
……
void CSortCutDlg::OnCreateGroup()
{
LPITEMIDLIST pidlBeginAt, pidlDestination ;
char szPath[ MAX_PATH] ; // 取得开始菜单或桌面的PIDL
SHGetSpecialFolderLocation( HWND_DESKTOP,
nBeginAt, &pidlBeginAt) ;
// 取得新建文件夹的父文件夹
if( !BrowseForFolder(pidlBeginAt ,
&pidlDestination,
"请选择新建文件夹/菜单组的位置:"))
return ;
// 把PIDL转换为路径名
SHGetPathFromIDList( pidlDestination, szPath) ;
//取得新建文件夹的名字
CNameDlg name_dlg;
if(name_dlg.DoModal() == IDCANCEL)
return;
//形成完整的新建文件夹名
wsprintf(szPath+lstrlen(szPath),"\\%s",
name_dlg.m_strName);
//创建文件夹(子目录)
if( !CreateDirectory( szPath, NULL))
{
MessageBox( "创建文件夹失败!") ;
return ;
}
// 通知shell有关变化
NotifyShell( SHCNE_MKDIR|SHCNE_INTERRUPT,
szPath);
}
void CSortCutDlg::OnCreateItem()
{
char szPath[MAX_PATH]="";
//快捷方式的目标应用程序名
char szLink[MAX_PATH]="";
//快捷方式的数据文件名
// 取得快捷方式的目标应用程序名
if( !SelectMenuItem( szPath))
return ;
// 取得新建快捷方式所在的文件夹
//并形成其数据文件名
if( !GetShortcutCrt( szLink))
return ;
// 创建快捷方式
if(!CreateLink( szPath, szLink) )
return;
// 通知shell有关变化
NotifyShell( SHCNE_CREATE|SHCNE_INTERRUPT,
szLink) ;
}
void CSortCutDlg::OnDeleteGroup()
{
LPITEMIDLIST pidlBeginAt, pidlFolder ;
char szPath[MAX_PATH]=""; // 取得开始菜单或桌面的PIDL
SHGetSpecialFolderLocation( HWND_DESKTOP,
nBeginAt, &pidlBeginAt) ;
// 取得要删除的文件夹
if( !BrowseForFolder( pidlBeginAt, &pidlFolder,
"请选择要删除的文件夹/菜单组:"))
return ;
// 把PIDL转化为路径名
SHGetPathFromIDList( pidlFolder, szPath) ;
// 删除文件夹
if(!DeleteFolder( szPath))
return ;
// 通知shell有关变化
NotifyShell( SHCNE_RMDIR|SHCNE_INTERRUPT,
szPath) ;
}
void CSortCutDlg::OnDeleteItem()
{
LPITEMIDLIST pidlBeginAt ;
char szShortcut[ MAX_PATH]="",
szPath[ MAX_PATH]=""; // 取得开始菜单或桌面的PIDL
SHGetSpecialFolderLocation( HWND_DESKTOP,
nBeginAt, &pidlBeginAt) ;
// 把PIDL转化为路径名
SHGetPathFromIDList( pidlBeginAt, szPath) ;
// 取得要删除的快捷方式
if( !GetShortcutDel( szPath, szShortcut))
return ;
// 删除快捷方式
if( !DeleteLink(szShortcut))
return ;
// 通知SHELL有关改变
NotifyShell( SHCNE_DELETE|SHCNE_INTERRUPT,
szShortcut) ;
}
void CSortCutDlg::OnDesktop()
{
//设置起始文件夹为桌面
nBeginAt=CSIDL_DESKTOPDIRECTORY ;
}
void CSortCutDlg::OnStartmenu()
{
//设置起始文件夹为“开始”菜单
nBeginAt=CSIDL_STARTMENU;
}