Solved

Explorer Context Menu for One Folder Only

Posted on 2014-02-27
8
756 Views
Last Modified: 2014-03-03
Hi,

I want to Add Right Click Context Menu  for only one Folder (e.g. C:\MyFolder\)

If i right click on any file inside C:\MyFolder, My Context Menu need to display. Outside C:\MyFolder no need to display My Context Menu. (like dropbox context menu for only one folder)

I am able to global Context Menu but not able to specific Folder Context Menu.


thanks

Kalpesh
0
Comment
Question by:Kalpesh Chhatrala
  • 4
  • 4
8 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 39892590
In your context menu's 'Initialize()' (http://msdn.microsoft.com/en-us/library/windows/desktop/bb775094%28v=vs.85%29.aspx) method - which is inherited from 'IShellExtInit' - check the folder name like

HRESULT CMyContextMenu::Initialize(LPCITEMIDLIST, LPDATAOBJECT pDataObj, HKEY)
{
FORMATETC fmt = { CF_HDROP, NULL, DVASPECT_CONTENT,
                  -1, TYMED_HGLOBAL };
STGMEDIUM stg = { TYMED_HGLOBAL };
HDROP     hDrop;
 
  // Look for CF_HDROP data in the data object. If there
  // is no such data, return an error back to Explorer.
  if ( FAILED( pDataObj->GetData ( &fmt, &stg ) ))
    return E_INVALIDARG;
 
  // Get a pointer to the actual data.
  hDrop = (HDROP) GlobalLock ( stg.hGlobal );
 
  // Make sure it worked.
  if ( NULL == hDrop )
    return E_INVALIDARG;

  TCHAR acFile[MAX_PATH];

  DragQueryFile(hDrop,	i,acFile,sizeof( acFile));

  if (!_tcscmp(acFile,_T("c:\\MyFolder"))) 
    m_bShowMenu = TRUE; // evaluate this flag in 'QueryContextMenu()' to check if you should display the menu

  //...

  return S_OK;
}

Open in new window

0
 
LVL 16

Author Comment

by:Kalpesh Chhatrala
ID: 39892611
I need code in C#. by mistake i added C++ as Topic.
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 39892687
That works in C# as well - see http://www.codeproject.com/Articles/174369/How-to-Write-Windows-Shell-Extension-with-NET-Lang ("How to Write Windows Shell Extension with .NET Languages") - i.e.

        public void Initialize(IntPtr pidlFolder, IntPtr pDataObj, IntPtr hKeyProgID)
        {
            if (pDataObj == IntPtr.Zero)
            {
                throw new ArgumentException();
            }

            FORMATETC fe = new FORMATETC();
            fe.cfFormat = (short)CLIPFORMAT.CF_HDROP;
            fe.ptd = IntPtr.Zero;
            fe.dwAspect = DVASPECT.DVASPECT_CONTENT;
            fe.lindex = -1;
            fe.tymed = TYMED.TYMED_HGLOBAL;
            STGMEDIUM stm = new STGMEDIUM();

            // The pDataObj pointer contains the objects being acted upon. In this 
            // example, we get an HDROP handle for enumerating the selected files 
            // and folders.
            IDataObject dataObject = (IDataObject)Marshal.GetObjectForIUnknown(pDataObj);
            dataObject.GetData(ref fe, out stm);

            try
            {
                // Get an HDROP handle.
                IntPtr hDrop = stm.unionmember;
                if (hDrop == IntPtr.Zero)
                {
                    throw new ArgumentException();
                }

                // Determine how many files are involved in this operation.
                uint nFiles = NativeMethods.DragQueryFile(hDrop, UInt32.MaxValue, null, 0);

                // This code sample displays the custom context menu item when only 
                // one file is selected. 
                if (nFiles == 1)
                {
                    // Get the path of the file.
                    StringBuilder fileName = new StringBuilder(260);
                    if (0 == NativeMethods.DragQueryFile(hDrop, 0, fileName,
                        fileName.Capacity))
                    {
                        Marshal.ThrowExceptionForHR(WinError.E_FAIL);
                    }
                    this.selectedFile = fileName.ToString();

                    if(this.selectedFile == "c:\MyFolder") this.showMenu = true;
                }
                else
                {
                    Marshal.ThrowExceptionForHR(WinError.E_FAIL);
                }

            }
            finally
            {
                NativeMethods.ReleaseStgMedium(ref stm);
            }
        }

Open in new window

0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 16

Author Comment

by:Kalpesh Chhatrala
ID: 39893198
Hi,

Thanks for solution. its working for me for files.

I need Context Menu for Folder also. can you help me for it ?
0
 
LVL 86

Expert Comment

by:jkr
ID: 39893278
That should actually work for foldes the same way...
0
 
LVL 86

Expert Comment

by:jkr
ID: 39893292
Hm, let me re-think that... How did you register the shell extension?
0
 
LVL 16

Author Comment

by:Kalpesh Chhatrala
ID: 39893850
I used code from provided link by you.

http://www.codeproject.com/Articles/174369/How-to-Write-Windows-Shell-Extension-with-NET-Lang

just added one condition for it.
0
 
LVL 16

Author Closing Comment

by:Kalpesh Chhatrala
ID: 39900291
Thanks.

Its working fine.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question