Click here to Skip to main content
15,899,126 members
Home / Discussions / C#
   

C#

 
GeneralOT Pin
DavidNohejl20-Apr-05 0:17
DavidNohejl20-Apr-05 0:17 
GeneralRe: Custom EventHandler Pin
eggie519-Apr-05 16:01
eggie519-Apr-05 16:01 
GeneralRe: Custom EventHandler Pin
Christian Graus19-Apr-05 16:05
protectorChristian Graus19-Apr-05 16:05 
GeneralRe: Custom EventHandler Pin
eggie519-Apr-05 17:02
eggie519-Apr-05 17:02 
GeneralMP3 player Pin
Kiki9919-Apr-05 12:40
Kiki9919-Apr-05 12:40 
GeneralRe: MP3 player Pin
Polis Pilavas19-Apr-05 12:48
Polis Pilavas19-Apr-05 12:48 
GeneralRight Click Explorer Shell Extension Pin
makaveli78919-Apr-05 11:35
makaveli78919-Apr-05 11:35 
GeneralRe: Right Click Explorer Shell Extension Pin
TylerBrinks19-Apr-05 11:49
TylerBrinks19-Apr-05 11:49 
You need to use COM interfaces to create a shell menu item handler. The interfaces you'll need are

[ComImport(), 
InterfaceType(ComInterfaceType.InterfaceIsIUnknown), 
GuidAttribute("000214e8-0000-0000-c000-000000000046")]
internal interface IShellExtInit
{
[PreserveSig()]
int Initialize (IntPtr pidlFolder, IntPtr lpdobj, uint /*HKEY*/ hKeyProgID);
}
	
[ComImport(), 
InterfaceType(ComInterfaceType.InterfaceIsIUnknown), 
GuidAttribute("000214e4-0000-0000-c000-000000000046")]
internal interface IContextMenu
{
[PreserveSig()]
int QueryContextMenu(HMenu hmenu, int iMenu, int idCmdFirst, int idCmdLast, CMF uFlags);
[PreserveSig()]
void InvokeCommand (IntPtr pici);
[PreserveSig()]
void GetCommandString(int idcmd, uint uflags, int reserved, StringBuilder commandstring, int cch);
}


You'll create a class that implements both interfaces. Your class will need a Guid attribute so it can be registered as a COM object with the shell.

The menu code you'll need will be similar to this

int IContextMenu.QueryContextMenu(HMenu hMenu, int iMenu, int idCmdFirst, int idCmdLast, CMF uFlags)
{
StringBuilder sb = new StringBuilder(1024);
uint selected = Helpers.DragQueryFile(_hDrop, 0xffffffff, null, 0);
if(selected > 0)
{
        _fileArray = new string[(int)selected];
	for(uint i = 0; i < selected; i++)
	{
		Helpers.DragQueryFile(_hDrop, i, sb, sb.Capacity + 1);
                fileArray[i] = sb.ToString();
	}
}


int id = 1;
if ( (uFlags & (CMF.CMF_VERBSONLY | CMF.CMF_DEFAULTONLY | CMF.CMF_NOVERBS)) == 0 || (uFlags & CMF.CMF_EXPLORE) != 0)
{
	HMenu submenu = Helpers.CreatePopupMenu();
	Helpers.AppendMenu(submenu, MFMENU.MF_STRING | MFMENU.MF_ENABLED, new IntPtr(idCmdFirst + id++), "Custom Sub Item 1");
Helpers.AppendMenu(submenu, MFMENU.MF_STRING | MFMENU.MF_ENABLED, new IntPtr(idCmdFirst + id++), "Custom Sub Item 2");

Helpers.InsertMenu(hMenu, 7, MFMENU.MF_BYPOSITION | MFMENU.MF_POPUP | MFMENU.MF_ENABLED, submenu.handle, "My Item");				
	}
return id;
}
		

void IContextMenu.GetCommandString(int idCmd, uint uFlags, int pwReserved, StringBuilder commandString, int cchMax)
{
	switch((GCS)uFlags)
	{
		default:
			break;
	}
}

		
void IContextMenu.InvokeCommand(IntPtr pici)
{
	try
	{
		Type typINVOKECOMMANDINFO = Type.GetType("My.Full.Namespace.INVOKECOMMANDINFO");
INVOKECOMMANDINFO ici = (INVOKECOMMANDINFO)Marshal.PtrToStructure(pici, typINVOKECOMMANDINFO);
				
	switch (ici.verb - 1)
	{
		case 0:
			MyMethod();
			break;

		case 1:
			MyOtherMethod();
			break;
		}
	}
	catch
	{
		// Prevent the shell from crashing/locking
	}
}

int IShellExtInit.Initialize(IntPtr pidlFolder, IntPtr lpdobj, uint hKeyProgID)
{
	try
	{
		// save the information about the selection
		_dataObject = null;
		if (lpdobj != (IntPtr)0)
		{
			_dataObject = (IDataObject)Marshal.GetObjectForIUnknown(lpdobj);
			FORMATETC fmt = new FORMATETC();
			fmt.cfFormat = CLIPFORMAT.CF_HDROP;
			fmt.ptd		 = 0;
			fmt.dwAspect = DVASPECT.DVASPECT_CONTENT;
			fmt.lindex	 = -1;
			fmt.tymed	 = TYMED.TYMED_HGLOBAL;
			STGMEDIUM medium = new STGMEDIUM();
			_dataObject.GetData(ref fmt, ref medium);
			_hDrop = medium.hGlobal;
		}
	}
	catch(Exception)
	{
	}
	return 0;
}

#region MIIM
	internal enum MIIM : uint
	{
		STATE =			0x00000001,
		ID =	        0x00000002,
		SUBMENU	=		0x00000004,
		CHECKMARKS =	0x00000008,
		TYPE =			0x00000010,
		DATA =			0x00000020,
		STRING =		0x00000040,
		BITMAP =		0x00000080,
		FTYPE =			0x00000100
	}

	#endregion

	#region MF
	internal enum MF : uint
	{
		INSERT =        0x00000000,
		CHANGE =        0x00000080,
		APPEND =        0x00000100,
		DELETE =        0x00000200,
		REMOVE =        0x00001000,
		BYCOMMAND =     0x00000000,
		BYPOSITION =    0x00000400,
		SEPARATOR =     0x00000800,
		ENABLED =       0x00000000,
		GRAYED =        0x00000001,
		DISABLED =      0x00000002,
		UNCHECKED =     0x00000000,
		CHECKED =       0x00000008,
		USECHECKBITMAPS=0x00000200,
		STRING =        0x00000000,
		BITMAP =        0x00000004,
		OWNERDRAW =     0x00000100,
		POPUP =         0x00000010,
		MENUBARBREAK =  0x00000020,
		MENUBREAK =     0x00000040,
		UNHILITE =      0x00000000,
		HILITE =        0x00000080,
		DEFAULT =       0x00001000,
		SYSMENU =       0x00002000,
		HELP =          0x00004000,
		RIGHTJUSTIFY =  0x00004000,
		MOUSESELECT =   0x00008000
	}

	#endregion

	#region CLIPFORMAT
	internal enum CLIPFORMAT : uint
	{
		CF_TEXT =			1,
		CF_BITMAP =			2,
		CF_METAFILEPICT =	 3,
		CF_SYLK =			4,
		CF_DIF =			5,
		CF_TIFF =			6,
		CF_OEMTEXT =		7,
		CF_DIB =			8,
		CF_PALETTE =		9,
		CF_PENDATA =		10,
		CF_RIFF =			11,
		CF_WAVE =			12,
		CF_UNICODETEXT =	13,
		CF_ENHMETAFILE =	14,
		CF_HDROP =			15,
		CF_LOCALE =			16,
		CF_MAX =			17,
		CF_OWNERDISPLAY =	0x0080,
		CF_DSPTEXT =		0x0081,
		CF_DSPBITMAP =		0x0082,
		CF_DSPMETAFILEPICT =0x0083,
		CF_DSPENHMETAFILE = 0x008E,
		CF_PRIVATEFIRST =	0x0200,
		CF_PRIVATELAST =	0x02FF,
		CF_GDIOBJFIRST =	0x0300,
		CF_GDIOBJLAST =		0x03FF
	}

	#endregion
	
	#region DVASPECT
	internal enum DVASPECT: uint
	{
		DVASPECT_CONTENT = 1,
		DVASPECT_THUMBNAIL = 2,
		DVASPECT_ICON = 4,
		DVASPECT_DOCPRINT = 8
	}

	#endregion
	
	#region TYMED
	internal enum TYMED: uint
	{
		TYMED_HGLOBAL = 1,
		TYMED_FILE =	2,
		TYMED_ISTREAM = 4,
		TYMED_ISTORAGE= 8,
		TYMED_GDI =		16,
		TYMED_MFPICT =	32,
		TYMED_ENHMF	=	64,
		TYMED_NULL=		0
    }

	#endregion
	
	#region CMF
	internal enum CMF: uint
	{
		CMF_NORMAL		= 0x00000000,
		CMF_DEFAULTONLY	= 0x00000001,
		CMF_VERBSONLY	= 0x00000002,
		CMF_EXPLORE		= 0x00000004,
		CMF_NOVERBS		= 0x00000008,
		CMF_CANRENAME	= 0x00000010,
		CMF_NODEFAULT	= 0x00000020,
		CMF_INCLUDESTATIC= 0x00000040,
		CMF_RESERVED	= 0xffff0000      // View specific
	}

	#endregion
	
	#region GCS
	internal enum GCS: uint
	{
		VERBA =			0x00000000,     // canonical verb
		HELPTEXTA =		0x00000001,     // help text (for status bar)
		VALIDATEA =		0x00000002,     // validate command exists
		VERBW =			0x00000004,     // canonical verb (unicode)
		HELPTEXTW =		0x00000005,     // help text (unicode version)
		VALIDATEW =		0x00000006,     // validate command exists (unicode)
		UNICODE =		0x00000004,     // for bit testing - Unicode string
		VERB =			GCS.VERBA,
		HELPTEXT =		GCS.HELPTEXTA,
		VALIDATE =		GCS.VALIDATEA
	}

	#endregion
	
	#region MENUITEMINFO
	[StructLayout(LayoutKind.Sequential)]
	internal struct MENUITEMINFO
	{
		public uint cbSize;
		public uint fMask;
		public uint fType;
		public uint fState;
		public int	wID;
		public int	/*HMENU*/	  hSubMenu;
 		public int	/*HBITMAP*/   hbmpChecked;
		public int	/*HBITMAP*/	  hbmpUnchecked;
		public int	/*ULONG_PTR*/ dwItemData;
		public String dwTypeData;
		public uint cch;
		public int /*HBITMAP*/ hbmpItem;
	}
	#endregion
	
	#region FORMATETC
	[StructLayout(LayoutKind.Sequential)]
	internal struct FORMATETC
	{
		public CLIPFORMAT	cfFormat;
		public uint			ptd;
		public DVASPECT		dwAspect;
		public int			lindex;
		public TYMED		tymed;
	}

	#endregion

	#region STGMEDIUM
	[StructLayout(LayoutKind.Sequential)]
	internal struct STGMEDIUM
	{
	  public uint tymed;
	  public uint hGlobal;
	  public uint pUnkForRelease;
	}

	#endregion
	
	#region INVOKECOMMANDINFO
	[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
	internal struct INVOKECOMMANDINFO
	{
		//NOTE: When SEE_MASK_HMONITOR is set, hIcon is treated as hMonitor
		public uint cbSize;						// sizeof(CMINVOKECOMMANDINFO)
		public uint fMask;						// any combination of CMIC_MASK_*
		public uint wnd;						// might be NULL (indicating no owner window)
		public int verb;
		[MarshalAs(UnmanagedType.LPStr)]
		public string parameters;				// might be NULL (indicating no parameter)
		[MarshalAs(UnmanagedType.LPStr)]
		public string directory;				// might be NULL (indicating no specific directory)
		public int Show;						// one of SW_ values for ShowWindow() API
		public uint HotKey;
		public uint hIcon;
	}

	#endregion
	
	#region IDataObject
	[ComImport(), InterfaceType(ComInterfaceType.InterfaceIsIUnknown), GuidAttribute("0000010e-0000-0000-C000-000000000046")]
	internal interface IDataObject
	{
		[PreserveSig()]
		int GetData(ref FORMATETC a, ref STGMEDIUM b);
		[PreserveSig()]
		void GetDataHere(int a, ref STGMEDIUM b);
		[PreserveSig()]
		int QueryGetData(int a);
		[PreserveSig()]
		int GetCanonicalFormatEtc(int a, ref int b);
		[PreserveSig()]
		int SetData(int a, int b, int c);
		[PreserveSig()]
		int EnumFormatEtc(uint a, ref Object b);
		[PreserveSig()]
		int DAdvise(int a, uint b, Object c, ref uint d);
		[PreserveSig()]
		int DUnadvise(uint a);
		[PreserveSig()]
		int EnumDAdvise(ref Object a);
	}

	#endregion

GeneralRe: Right Click Explorer Shell Extension Pin
makaveli78919-Apr-05 12:16
makaveli78919-Apr-05 12:16 
GeneralRe: Right Click Explorer Shell Extension Pin
TylerBrinks19-Apr-05 12:19
TylerBrinks19-Apr-05 12:19 
GeneralAuto Insert Interface and Base Class members - Not working Pin
Tristan Rhodes19-Apr-05 10:04
Tristan Rhodes19-Apr-05 10:04 
GeneralDisallowing new or delete record in a DataGrid Pin
Luis Alonso Ramos19-Apr-05 9:22
Luis Alonso Ramos19-Apr-05 9:22 
GeneralRe: Disallowing new or delete record in a DataGrid Pin
Jim Matthews19-Apr-05 10:35
Jim Matthews19-Apr-05 10:35 
GeneralRe: Disallowing new or delete record in a DataGrid Pin
Luis Alonso Ramos19-Apr-05 12:15
Luis Alonso Ramos19-Apr-05 12:15 
GeneralSending MMS form mobile to pc Pin
Member 151404119-Apr-05 9:08
Member 151404119-Apr-05 9:08 
QuestionHow to add 3D world scene in a movie ? Pin
Tifa_Mido19-Apr-05 8:49
Tifa_Mido19-Apr-05 8:49 
QuestionCan i add a background image to the Windows datagrid? Pin
RajeshGuptha19-Apr-05 8:14
RajeshGuptha19-Apr-05 8:14 
AnswerRe: Can i add a background image to the Windows datagrid? Pin
Jim Matthews19-Apr-05 10:39
Jim Matthews19-Apr-05 10:39 
GeneralRe: Can i add a background image to the Windows datagrid? Pin
RajeshGuptha19-Apr-05 19:14
RajeshGuptha19-Apr-05 19:14 
GeneralRe: Can i add a background image to the Windows datagrid? Pin
Jim Matthews20-Apr-05 3:14
Jim Matthews20-Apr-05 3:14 
GeneralRe: Can i add a background image to the Windows datagrid? Pin
RajeshGuptha20-Apr-05 7:20
RajeshGuptha20-Apr-05 7:20 
GeneralTray Icons Pin
CStiefeling19-Apr-05 8:09
CStiefeling19-Apr-05 8:09 
GeneralRe: Tray Icons Pin
leppie19-Apr-05 19:11
leppie19-Apr-05 19:11 
QuestionDb2 connection?? Pin
trk_wakil19-Apr-05 7:38
trk_wakil19-Apr-05 7:38 
GeneralDragOver assistance needed... Pin
new_phoenix19-Apr-05 7:27
new_phoenix19-Apr-05 7:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.