home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / winui / shell / shellext / ctxmenu.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-10-05  |  9.4 KB  |  302 lines

  1. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  2. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  3. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  4. // PARTICULAR PURPOSE.
  5. //
  6. // Copyright (C) 1993-1997  Microsoft Corporation.  All Rights Reserved.
  7. //
  8. //  MODULE:   ctxmenu.cpp
  9. //
  10. //  PURPOSE:   Implements the IContextMenu member functions necessary to support
  11. //             the context menu portioins of this shell extension.  Context menu
  12. //             shell extensions are called when the user right clicks on a file
  13. //             (of the type registered for the shell extension--see SHELLEXT.REG
  14. //             for details on the registry entries.  In this sample, the relevant
  15. //             files are of type .GAK) in the Explorer, or selects the File menu 
  16. //             item.
  17. //
  18.  
  19. #include "priv.h"
  20. #include "shellext.h"
  21.  
  22.  
  23. //
  24. //  FUNCTION: CShellExt::QueryContextMenu(HMENU, UINT, UINT, UINT, UINT)
  25. //
  26. //  PURPOSE: Called by the shell just before the context menu is displayed.
  27. //           This is where you add your specific menu items.
  28. //
  29. //  PARAMETERS:
  30. //    hMenu      - Handle to the context menu
  31. //    indexMenu  - Index of where to begin inserting menu items
  32. //    idCmdFirst - Lowest value for new menu ID's
  33. //    idCmtLast  - Highest value for new menu ID's
  34. //    uFlags     - Specifies the context of the menu event
  35. //
  36. //  RETURN VALUE:
  37. //
  38. //
  39. //  COMMENTS:
  40. //
  41.  
  42. STDMETHODIMP CShellExt::QueryContextMenu(HMENU hMenu,
  43.                                          UINT indexMenu,
  44.                                          UINT idCmdFirst,
  45.                                          UINT idCmdLast,
  46.                                          UINT uFlags)
  47. {
  48.     ODS("CShellExt::QueryContextMenu()\r\n");
  49.  
  50.     UINT idCmd = idCmdFirst;
  51.     char szMenuText[64];
  52.     char szMenuText2[64];
  53.     char szMenuText3[64];
  54.     char szMenuText4[64];
  55.     BOOL bAppendItems=TRUE;
  56.  
  57.     if ((uFlags & 0x000F) == CMF_NORMAL)  //Check == here, since CMF_NORMAL=0
  58.     {
  59.         ODS("CMF_NORMAL...\r\n");
  60.         lstrcpy(szMenuText, "&New .GAK menu 1, Normal File");
  61.         lstrcpy(szMenuText2, "&New .GAK menu 2, Normal File");
  62.         lstrcpy(szMenuText3, "&New .GAK menu 3, Normal File");
  63.         lstrcpy(szMenuText4, "&New .GAK menu 4, Normal File");
  64.     }
  65.     else
  66.         if (uFlags & CMF_VERBSONLY)
  67.         {
  68.             ODS("CMF_VERBSONLY...\r\n");
  69.             lstrcpy(szMenuText, "&New .GAK menu 1, Shortcut File");
  70.             lstrcpy(szMenuText2, "N&ew .GAK menu 2, Shortcut File");
  71.             lstrcpy(szMenuText3, "&New .GAK menu 3, Shortcut File");
  72.             lstrcpy(szMenuText4, "&New .GAK menu 4, Shortcut File");
  73.         }
  74.     else
  75.         if (uFlags & CMF_EXPLORE)
  76.         {
  77.             ODS("CMF_EXPLORE...\r\n");
  78.             lstrcpy(szMenuText, "&New .GAK menu 1, Normal File right click in Explorer");
  79.             lstrcpy(szMenuText2, "N&ew .GAK menu 2, Normal File right click in Explorer");
  80.             lstrcpy(szMenuText3, "&New .GAK menu 3, Normal File right click in Explorer");
  81.             lstrcpy(szMenuText4, "&New .GAK menu 4, Normal File right click in Explorer");
  82.         }
  83.     else
  84.         if (uFlags & CMF_DEFAULTONLY)
  85.         {
  86.             ODS("CMF_DEFAULTONLY...\r\n");
  87.             bAppendItems = FALSE;
  88.         }
  89.     else
  90.         {
  91.             char szTemp[32];
  92.  
  93.             wsprintf(szTemp, "uFlags==>%d\r\n", uFlags);
  94.             ODS("CMF_default...\r\n");
  95.             ODS(szTemp);
  96.             bAppendItems = FALSE;
  97.         }
  98.  
  99.     if (bAppendItems)
  100.     {
  101.         InsertMenu(hMenu, indexMenu++, MF_SEPARATOR|MF_BYPOSITION, 0, NULL);
  102.         
  103.         InsertMenu(hMenu,
  104.                    indexMenu++,
  105.                    MF_STRING|MF_BYPOSITION,
  106.                    idCmd++,
  107.                    szMenuText);
  108.         
  109.         InsertMenu(hMenu, indexMenu++, MF_SEPARATOR|MF_BYPOSITION, 0, NULL);
  110.  
  111.         InsertMenu(hMenu,
  112.                    indexMenu++,
  113.                    MF_STRING|MF_BYPOSITION,
  114.                    idCmd++,
  115.                    szMenuText2);
  116.         
  117.         InsertMenu(hMenu, indexMenu++, MF_SEPARATOR|MF_BYPOSITION, 0, NULL);
  118.  
  119.         InsertMenu(hMenu,
  120.                    indexMenu++,
  121.                    MF_STRING|MF_BYPOSITION,
  122.                    idCmd++,
  123.                    szMenuText3);
  124.  
  125.         InsertMenu(hMenu,
  126.                    indexMenu++,
  127.                    MF_STRING|MF_BYPOSITION,
  128.                    idCmd++,
  129.                    szMenuText4);
  130.  
  131.         return ResultFromShort(idCmd-idCmdFirst); //Must return number of menu
  132.                                                   //items we added.
  133.    }
  134.  
  135.    return NOERROR;
  136. }
  137.  
  138. //
  139. //  FUNCTION: CShellExt::InvokeCommand(LPCMINVOKECOMMANDINFO)
  140. //
  141. //  PURPOSE: Called by the shell after the user has selected on of the
  142. //           menu items that was added in QueryContextMenu().
  143. //
  144. //  PARAMETERS:
  145. //    lpcmi - Pointer to an CMINVOKECOMMANDINFO structure
  146. //
  147. //  RETURN VALUE:
  148. //
  149. //
  150. //  COMMENTS:
  151. //
  152.  
  153. STDMETHODIMP CShellExt::InvokeCommand(LPCMINVOKECOMMANDINFO lpcmi)
  154. {
  155.     ODS("CShellExt::InvokeCommand()\r\n");
  156.  
  157.     HRESULT hr = E_INVALIDARG;
  158.  
  159.     //If HIWORD(lpcmi->lpVerb) then we have been called programmatically
  160.     //and lpVerb is a command that should be invoked.  Otherwise, the shell
  161.     //has called us, and LOWORD(lpcmi->lpVerb) is the menu ID the user has
  162.     //selected.  Actually, it's (menu ID - idCmdFirst) from QueryContextMenu().
  163.     if (!HIWORD(lpcmi->lpVerb))
  164.     {
  165.         UINT idCmd = LOWORD(lpcmi->lpVerb);
  166.  
  167.         switch (idCmd)
  168.         {
  169.             case 0:
  170.                 hr = DoGAKMenu1(lpcmi->hwnd,
  171.                                 lpcmi->lpDirectory,
  172.                                 lpcmi->lpVerb,
  173.                                 lpcmi->lpParameters,
  174.                                 lpcmi->nShow);
  175.                 break;
  176.  
  177.             case 1:
  178.                 hr = DoGAKMenu2(lpcmi->hwnd,
  179.                                 lpcmi->lpDirectory,
  180.                                 lpcmi->lpVerb,
  181.                                 lpcmi->lpParameters,
  182.                                 lpcmi->nShow);
  183.                 break;
  184.  
  185.             case 2:
  186.                 hr = DoGAKMenu3(lpcmi->hwnd,
  187.                                 lpcmi->lpDirectory,
  188.                                 lpcmi->lpVerb,
  189.                                 lpcmi->lpParameters,
  190.                                 lpcmi->nShow);
  191.                 break;
  192.  
  193.             case 3:
  194.                 hr = DoGAKMenu4(lpcmi->hwnd,
  195.                                 lpcmi->lpDirectory,
  196.                                 lpcmi->lpVerb,
  197.                                 lpcmi->lpParameters,
  198.                                 lpcmi->nShow);
  199.                 break;
  200.         }
  201.     }
  202.     return hr;
  203. }
  204.  
  205.  
  206. //
  207. //  FUNCTION: CShellExt::InvokeCommand(LPCMINVOKECOMMANDINFO)
  208. //
  209. //  PURPOSE: Called by the shell after the user has selected on of the
  210. //           menu items that was added in QueryContextMenu().
  211. //
  212. //  PARAMETERS:
  213. //    lpcmi - Pointer to an CMINVOKECOMMANDINFO structure
  214. //
  215. //  RETURN VALUE:
  216. //
  217. //
  218. //  COMMENTS:
  219. //
  220.  
  221. STDMETHODIMP CShellExt::GetCommandString(UINT idCmd,
  222.                                          UINT uFlags,
  223.                                          UINT FAR *reserved,
  224.                                          LPSTR pszName,
  225.                                          UINT cchMax)
  226. {
  227.     ODS("CShellExt::GetCommandString()\r\n");
  228.  
  229.     switch (idCmd)
  230.     {
  231.         case 0:
  232.             lstrcpy(pszName, "New menu item number 1");
  233.             break;
  234.  
  235.         case 1:
  236.             lstrcpy(pszName, "New menu item number 2");
  237.             break;
  238.  
  239.         case 2:
  240.             lstrcpy(pszName, "New menu item number 3");
  241.             break;
  242.  
  243.         case 3:
  244.             lstrcpy(pszName, "New menu item number 4");
  245.             break;
  246.     }
  247.  
  248.     return NOERROR;
  249. }
  250.  
  251. STDMETHODIMP CShellExt::DoGAKMenu1(HWND hParent,
  252.                                    LPCSTR pszWorkingDir,
  253.                                    LPCSTR pszCmd,
  254.                                    LPCSTR pszParam,
  255.                                    int iShowCmd)
  256. {
  257.     ODS("CShellExt::DoGAKMenu1()\r\n");
  258.  
  259.     MessageBox(hParent, "Menu item 1!", "Shell Extension Sample", MB_OK);
  260.  
  261.     return NOERROR;
  262. }
  263.  
  264. STDMETHODIMP CShellExt::DoGAKMenu2(HWND hParent,
  265.                                    LPCSTR pszWorkingDir,
  266.                                    LPCSTR pszCmd,
  267.                                    LPCSTR pszParam,
  268.                                    int iShowCmd)
  269. {
  270.     ODS("CShellExt::DoGAKMenu2()\r\n");
  271.  
  272.     MessageBox(hParent, "Menu item 2!", "Shell Extension Sample", MB_OK);
  273.  
  274.     return NOERROR;
  275. }
  276.  
  277. STDMETHODIMP CShellExt::DoGAKMenu3(HWND hParent,
  278.                                    LPCSTR pszWorkingDir,
  279.                                    LPCSTR pszCmd,
  280.                                    LPCSTR pszParam,
  281.                                    int iShowCmd)
  282. {
  283.     ODS("CShellExt::DoGAKMenu3()\r\n");
  284.  
  285.     MessageBox(hParent, "Menu item 3!", "Shell Extension Sample", MB_OK);
  286.  
  287.     return NOERROR;
  288. }
  289.  
  290. STDMETHODIMP CShellExt::DoGAKMenu4(HWND hParent,
  291.                                    LPCSTR pszWorkingDir,
  292.                                    LPCSTR pszCmd,
  293.                                    LPCSTR pszParam,
  294.                                    int iShowCmd)
  295. {
  296.     ODS("CShellExt::DoGAKMenu4()\r\n");
  297.  
  298.     MessageBox(hParent, "Menu item 4!", "Shell Extension Sample", MB_OK);
  299.  
  300.     return NOERROR;
  301. }
  302.