The MASM Forum Archive 2004 to 2012

General Forums => The Workshop => Topic started by: NCR on February 16, 2011, 05:53:02 AM

Title: Listbox with Popupmenu
Post by: NCR on February 16, 2011, 05:53:02 AM
Hi All!,

ii would like to know if you can point me an example of Listbox with a popup menu (masm32). What i'm trying to do is to display a popup menu in a multiselect listbox, then, the user can select one or more items and copy the text to the clipboard. Obviously, i just want to display the popup menu when the user clicks just in the listbox.

Can you help me with this?.

Thanks in advanced,
NCR
Title: Re: Listbox with Popupmenu
Post by: hutch-- on February 16, 2011, 06:29:02 AM
Subclass a listbox and trap the WM_LBUTTONDOWN message.
Title: Re: Listbox with Popupmenu
Post by: NCR on February 16, 2011, 06:37:38 AM
Hi hutch!,

would you explain me how to do that? i'm not an expert asm programmer.

Thanks in advanced!,
NCR
Title: Re: Listbox with Popupmenu
Post by: jj2007 on February 16, 2011, 07:21:18 AM
Search the Forum for GWL_WNDPROC. You probably want to catch WM_RBUTTONDOWN.
Title: Re: Listbox with Popupmenu
Post by: NCR on February 16, 2011, 08:00:19 AM
Hi jj2007!,

thanks for the reply. I searched and found this: http://www.masm32.com/board/index.php?topic=15708.0 and this: http://win32assembly.online.fr/tut20.html

I think these articles would be enough.

Anyways, if i have another question, i'll ask :P

Thanks!.
Title: Re: Listbox with Popupmenu
Post by: dedndave on February 16, 2011, 11:10:24 AM
i use WM_RBUTTONUP   :bg
TstPop: cmp     ecx,WM_RBUTTONUP
        jnz     WndPr2

;----------------------

;WM_RBUTTONUP

        movzx   edx,word ptr lParam+2
        movzx   ecx,word ptr lParam
        push    edx
        push    ecx
        INVOKE  ClientToScreen,hWnd,esp
        pop     ecx
        pop     edx
        INVOKE  TrackPopupMenu,hEditMenu,
                TPM_RETURNCMD or TPM_LEFTALIGN or TPM_TOPALIGN or TPM_NOANIMATION,
                ecx,edx,0,hWnd,0

;----------------------

;WM_COMMAND

WndPrM: cmp     eax,IDM_EDIT_COPY
        jz      WndPrP

when the user releases the right mouse button, a popup appears that is the same as the Edit popup from the main menu (hEditMenu)
TrackPopupMenu returns (in EAX) the ID number of a selected item, so the code falls through to parse it as a WM_COMMAND
that option was selected by using the TPM_RETURNCMD flag
you can create a different menu and pass the handle to that if the Edit menu isn't what you want
if i wanted to reduce the code, i could push the TrackPopupMenu parameters individually, but it isn't as clear - it would save 4 bytes   :P
Title: Re: Listbox with Popupmenu
Post by: NCR on February 16, 2011, 11:42:23 AM
Hi dedndave!,

thanks for the answer!. i tried that and it seems to work fine, but, i have a problem: the popup menu shows up anywhere in the window but not in the lixtbox. i need to adjust the coordinates before, i guess.
Title: Re: Listbox with Popupmenu
Post by: dedndave on February 16, 2011, 11:54:41 AM
the lParam hiword and loword are client coordinates of the hWnd window
TrackPopupMenu wants screen coordinates
you may have to adjust them according to the location of your listbox in the client area
i guess i would adjust them before the ClientToScreen call
Title: Re: Listbox with Popupmenu
Post by: dedndave on February 16, 2011, 12:00:18 PM
i might mention - if you create a special menu that is not "attached" to a window with SetMenu,
then you are supposed to use DestroyMenu before exiting the application to free the resources
in my code, i use a handle from the main menu, which is destroyed because...
all menus and submenus that are assigned to a window are destroyed for you when the window is destroyed
Title: Re: Listbox with Popupmenu
Post by: donkey on February 16, 2011, 01:53:45 PM
Hi,

Take a look at my code for the RadAsm favorites addin, it has a listbox with a popup menu...

http://www.masm32.com/board/index.php?topic=14100.msg123844#msg123844
Title: Re: Listbox with Popupmenu
Post by: NCR on February 16, 2011, 02:13:26 PM
Hi!,

@dedndave: Thanks for the hints!.
@donkey: Thanks for the example, that's exactly what i want to do, i'll take a look at it.

Thank you guys!.

ps. what hutch point me could be a very good exercise!!! (for me, of course :P you don't need it).
Title: Re: Listbox with Popupmenu
Post by: dedndave on February 16, 2011, 02:44:36 PM
in Edgar's example, he creates and destroys the menu each time, i guess
i would probably create it once at init and destroy it once on exit
although - he may have a good reason for doing it that way that isn't obvious to me - he's a pretty sharp guy   :U
Title: Re: Listbox with Popupmenu
Post by: NCR on February 16, 2011, 05:10:09 PM
Hi All!,

i think i got it! is working!.

Thanks a lot for the answers!!!.