(C #) Windows Shell shell programming Series 4-context menu (icontextmenu) (2) embedded menu and execution command

Source: Internet
Author: User

(This seriesArticleOriginal by lemon (lc_mtt), reprinted please indicate the source, thank you ~)

The previous section describes how to bring up the context menu of ishellfolder, that is, icontextmenu. Sometimes we need to add some menu items on this menu. For example, you can open the resource manager and check the right-click menu in the left directory tree. A collapsed/expanded menu item is added to the top layer. Well, we can also add this menu item.
Modify example 3. Locate querycontextmenu and provide a menu handle:

// Provide a handle for the pop-up menu
Intptr contextmenu = API. createpopupmenu ();
Icontextmenu. querycontextmenu (contextmenu, 0,
Api. Sort _first, Api. Sort _last, CMF. Normal | CMF. Sort E );

Then add the followingCode:

/** // Add a custom menu
String topinvoke = tree1.selectednode. isexpanded? "Fold (& A)": "Expand (& )";
MFT extraflag = (tree1.selectednode. nodes. Count> 0 )? 0: MFT. grayed;
Api. insertmenu (contextmenu, 0, MFT. byposition | extraflag,
(INT) (API. pai_last + 1), topinvoke );
// Add a separator.
Api. insertmenu (contextmenu, 1, MFT. byposition | MFT. separator, 0 ,"-");
// Set the first menu to the default menu, that is, bold
Api. setmenudefaultitem (contextmenu, 0, true );
/**//////////////////////////

Here we use the insertmenu API:

[Dllimport ("USER32 ",
Setlasterror = true,
Charset = charset. Auto)]
Public static extern bool insertmenu (
Intptr hmenu,
Uint uposition,
MFT uflags,
Uint uidnewitem,
[Financialas (unmanagedtype. lptstr)]
String lpnewitem );

Parameter 2 indicates the position where the menu item is added, starting from 0.
Parameter 3 indicates the flag. The menu status and position calculation method are provided here. It is an enumeration:

Public Enum MFT
Public Enum MFT: uint
{
Grayed = 0x00000003,
X00000003,
X00000008,
Separator = 0x00000800,
Radiocheck = 0x00000200,
Bitmap = 0x00000004,
Ownerdraw = 0x00000100,
Menubarbreak= 0x00000020,
Menubreak = 0x00000040,
Rightorder = 0x00002000,
Bycommand = 0x00000000,
Byposition = 0x00000400,
Popup = 0x00000010
}

Mf_byposition indicates that the index item is used to calculate the position. The first menu is 0, and the second menu is 1, and so on...
Parameter 4 indicates the command value. We can execute corresponding functions based on the command value.
Then you can bring up the menu:

// Pop-up menu
Uint cmd = API. trackpopupmenuex (contextmenu, TPM. returncmd,
Mouseposition. X, mouseposition. Y, this. Handle, intptr. Zero );

You can see the effect of the pop-up menu. Of course, we must also do something to respond to the execution of this menu:

// Obtain the command serial number and execute the menu command
If (CMD> = API. cmd_first)
{

// Custom menu command
If (cmd = API. cmd_last + 1)
{
If (tree1.selectednode. isexpanded)
Tree1.selectednode. Collapse ();
Else
Tree1.selectednode. Expand ();
}
}



Run the menu command
Can I directly call the corresponding command of the menu item without a menu prompt? The answer is yes.

Do you still remember how to display a file or folder attribute dialog box?

Yes, you can use shellexecuteex and specify the lpverb field of shellexecuteinfo as properties. However, this method can only view the attributes of one file. How can you view multiple attributes at the same time?

You need to know that shellexecuteex depends on icontextmenu to check the file attributes, so the answer is still on icontextmenu. We only need to pass the pidl of the file or file to be viewed as a parameter when calling getuiobjectof, then you can call the invokecommand method directly.

Of course, in our example, the property of an object is displayed and modified by yourself.

We must first obtain the icontextmenu interface:

// Obtain the icontextmenu Interface
Intptr icontextmenuptr = intptr. zero;
Icontextmenuptr = iparent. getuiobjectof (intptr. Zero, (uint) pidls. length,
Pidls, ref guids. iid_icontextmenu, out icontextmenuptr );
Icontextmenu = (icontextmenu) Marshal. getobjectforiunknown (icontextmenuptr );

But we don't bring up this menu, just call invokecommand to execute the command:

// Directly execute the command
Cminvokecommandinfoex invoke = new cminvokecommandinfoex ();
Invoke. cbsize = marshal. sizeof (typeof (cminvokecommandinfoex ));
Invoke. lpverb = marshal. stringtohglobalansi ("properties ");
Invoke. lpdirectory = string. empty;
Invoke. fmask = 0;
Invoke. nshow = 1;
Icontextmenu. invokecommand (ref invoke );

For more information about verb, see msdn. What I am doing here is to execute "attributes". If you want to execute other commands or execute according to indexes, you can also. I will not conduct any further research here.

Source code:/files/lemony/winshell4.rar
I am considering whether to talk about icons in the next section or continue to talk about icontextmenu. You also know that there are usually several WinRAR options in the context menu of a file, with cute icons.

It is very likely that the next section will show you how to achieve this effect in C. We hope you will support pai_^ in more ways.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.