What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » Tips

Tips
Author: Message:
Matti
Elite Member
*****

Avatar
Script Developer and Helper

Posts: 1646
Reputation: 39
32 / Male / Flag
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.
Plus! Script Developer | Plus! Beta Tester | Creator of Countdown Live | Co-developer of Screenshot Sender 5

Found my post useful? Rate me!
06-07-2007 05:38 PM
Profile E-Mail PM Web Find Quote Report
« Next Oldest Return to Top Next Newest »

Messages In This Thread
Tips - by surfichris on 03-13-2006 at 12:06 PM
RE: Tips - by -dt- on 03-13-2006 at 12:11 PM
RE: Tips - by segosa on 03-13-2006 at 12:39 PM
RE: Tips - by surfichris on 03-13-2006 at 01:13 PM
RE: Tips - by Volv on 04-19-2006 at 06:29 AM
RE: Tips - by -dt- on 04-19-2006 at 06:57 AM
RE: Tips - by davidt on 08-15-2006 at 09:21 AM
RE: RE: Tips - by CookieRevised on 10-21-2006 at 01:44 AM
RE: Tips - by Plan-1130 on 10-25-2006 at 08:15 PM
RE: Tips - by matty on 10-25-2006 at 08:21 PM
RE: Tips - by tryxter on 01-08-2007 at 06:46 PM
RE: Tips - by CookieRevised on 01-28-2007 at 01:15 AM
RE: Tips - by Matti on 06-02-2007 at 05:02 PM
RE: Tips - by Matti on 06-07-2007 at 05:38 PM
RE: Tips - by henry1817 on 12-30-2009 at 04:13 PM
RE: Tips - by matty on 12-30-2009 at 04:20 PM
RE: Tips - by whiz on 06-24-2010 at 10:53 AM


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On