Quick addition: "Create shortcut" menu entry.

This commit is contained in:
Sebastien Barre 2001-11-14 14:39:26 -05:00
parent f978b6f7ea
commit 669f7e7f07
4 changed files with 164 additions and 4 deletions

View File

@ -90,7 +90,7 @@ STYLE WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_POPUP | WS_VISIBLE | WS_CAPTION |
WS_SYSMENU | WS_THICKFRAME
EXSTYLE WS_EX_APPWINDOW
CAPTION "CMakeSetupDialog"
FONT 8, "MS Sans Serif"
FONT 8, "MS Sans Serif", 0, 0, 0x1
BEGIN
COMBOBOX IDC_WhereSource,96,6,133,66,CBS_DROPDOWN |
CBS_AUTOHSCROLL | WS_VSCROLL | WS_TABSTOP
@ -191,6 +191,7 @@ END
STRINGTABLE DISCARDABLE
BEGIN
IDS_ABOUTBOX "&About CMakeSetup..."
IDS_CREATESHORTCUT "&Create shortcut"
END
#endif // English (U.S.) resources

View File

@ -149,6 +149,12 @@ BOOL CMakeSetupDialog::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "Create shortcut" menu item to system menu.
// IDM_CREATESHORTCUT must be in the system command range.
ASSERT((IDM_CREATESHORTCUT & 0xFFF0) == IDM_CREATESHORTCUT);
ASSERT(IDM_CREATESHORTCUT < 0xF000);
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
@ -158,12 +164,24 @@ BOOL CMakeSetupDialog::OnInitDialog()
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strCreateShortcutMenu;
strCreateShortcutMenu.LoadString(IDS_CREATESHORTCUT);
if (!strCreateShortcutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING,
IDM_CREATESHORTCUT,
strCreateShortcutMenu);
}
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
pSysMenu->AppendMenu(MF_STRING,
IDM_ABOUTBOX,
strAboutMenu);
}
}
@ -207,6 +225,10 @@ void CMakeSetupDialog::OnSysCommand(UINT nID, LPARAM lParam)
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else if ((nID & 0xFFF0) == IDM_CREATESHORTCUT)
{
CreateShortcut();
}
else
{
CDialog::OnSysCommand(nID, lParam);
@ -816,3 +838,137 @@ void CMakeSetupDialog::OnEditchangeGenerator()
// TODO: Add your control notification handler code here
}
// Create a shortcut on the desktop with the current Source/Build dir.
int CMakeSetupDialog::CreateShortcut()
{
// m_WhereSource = cmdInfo.m_WhereSource;
// m_WhereBuild = cmdInfo.m_WhereBuild;
// Find the desktop folder and create the link name
HKEY hKey;
if(RegOpenKeyEx(HKEY_CURRENT_USER,
"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders",
0, KEY_READ, &hKey) != ERROR_SUCCESS)
{
AfxMessageBox ("Create shortcut: unable to find 'Shell Folders' key in registry!");
return 1;
}
DWORD dwType, dwSize;
#define MAXPATH 1024
char link_name[MAXPATH];
dwSize = MAXPATH;
if(RegQueryValueEx(hKey,
(LPCTSTR)"Desktop",
NULL,
&dwType,
(BYTE *)link_name,
&dwSize) != ERROR_SUCCESS)
{
AfxMessageBox ("Create shortcut: unable to find 'Desktop' registry value in 'Shell Folders' key!");
return 1;
}
if(dwType != REG_SZ)
{
AfxMessageBox ("Create shortcut: 'Desktop' registry value in 'Shell Folders' key has wrong type!");
return 1;
}
strcat(link_name, "\\CMake - ");
std::string current_dir = cmSystemTools::GetFilenameName((LPCTSTR)m_WhereSource);
strcat(link_name, current_dir.c_str());
strcat(link_name, ".lnk");
// Find the path to the current executable
char path_to_current_exe[MAXPATH];
::GetModuleFileName(NULL, path_to_current_exe, MAXPATH);
// Create the shortcut
HRESULT hres;
IShellLink *psl;
// Initialize the COM library
hres = CoInitialize(NULL);
if (! SUCCEEDED (hres))
{
AfxMessageBox ("Create shortcut: unable to initialize the COM library!");
return 1;
}
// Create an IShellLink object and get a pointer to the IShellLink
// interface (returned from CoCreateInstance).
hres = CoCreateInstance(CLSID_ShellLink,
NULL,
CLSCTX_INPROC_SERVER,
IID_IShellLink,
(void **)&psl);
if (! SUCCEEDED (hres))
{
AfxMessageBox ("Create shortcut: unable to create IShellLink instance!");
return 1;
}
IPersistFile *ppf;
// Query IShellLink for the IPersistFile interface for
// saving the shortcut in persistent storage.
hres = psl->QueryInterface(IID_IPersistFile, (void **)&ppf);
if (SUCCEEDED (hres))
{
// Set the path to the shortcut target.
hres = psl->SetPath(path_to_current_exe);
if (! SUCCEEDED (hres))
{
AfxMessageBox ("Create shortcut: SetPath failed!");
}
// Set the arguments of the shortcut.
CString args = " /H=" + m_WhereSource + " /B=" + m_WhereBuild;
hres = psl->SetArguments(args);
if (! SUCCEEDED (hres))
{
AfxMessageBox ("Create shortcut: SetArguments failed!");
}
// Set the description of the shortcut.
hres = psl->SetDescription("Shortcut to CMakeSetup");
if (! SUCCEEDED (hres))
{
AfxMessageBox ("Create shortcut: SetDescription failed!");
}
// Ensure that the string consists of ANSI characters.
WORD wsz[MAX_PATH];
MultiByteToWideChar(CP_ACP, 0, link_name, -1, wsz, MAX_PATH);
// Save the shortcut via the IPersistFile::Save member function.
hres = ppf->Save(wsz, TRUE);
if (! SUCCEEDED (hres))
{
AfxMessageBox ("Create shortcut: Save failed!");
}
// Release the pointer to IPersistFile.
ppf->Release ();
}
// Release the pointer to IShellLink.
psl->Release ();
return 0;
}

View File

@ -65,7 +65,8 @@ protected:
void FillCacheGUIFromCacheManager();
// copy from the list box to the cache manager
void FillCacheManagerFromCacheGUI();
// Create a shortcut on the desktop with the current Source/Build dir.
int CreateShortcut();
HICON m_hIcon;
CString m_RegistryKey;

View File

@ -3,9 +3,11 @@
// Used by CMakeSetup.rc
//
#define IDM_ABOUTBOX 0x0010
#define IDM_CREATESHORTCUT 0x0020
#define IDD_ABOUTBOX 100
#define IDS_ABOUTBOX 101
#define IDD_CMakeSetupDialog_DIALOG 102
#define IDS_CREATESHORTCUT 102
#define IDR_MAINFRAME 128
#define IDC_WhereSource 1001
#define IDC_BUTTON2 1002
@ -28,6 +30,6 @@
#define _APS_NEXT_RESOURCE_VALUE 133
#define _APS_NEXT_COMMAND_VALUE 32771
#define _APS_NEXT_CONTROL_VALUE 1021
#define _APS_NEXT_SYMED_VALUE 101
#define _APS_NEXT_SYMED_VALUE 102
#endif
#endif