Matti
Elite Member
Script Developer and Helper
Posts: 1646 Reputation: 39
32 / /
Joined: Apr 2004
|
RE: Tips
Show a popup menu when you right-click a ListViewControl's item
This snippet will show you how you can create a popup menu which will pop up when the user right-clicks a ListViewControl's item, and how you should retrieve the response of the menu. It uses CreatePopupMenu to create the mnu, AppendMenuW to add items and TrackPopupMenu to open it and get the clicked menu item.
This snippet uses some parts of Matty's tray icon script, which initially was meant to create a tray icon with a popup menu. In this snippet, it was modified to only create a menu and show it on right-click. Much thanks to Matty to let me use his code in this snippet!
code: //Message constants for menu
//These should be declared on global scope, so at the top of your script, outside any function
var MF_CHECKED = 0x8;
var MF_APPEND = 0x100;
var MF_DISABLED = 0x2;
var MF_GRAYED = 0x1;
var MF_SEPARATOR = 0x800;
var MF_STRING = 0x0;
var TPM_LEFTALIGN = 0x0;
var TPM_RETURNCMD = 0x0100;
var TPM_VERNEGANIMATION = 0x2000;
//Variable to store our subclass window in, needed to retrieve messages from the menu
//We don't need those messages, but MSDN says that we must pass a window handle
//Therefore, you should add a new window to your XML file, like this:
/*
<Window Id="WndSubclass" Version="1">
<Attributes>
<ShowInTaskbar>False</ShowInTaskbar>
</Attributes>
<DialogTmpl/>
<Position Width="0" Height="0"/>
</Window>
*/
var subclass = false;
//This is an example function.
//It is just to demonstrate how you should place the menu creation code
//Original CreatePopupMenu and AppendMenuW code by Matty
function WindowOpen() {
var PlusWnd = MsgPlus.CreateWnd("Windows.xml", "MyWindow"); //Create our window
hMenu = Interop.Call("user32", "CreatePopupMenu"); //Create a new popup menu and get its handle
//Add menu items
Interop.Call("user32", "AppendMenuW", hMenu, MF_STRING, 101 /*This can be any number you want, and is identifier of the item*/, "Do this");
Interop.Call("user32", "AppendMenuW", hMenu, MF_STRING, 102, "Do that");
Interop.Call("user32", "AppendMenuW", hMenu, MF_SEPARATOR, 0, 0);
Interop.Call("user32", "AppendMenuW", hMenu, MF_CHECKED, 103, "I'm checked!");
//Do anything you want to do with your window after its creation...
}
//A sample function to process the menu click
function ProcessMenuItem(PlusWnd, LVItem, MnuItem) {
//Let's see what item is clicked and react on it
switch(MnuItem) {
case 101: //Do this
DoThis();
break;
case 102: //Do that
DoThat(PlusWnd.LstView_GetItemText("LVThing", LVItem, 0)); //Send the item text of the 1st column of the right-clicked item to the function
break;
case 103: //I'm checked
ToggleCheck();
break;
}
}
//The right-click event, probably the most important piece of this snippet
function OnMyWindowEvent_LstViewRClicked(PlusWnd, CtrlId, Index) {
if(CtrlId == "LVThing") { //If our ListViewControl is clicked...
if(Index < 0) { //If the click didn't occur on an item...
return; //...do nothing
} else {
//Original GetCursorPos and TrackPopupMenu code by Matty
if(!subclass) subclass = MsgPlus.CreateWnd("Windows.xml", "WndSubclass", 2); //Create our subclass window, if it's not yet created
var POINTAPI = Interop.Allocate(8); //Create a structure to store the position of the cursor
Interop.Call("user32", "GetCursorPos", POINTAPI); //Get the position of the cursor and store it
var Result = Interop.Call("user32", "TrackPopupMenu", hMenuMore, TPM_LEFTALIGN | TPM_RETURNCMD | TPM_VERNEGANIMATION, POINTAPI.ReadDWORD(0), POINTAPI.ReadDWORD(4), 0, subclass.Handle, 0); //Open the menu and store the result
ProcessMenuItem(PlusWnd, Index, Result); //Send the result to a function for further processing
}
}
}
This post was edited on 12-06-2007 at 08:20 PM by Matti.
|
|