Exemplo n.º 1
0
 public static extern bool InsertMenuItem(
     IntPtr hMenu,
     uint uItem,
     [MarshalAs(UnmanagedType.Bool)] bool fByPosition,
     ref MENUITEMINFO mii);
Exemplo n.º 2
0
        /// <summary>
        /// Add commands to a shortcut menu. 메뉴추가
        /// </summary>
        /// <param name="hMenu">A handle to the shortcut menu.</param>
        /// <param name="iMenu">
        /// The zero-based position at which to insert the first new menu item.
        /// </param>
        /// <param name="idCmdFirst">
        /// The minimum value that the handler can specify for a menu item ID.
        /// </param>
        /// <param name="idCmdLast">
        /// The maximum value that the handler can specify for a menu item ID.
        /// </param>
        /// <param name="uFlags">
        /// Optional flags that specify how the shortcut menu can be changed.
        /// </param>
        /// <returns>
        /// If successful, returns an HRESULT value that has its severity value set
        /// to SEVERITY_SUCCESS and its code value set to the offset of the largest
        /// command identifier that was assigned, plus one.
        /// </returns>
        public int QueryContextMenu(
            IntPtr hMenu,
            uint iMenu,
            uint idCmdFirst,
            uint idCmdLast,
            uint uFlags)
        {
            // If uFlags include CMF_DEFAULTONLY then we should not do anything.
            if (((uint)CMF.CMF_DEFAULTONLY & uFlags) != 0)
            {
                return(WinError.MAKE_HRESULT(WinError.SEVERITY_SUCCESS, 0, 0));
            }


            // ORIGINAL
            // Use either InsertMenu or InsertMenuItem to add menu items.

            /*
             * MENUITEMINFO mii = new MENUITEMINFO();
             * mii.cbSize = (uint)Marshal.SizeOf(mii);
             * mii.fMask = MIIM.MIIM_BITMAP | MIIM.MIIM_STRING | MIIM.MIIM_FTYPE | MIIM.MIIM_ID | MIIM.MIIM_STATE;
             * mii.wID = idCmdFirst + itemCount++;
             * mii.fType = MFT.MFT_STRING;
             * mii.dwTypeData = this.menuText;
             * mii.fState = MFS.MFS_ENABLED;
             * mii.hbmpItem = this.menuBmp;
             * if (!NativeMethods.InsertMenuItem(hMenu, iMenu, true, ref mii))
             * {
             *  return Marshal.GetHRForLastWin32Error();
             * }
             */

            IntPtr hSubMenu = NativeMethods.CreatePopupMenu();

            // 메인 메뉴를 만든다.
            MENUITEMINFO mii2 = new MENUITEMINFO();

            mii2.cbSize     = (uint)Marshal.SizeOf(mii2);
            mii2.fMask      = MIIM.MIIM_BITMAP | MIIM.MIIM_SUBMENU | MIIM.MIIM_STRING | MIIM.MIIM_FTYPE | MIIM.MIIM_ID | MIIM.MIIM_STATE;
            mii2.hSubMenu   = hSubMenu;
            mii2.wID        = idCmdFirst + itemCount;
            mii2.fType      = MFT.MFT_STRING;
            mii2.dwTypeData = this.menuText;
            mii2.fState     = MFS.MFS_ENABLED;
            //mii2.hbmpItem = this.menuBmp;


            uint subItemCount = 0;

            //SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindows();
            //string filename;



            foreach (string file in selectedFiles)
            {
                MENUITEMINFO mii3 = new MENUITEMINFO();
                mii3.cbSize     = (uint)Marshal.SizeOf(mii2);
                mii3.fMask      = MIIM.MIIM_STRING | MIIM.MIIM_FTYPE | MIIM.MIIM_ID | MIIM.MIIM_STATE;
                mii3.wID        = idCmdFirst + itemCount;
                mii3.fType      = MFT.MFT_STRING;
                mii3.dwTypeData = file;
                mii3.fState     = MFS.MFS_ENABLED;
                //mii3.hbmpItem = this.menuBmp;

                if (!NativeMethods.InsertMenuItem(hSubMenu, subItemCount++, true, ref mii3))
                {
                    return(Marshal.GetHRForLastWin32Error());
                }

                pathMap.Add((int)itemCount, this.selectedFile);
                itemCount++;
            }



            //foreach (SHDocVw.InternetExplorer ie in shellWindows)
            //{

            //	/*
            //string[] paths = {"path1", "path2", "path3"};
            //foreach(string s in paths ) {
            //	*/

            //	filename = Path.GetFileNameWithoutExtension(ie.FullName).ToLower();


            //	if (filename.Equals("explorer"))
            //	{
            //		string ExplorerWindowPath = ie.LocationURL;
            //		ExplorerWindowPath = ExplorerWindowPath.Replace("file:///", "");


            //		// Filters out the current explorer window

            //		// Had a file in c:\ and it matched c:\alertus-dev...
            //		if (/*Path.GetDirectoryName(selectedFile) != Path.GetDirectoryName(ExplorerWindowPath) && */
            //			!String.IsNullOrEmpty(ExplorerWindowPath) )
            //		{
            //			  // Subitem1
            //			MENUITEMINFO mii3 = new MENUITEMINFO();
            //			mii3.cbSize = (uint)Marshal.SizeOf(mii2);
            //			mii3.fMask = MIIM.MIIM_STRING | MIIM.MIIM_FTYPE | MIIM.MIIM_ID | MIIM.MIIM_STATE;
            //			mii3.wID = idCmdFirst + itemCount;
            //			mii3.fType = MFT.MFT_STRING;
            //			mii3.dwTypeData = ExplorerWindowPath;
            //			mii3.fState = MFS.MFS_ENABLED;
            //			//mii3.hbmpItem = this.menuBmp;

            //			if (!NativeMethods.InsertMenuItem(hSubMenu, subItemCount++, true, ref mii3))
            //			{
            //				return Marshal.GetHRForLastWin32Error();
            //			}

            //			pathMap.Add((int)itemCount, ExplorerWindowPath);
            //			itemCount++;

            //			mii3.dwTypeData += "22";

            //			if (!NativeMethods.InsertMenuItem(hSubMenu, subItemCount++, true, ref mii3))
            //			{
            //				return Marshal.GetHRForLastWin32Error();
            //			}

            //			ExplorerWindowPath += "2";
            //			pathMap.Add((int)itemCount, ExplorerWindowPath);

            //			itemCount++;
            //		}
            //	}
            //}



            // Adding the POPUP Menu
            if (!NativeMethods.InsertMenuItem(hMenu, iMenu + 0, true, ref mii2))
            {
                return(Marshal.GetHRForLastWin32Error());
            }



            // Return an HRESULT value with the severity set to SEVERITY_SUCCESS.
            // Set the code value to the offset of the largest command identifier
            // that was assigned, plus one (1).
            return(WinError.MAKE_HRESULT(WinError.SEVERITY_SUCCESS, 0,
                                         IDM_DISPLAY + itemCount));
        }