?? shortcutdlg.cpp
字號:
// ShortCutDlg.cpp : implementation file
//
#include "stdafx.h"
#include "ShortCut.h"
#include "ShortCutDlg.h"
#include "NameDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CShortCutDlg dialog
CShortCutDlg::CShortCutDlg(CWnd* pParent /*=NULL*/)
: CDialog(CShortCutDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CShortCutDlg)
m_nLocation = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CShortCutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CShortCutDlg)
DDX_Radio(pDX, IDC_RADIO1, m_nLocation);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CShortCutDlg, CDialog)
//{{AFX_MSG_MAP(CShortCutDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_CREATEGROUP, OnCreateGroup)
ON_BN_CLICKED(IDC_CREATEITEM, OnCreateItem)
ON_BN_CLICKED(IDC_DELETEGROUP, OnDeleteGroup)
ON_BN_CLICKED(IDC_DELETEITEM, OnDeleteItem)
ON_WM_DESTROY()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CShortCutDlg message handlers
BOOL CShortCutDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
CoInitialize (NULL);
return TRUE; // return TRUE unless you set the focus to a control
}
void CShortCutDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CShortCutDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CShortCutDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
//瀏覽文件夾
BOOL CShortCutDlg::BrowseForFolder(
LPITEMIDLIST pidlRoot,//瀏覽開始處的PIDL
LPITEMIDLIST *ppidlDestination,
//瀏覽結(jié)束時所選擇的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 ;
}
//取得快捷方式的目標應用程序名
BOOL CShortCutDlg::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 ;
}
//取得要創(chuàng)建的快捷方式的名字
BOOL CShortCutDlg::GetShortcutCrt(LPSTR szPath)
{
LPITEMIDLIST pidlBeginAt, pidlDestination ;
// 取得開始菜單或桌面的PIDL
SHGetSpecialFolderLocation( HWND_DESKTOP,
nBeginAt, &pidlBeginAt) ;
// 取得要創(chuàng)建的快捷方式所在的位置
if( !BrowseForFolder(pidlBeginAt, &pidlDestination,
"請選擇快捷方式所在的位置:"))
return FALSE ;
// 把PIDL轉(zhuǎn)換為路徑名
SHGetPathFromIDList( pidlDestination, szPath) ;
// 取得快捷方式名稱
CNameDlg name_dlg;
if(name_dlg.DoModal() == IDCANCEL)
return FALSE ;
//把快捷方式名和擴展名.LNK添加到路徑名后
//形成完整的快捷方式數(shù)據(jù)文件名
wsprintf(szPath+lstrlen(szPath),"\\%s.lnk",
name_dlg.m_strName) ;
return TRUE ;
}
//創(chuàng)建快捷方式
BOOL CShortCutDlg::CreateLink (
LPSTR szPath,//快捷方式的目標應用程序名
LPSTR szLink)//快捷方式的數(shù)據(jù)文件名(*.lnk)
{
HRESULT hres ;
IShellLink * psl ;
IPersistFile* ppf ;
WORD wsz[ MAX_PATH] ;
//創(chuàng)建一個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接口
//用于保存快捷方式的數(shù)據(jù)文件 (*.lnk)
hres = psl -> QueryInterface( IID_IPersistFile,
(void**)&ppf) ;
if( FAILED( hres))
return FALSE ;
// 確保數(shù)據(jù)文件名為ANSI格式
MultiByteToWideChar( CP_ACP, 0, szLink, -1,
wsz, MAX_PATH) ;
//調(diào)用IPersistFile::Save
//保存快捷方式的數(shù)據(jù)文件 (*.lnk)
hres = ppf -> Save( wsz, STGM_READWRITE) ;
//釋放IPersistFile和IShellLink接口
ppf -> Release( ) ;
psl -> Release( ) ;
return TRUE;
}
BOOL CShortCutDlg::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 ;
// 刪除文件夾及其內(nèi)容
if( 0 != SHFileOperation( &fos))
return FALSE ;
return TRUE;
}
//取得要刪除的快捷方式
BOOL CShortCutDlg::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 ;
}
//刪除快捷方式的數(shù)據(jù)文件 (*.lnk)
BOOL CShortCutDlg::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 CShortCutDlg::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);
}
void CShortCutDlg::OnCreateGroup()
{
UpdateData(TRUE);
if(m_nLocation==0)
{
//設置起始文件夾為桌面
nBeginAt=CSIDL_DESKTOPDIRECTORY ;
}
else
{
//設置起始文件夾為“開始”菜單
nBeginAt=CSIDL_STARTMENU;
}
LPITEMIDLIST pidlBeginAt, pidlDestination ;
char szPath[ MAX_PATH] ;
// 取得開始菜單或桌面的PIDL
SHGetSpecialFolderLocation( HWND_DESKTOP,
nBeginAt, &pidlBeginAt) ;
// 取得新建文件夾的父文件夾
if( !BrowseForFolder(pidlBeginAt ,
&pidlDestination,
"請選擇新建文件夾/菜單組的位置:"))
return ;
// 把PIDL轉(zhuǎn)換為路徑名
SHGetPathFromIDList( pidlDestination, szPath) ;
//取得新建文件夾的名字
CNameDlg name_dlg;
if(name_dlg.DoModal() == IDCANCEL)
return;
//形成完整的新建文件夾名
wsprintf(szPath+lstrlen(szPath),"\\%s",
name_dlg.m_strName);
//創(chuàng)建文件夾(子目錄)
if( !CreateDirectory( szPath, NULL))
{
MessageBox( "創(chuàng)建文件夾失敗!") ;
return ;
}
// 通知shell有關變化
NotifyShell( SHCNE_MKDIR|SHCNE_INTERRUPT,
szPath);
}
void CShortCutDlg::OnCreateItem()
{
UpdateData(TRUE);
if(m_nLocation==0)
{
//設置起始文件夾為桌面
nBeginAt=CSIDL_DESKTOPDIRECTORY ;
}
else
{
//設置起始文件夾為“開始”菜單
nBeginAt=CSIDL_STARTMENU;
}
char szPath[MAX_PATH]="";
//快捷方式的目標應用程序名
char szLink[MAX_PATH]="";
//快捷方式的數(shù)據(jù)文件名
// 取得快捷方式的目標應用程序名
if( !SelectMenuItem( szPath))
return ;
// 取得新建快捷方式所在的文件夾
//并形成其數(shù)據(jù)文件名
if( !GetShortcutCrt( szLink))
return ;
// 創(chuàng)建快捷方式
if(!CreateLink( szPath, szLink) )
return;
// 通知shell有關變化
NotifyShell( SHCNE_CREATE|SHCNE_INTERRUPT,
szLink) ;
}
void CShortCutDlg::OnDeleteGroup()
{
UpdateData(TRUE);
if(m_nLocation==0)
{
//設置起始文件夾為桌面
nBeginAt=CSIDL_DESKTOPDIRECTORY ;
}
else
{
//設置起始文件夾為“開始”菜單
nBeginAt=CSIDL_STARTMENU;
}
LPITEMIDLIST pidlBeginAt, pidlFolder ;
char szPath[MAX_PATH]="";
// 取得開始菜單或桌面的PIDL
SHGetSpecialFolderLocation( HWND_DESKTOP,
nBeginAt, &pidlBeginAt) ;
// 取得要刪除的文件夾
if( !BrowseForFolder( pidlBeginAt, &pidlFolder,
"請選擇要刪除的文件夾/菜單組:"))
return ;
// 把PIDL轉(zhuǎn)化為路徑名
SHGetPathFromIDList( pidlFolder, szPath) ;
// 刪除文件夾
if(!DeleteFolder( szPath))
return ;
// 通知shell有關變化
NotifyShell( SHCNE_RMDIR|SHCNE_INTERRUPT,
szPath) ;
}
void CShortCutDlg::OnDeleteItem()
{
UpdateData(TRUE);
if(m_nLocation==0)
{
//設置起始文件夾為桌面
nBeginAt=CSIDL_DESKTOPDIRECTORY ;
}
else
{
//設置起始文件夾為“開始”菜單
nBeginAt=CSIDL_STARTMENU;
}
LPITEMIDLIST pidlBeginAt ;
char szShortcut[ MAX_PATH]="",
szPath[ MAX_PATH]="";
// 取得開始菜單或桌面的PIDL
SHGetSpecialFolderLocation( HWND_DESKTOP,
nBeginAt, &pidlBeginAt) ;
// 把PIDL轉(zhuǎn)化為路徑名
SHGetPathFromIDList( pidlBeginAt, szPath) ;
// 取得要刪除的快捷方式
if( !GetShortcutDel( szPath, szShortcut))
return ;
// 刪除快捷方式
if( !DeleteLink(szShortcut))
return ;
// 通知SHELL有關改變
NotifyShell( SHCNE_DELETE|SHCNE_INTERRUPT,
szShortcut) ;
}
void CShortCutDlg::OnDestroy()
{
CoUninitialize();
CDialog::OnDestroy();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -