home *** CD-ROM | disk | FTP | other *** search
/ Mastering Computers 3 / Mastering Computers Vol 3.iso / Win95 / Fun&Utils / STRTMENU.EXE / ADDITEM.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-21  |  8.5 KB  |  310 lines

  1. /**************************************************************************
  2.  
  3.    THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  4.    ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  5.    THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  6.    PARTICULAR PURPOSE.
  7.  
  8.    Copyright (C) 1993-1995  Microsoft Corporation.  All Rights Reserved.
  9.  
  10.    File:          AddItem.cpp
  11.    
  12.    Description:   Provides the functionality for adding a shortcut to 
  13.                   the start menu.
  14.  
  15. **************************************************************************/
  16.  
  17. #define STRICT
  18.  
  19. /**************************************************************************
  20.    Include Files
  21. **************************************************************************/
  22.  
  23. #include <windows.h>
  24. #include <windowsx.h>
  25. #include <shlobj.h>
  26. #include "globals.h"
  27. #include "resource.h"
  28.  
  29. /**************************************************************************
  30.    Local Function Prototypes
  31. **************************************************************************/
  32. extern "C" {
  33.  
  34. BOOL GetShortcut(HWND, LPSTR);
  35. BOOL GetProgram(HWND, LPSTR);
  36. BOOL GetShortcutName(HWND, LPSTR);
  37. BOOL CALLBACK GetShortcutNameDlgProc(HWND, UINT, WPARAM, LPARAM);
  38. HRESULT CreateLink(LPCSTR, LPSTR, LPSTR);
  39.  
  40. }
  41.  
  42. /**************************************************************************
  43.    Global Variables
  44. **************************************************************************/
  45.  
  46. char  g_szProgram[MAX_PATH];
  47.  
  48. /**************************************************************************
  49.  
  50.    AddItem()
  51.  
  52. **************************************************************************/
  53.  
  54. BOOL AddItem(HWND hWnd)
  55. {
  56. char  szProgram[MAX_PATH],
  57.       szShortcut[MAX_PATH];
  58.  
  59. //get the program to create the shortcut for
  60. if(!GetProgram(hWnd, szProgram))
  61.    return FALSE;
  62.  
  63. //get the location of the new shortcut
  64. if(!GetShortcut(hWnd, szShortcut))
  65.    return FALSE;
  66.  
  67. //create the shortcut
  68. CreateLink(szProgram, szShortcut, "");
  69.  
  70. return TRUE;
  71. }
  72.  
  73. /**************************************************************************
  74.  
  75.    GetShortcut()
  76.  
  77. **************************************************************************/
  78.  
  79. BOOL GetShortcut(HWND hWnd, LPSTR lpszPath)
  80. {
  81. LPITEMIDLIST   pidlStartMenu,
  82.                pidlDestination;
  83. char           szTemp[MAX_PATH];
  84.  
  85. //get the pidl for the start menu
  86. SHGetSpecialFolderLocation(NULL, CSIDL_STARTMENU, &pidlStartMenu);
  87.  
  88. //get the destination folder
  89. if(!GetFolder(hWnd, &pidlDestination, pidlStartMenu, szTemp, "Select Location for Shortcut"))
  90.    return FALSE;
  91.  
  92. //get the path for the folder
  93. SHGetPathFromIDList(pidlDestination, lpszPath);
  94.  
  95. //append the shorcut filename
  96. if(!GetShortcutName(hWnd, lpszPath))
  97.    return FALSE;
  98.  
  99. //add .LNK to the shortcut file name
  100. lstrcat(lpszPath, ".lnk");
  101.  
  102. return TRUE;
  103. }
  104.  
  105. /**************************************************************************
  106.  
  107.    GetFolder()
  108.  
  109. **************************************************************************/
  110.  
  111. BOOL GetFolder(   HWND hwndParent,
  112.                   LPITEMIDLIST *ppidlDestination,
  113.                   LPITEMIDLIST pidlRoot,
  114.                   LPSTR lpszDisplayName,
  115.                   LPCSTR lpszTitle)
  116. {
  117. BROWSEINFO  BrInfo;
  118.  
  119. ZeroMemory(&BrInfo, sizeof(BrInfo));
  120. BrInfo.hwndOwner        = hwndParent;
  121. BrInfo.pidlRoot         = pidlRoot; //browse from the start menu down
  122. BrInfo.pszDisplayName   = lpszDisplayName;
  123. BrInfo.lpszTitle        = lpszTitle;
  124.  
  125. //use the shell's folder browser
  126. *ppidlDestination = SHBrowseForFolder(&BrInfo);
  127.  
  128. //did the user select the cancel button
  129. if(NULL == *ppidlDestination)
  130.    return FALSE;
  131.  
  132. return TRUE;
  133. }
  134.  
  135. /**************************************************************************
  136.  
  137.    GetProgram()
  138.  
  139. **************************************************************************/
  140.  
  141. BOOL GetProgram(HWND hWnd, LPSTR lpszSource)
  142. {
  143. BOOL           ret;
  144. OPENFILENAME   ofn;
  145. char           szFileName[_MAX_PATH] = "",
  146.                szTitleName[_MAX_FNAME + _MAX_EXT] = "";
  147.  
  148.  
  149. static CHAR szFilter[] = "Programs\0*.exe\0";
  150.  
  151. ZeroMemory(&ofn, sizeof(OPENFILENAME));
  152.  
  153. ofn.lStructSize       = sizeof (OPENFILENAME);
  154. ofn.hwndOwner         = hWnd;
  155. ofn.lpstrFilter       = szFilter;
  156. ofn.nFilterIndex      = 0;
  157. ofn.nMaxFile          = _MAX_PATH;
  158. ofn.nMaxFileTitle     = _MAX_FNAME + _MAX_EXT;
  159. ofn.lpstrTitle        = "Select Program";
  160. ofn.lpstrFile         = szFileName;
  161. ofn.lpstrFileTitle    = szTitleName;
  162. ofn.Flags             = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_EXPLORER;
  163.  
  164. ret = GetOpenFileName (&ofn) ;
  165.  
  166. if(ret)
  167.    {
  168.    lstrcpy(lpszSource, szFileName);
  169.    
  170.    GetFileTitle(szFileName, g_szProgram, sizeof(g_szProgram));
  171.    }
  172.  
  173. return ret;
  174. }
  175.  
  176. /**************************************************************************
  177.  
  178.    GetShortcutName()
  179.  
  180. **************************************************************************/
  181.  
  182. BOOL GetShortcutName(HWND hWnd, LPSTR lpszPath)
  183. {
  184. return DialogBoxParam(  g_hInstance, 
  185.                         MAKEINTRESOURCE(IDD_GET_NAME), 
  186.                         hWnd,
  187.                         GetShortcutNameDlgProc,
  188.                         (LPARAM)lpszPath);
  189. }
  190.  
  191. /**************************************************************************
  192.  
  193.    GetShortcutNameDlgProc()
  194.  
  195. **************************************************************************/
  196.  
  197. BOOL CALLBACK GetShortcutNameDlgProc(  HWND hWnd, 
  198.                                        UINT uMsg, 
  199.                                        WPARAM wParam, 
  200.                                        LPARAM lParam)
  201. {
  202. static LPSTR   lpszShorcut;
  203.  
  204. switch(uMsg)
  205.    {
  206.    case WM_INITDIALOG:
  207.       lpszShorcut = (LPSTR)lParam;
  208.       
  209.       SetWindowText(hWnd, "Enter Shortcut Name");
  210.       SetDlgItemText(hWnd, IDC_STATIC_TEXT, "Enter a name for the shortcut");
  211.       SetDlgItemText(hWnd, IDC_TEXT, g_szProgram);
  212.       return TRUE;
  213.  
  214.    case WM_COMMAND:
  215.       switch (GET_WM_COMMAND_ID(wParam, lParam))
  216.          {
  217.          case IDOK:
  218.             {
  219.             char  szTemp[MAX_PATH];
  220.             
  221.             GetDlgItemText(hWnd, IDC_TEXT, szTemp, sizeof(szTemp));
  222.             lstrcat(lpszShorcut, "\\");
  223.             lstrcat(lpszShorcut, szTemp);
  224.             EndDialog(hWnd, TRUE);
  225.             }      
  226.             break;
  227.             
  228.          case IDCANCEL:
  229.             EndDialog(hWnd, FALSE);
  230.             break;
  231.          }
  232.       return TRUE;
  233.    }
  234.    
  235. return FALSE;
  236. }
  237.  
  238. /**************************************************************************
  239.  
  240.    CreateLink()
  241.  
  242.    uses the shell's IShellLink and IPersistFile interfaces to create and 
  243.    store a shortcut to the specified object. 
  244.  
  245.    Returns the result of calling the member functions of the interfaces. 
  246.  
  247.    lpszPathObj - address of a buffer containing the path of the object 
  248.  
  249.    lpszPathLink - address of a buffer containing the path where the shell 
  250.       link is to be stored 
  251.  
  252.    lpszDesc - address of a buffer containing the description of the shell 
  253.       link 
  254.  
  255. **************************************************************************/
  256.  
  257. HRESULT CreateLink(  LPCSTR lpszSource, 
  258.                      LPSTR lpszTarget, 
  259.                      LPSTR lpszDesc) 
  260. HRESULT hres; 
  261. IShellLink* pShellLink; 
  262.  
  263. //CoInitialize must be called before this
  264. // Get a pointer to the IShellLink interface. 
  265. hres = CoCreateInstance(   CLSID_ShellLink, 
  266.                            NULL, 
  267.                            CLSCTX_INPROC_SERVER, 
  268.                            IID_IShellLink, 
  269.                            (LPVOID*)&pShellLink); 
  270. if (SUCCEEDED(hres)) 
  271.    { 
  272.    IPersistFile* pPersistFile; 
  273.  
  274.    // Set the path to the shortcut target, and add the description. 
  275.    pShellLink->SetPath(lpszSource); 
  276.    pShellLink->SetDescription(lpszDesc); 
  277.  
  278.    // Query IShellLink for the IPersistFile interface for saving the 
  279.    // shortcut in persistent storage. 
  280.    hres = pShellLink->QueryInterface(IID_IPersistFile, (LPVOID*)&pPersistFile); 
  281.  
  282.    if (SUCCEEDED(hres)) 
  283.       { 
  284.       WCHAR wsz[MAX_PATH]; 
  285.  
  286.       // Ensure that the string is ANSI. 
  287.       MultiByteToWideChar( CP_ACP, 
  288.                            0, 
  289.                            lpszTarget, 
  290.                            -1, 
  291.                            wsz, 
  292.                            MAX_PATH); 
  293.  
  294.       // Save the link by calling IPersistFile::Save. 
  295.       hres = pPersistFile->Save(wsz, TRUE); 
  296.  
  297.       if(FAILED(hres)) 
  298.          ErrorHandler();
  299.  
  300.       pPersistFile->Release(); 
  301.       } 
  302.  
  303.    pShellLink->Release(); 
  304.    } 
  305.  
  306. return hres; 
  307. }
  308.  
  309.