private void deleteToolStripButton_Click(object sender, EventArgs e) { if (m_selectedPidls != null && m_selectedPidls.Length > 0) { IntPtr iContextMenuPtr; m_shellView.GetItemObject((uint)NativeMethods.SVGIO.SVGIO_SELECTION, ref NativeMethods.IID_IContextMenu, out iContextMenuPtr); NativeMethods.IContextMenu iContextMenu = (NativeMethods.IContextMenu) Marshal.GetTypedObjectForIUnknown(iContextMenuPtr, typeof (NativeMethods.IContextMenu)); NativeMethods.CMINVOKECOMMANDINFOEX cmi = new NativeMethods.CMINVOKECOMMANDINFOEX(); cmi.cbSize = Marshal.SizeOf(typeof (NativeMethods.CMINVOKECOMMANDINFOEX)); cmi.lpVerb = Marshal.StringToHGlobalAnsi("Delete"); cmi.hwnd = Handle; // Invoke the delete command of the context menu iContextMenu.InvokeCommand(ref cmi); } }
private void createNewFolderToolStripButton_Click(object sender, EventArgs e) { if (m_useCreateNewFolderDialog) { // A new folder is created through a dialog and then the folder is instantly browsed to string path = GetDisplayName(m_desktopFolder, m_pidlAbsCurrent, NativeMethods.SHGNO.SHGDN_FORPARSING); m_newFolderDialog = new NewFolderDialog(path); if (m_newFolderDialog.ShowDialog(this) == DialogResult.Cancel) { m_newFolderDialog.FolderCreated = false; m_newFolderDialog.FolderPath = string.Empty; } } else { // A new folder is created the traditional way IntPtr iContextMenuPtr; NativeMethods.IContextMenu iContextMenu; if (m_shellView.GetItemObject((uint)NativeMethods.SVGIO.SVGIO_BACKGROUND, ref NativeMethods.IID_IContextMenu, out iContextMenuPtr) == NativeMethods.S_OK) { iContextMenu = (NativeMethods.IContextMenu) Marshal.GetTypedObjectForIUnknown(iContextMenuPtr, typeof (NativeMethods.IContextMenu)); IntPtr hMenu = NativeMethods.User32.CreatePopupMenu(); if (hMenu != IntPtr.Zero) { if (iContextMenu.QueryContextMenu( hMenu, 0, NativeMethods.MIN_SHELL_ID, NativeMethods.MAX_SHELL_ID, NativeMethods.CMF_NORMAL) > -1) { IntPtr iShellViewPtr = Marshal.GetIUnknownForObject(m_shellView); IntPtr iFolderViewPtr; Marshal.QueryInterface(iShellViewPtr, ref NativeMethods.IID_IFolderView, out iFolderViewPtr); NativeMethods.IFolderView iFolderView = (NativeMethods.IFolderView)Marshal.GetObjectForIUnknown(iFolderViewPtr); int nCount; iFolderView.ItemCount((int)NativeMethods.SVGIO.SVGIO_ALLVIEW, out nCount); for (int i = 0; i < nCount; i++) { iFolderView.SelectItem(i, NativeMethods.SVSI_DESELECT); } NativeMethods.CMINVOKECOMMANDINFOEX cmi = new NativeMethods.CMINVOKECOMMANDINFOEX(); cmi.cbSize = Marshal.SizeOf(typeof (NativeMethods.CMINVOKECOMMANDINFOEX)); cmi.lpVerb = Marshal.StringToHGlobalAnsi("NewFolder"); cmi.lpVerbW = Marshal.StringToHGlobalUni("NewFolder"); cmi.nShow = NativeMethods.SW_SHOWNORMAL; cmi.hwnd = Handle; cmi.fMask = NativeMethods.CMIC_MASK_UNICODE | NativeMethods.CMIC_MASK_FLAG_NO_UI; // Invoke the new folder command of the context menu if (iContextMenu.InvokeCommand(ref cmi) == NativeMethods.S_OK) { iFolderView.ItemCount((int)NativeMethods.SVGIO.SVGIO_ALLVIEW, out nCount); iFolderView.SelectItem(nCount - 1, NativeMethods.SVSI_EDIT); } Marshal.ReleaseComObject(iFolderView); Marshal.Release(iFolderViewPtr); Marshal.Release(iShellViewPtr); } NativeMethods.User32.DestroyMenu(hMenu); } Marshal.ReleaseComObject(iContextMenu); } } }
private void InvokeCommand(NativeMethods.IContextMenu iContextMenu, string cmd, string parentDir) { NativeMethods.CMINVOKECOMMANDINFOEX invoke = new NativeMethods.CMINVOKECOMMANDINFOEX(); invoke.hwnd = Handle; invoke.cbSize = Marshal.SizeOf(invoke); invoke.lpVerb = Marshal.StringToHGlobalAnsi(cmd); invoke.lpDirectory = Marshal.StringToHGlobalAnsi(parentDir); invoke.lpVerbW = Marshal.StringToHGlobalUni(cmd); invoke.lpDirectoryW = Marshal.StringToHGlobalUni(parentDir); invoke.fMask = NativeMethods.CMIC_MASK_UNICODE | ((ModifierKeys & Keys.Control) != 0 ? NativeMethods.CMIC_MASK_CONTROL_DOWN : 0) | ((ModifierKeys & Keys.Shift) != 0 ? NativeMethods.CMIC_MASK_SHIFT_DOWN : 0); invoke.nShow = NativeMethods.SW_SHOWNORMAL; iContextMenu.InvokeCommand(ref invoke); }