April 20, 2024, 04:05:46 AM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Listbox with context menu

Started by LarryMc, June 03, 2012, 06:08:39 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

LarryMc

It seems there is no built-in way to add a context menu to a listbox.
The solution is to subclass the listbox.
The following is an example of how to do it.
$include "windowssdk.inc"
$include "commctrl.inc"

CONST LISTBOX_1 = 1000
$define SUBCLASS1_ID 54321 ' any number
window d1
openwindow d1,0,0,300,202,0x80CB0080,0,"Caption",&d1_handler
CONTROL d1,@LISTBOX,"ListBox1",13,10,173,155,0x50A10140,LISTBOX_1
SetWindowSubclass(GetControlhandle(d1,LISTBOX_1), &MySubclassProc, SUBCLASS1_ID, 0)
OnInit()
waituntil IsWindowClosed(d1)
end

SUB d1_handler(),int
SELECT @MESSAGE
CASE @IDcreate
CENTERWINDOW d1
CASE @IDCLOSEWINDOW
RemoveWindowSubclass(GetControlhandle(d1,LISTBOX_1), &MySubclassProc, SUBCLASS1_ID)
CLOSEwindow d1
CASE @IDCONTROL
SELECT @CONTROLID
CASE LISTBOX_1
/* respond to control notifications here */
ENDSELECT
CASE @IDMENUPICK
         SELECT @MENUNUM
CASE 101
messagebox d1,"Option 1 was selected","Selection",0
CASE 102
messagebox d1,"Option 2 was selected","Selection",0
ENDSELECT
ENDSELECT
RETURN 0
ENDSUB


sub OnInit()
SendMessage(d1, LB_ADDSTRING, 0, "item 1", LISTBOX_1)
SendMessage(d1, LB_ADDSTRING, 0, "item 2", LISTBOX_1)
SendMessage(d1, LB_ADDSTRING, 0, "item 3", LISTBOX_1)
SendMessage(d1, LB_ADDSTRING, 0, "", LISTBOX_1)
SendMessage(d1, LB_ADDSTRING, 0, "item 5", LISTBOX_1)
endsub


sub MySubclassProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam,UINT_PTR uIdSubclass,DWORD_PTR dwRefData)
Select uMsg
case WM_RBUTTONUP
POINT cursor
GetCursorPos(cursor)
ScreenToClient(d1.hwnd, cursor)
CONTEXTMENU d1, cursor.x, cursor.y
MENUITEM "Option 1", 0, 101
MENUITEM "Option 2", 0, 102
ENDMENU
Case WM_DESTROY
RemoveWindowSubclass(hWnd,&MySubclassProc,SUBCLASS1_ID)
EndSelect
Return DefSubclassProc(hWnd,uMsg,wParam,lParam)
EndSub
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library