WM_MENUGETOBJECT help

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
kczx3
Posts: 1648
Joined: 06 Oct 2015, 21:39

WM_MENUGETOBJECT help

19 Feb 2020, 21:02

I was playing around with using a Menu with MNS_DRAGDROP (and MNS_MODELESS) in response to a different users post for help. I hadn't seen this before on here and searching turned up little.

I can't figure out how I would be able to enumerate the files/folders dropped onto a menu item in the WM_MENUGETOBJECT event. Do I have to implement the interface for iDropTarget myself or use the riid and pvObj items of the MENUGETOBJECTINFO structure somehow?

This isn't really a v2 specific question but that's why I typically try to use. The code I've been playing with:

Code: Select all

OnMessage(WM_MENUGETOBJECT := 0x124, "GetObject")

m := MenuCreate()

m.Add("Drop on Me!", "Dropped")
m.Add("Drag Me!", "Dropped")

menuInfo := BufferAlloc(16+3*A_PtrSize)
NumPut("UInt", 16+3*A_PtrSize, menuInfo)
NumPut("UInt", 0x10, menuInfo, 4)
NumPut("UInt", 0x20000000 | 0x40000000, menuInfo, 8) ; MNS_DRAGDROP and MNS_MODELESS, the latter is required
DllCall("SetMenuInfo", "Ptr", m.handle, "Ptr", menuInfo)

F4::m.show()

Dropped(path, *) {
    MsgBox("Dropped!")
}

GetObject(wParam, lParam, msg, hWnd) {
    global m
    static paths := [A_Desktop, A_MyDocuments]
    
    flags := NumGet(lParam, "UInt")
    pos := NumGet(lParam, 4, "UInt")
    hMenu := NumGet(lParam, 12, "Ptr")
    riid := NumGet(lParam, 20, "Ptr")
    pInt := NumGet(lParam, 28, "Ptr")
    
    if (hMenu != m.handle) {
        ToolTip("Our menu")
        return 1
    }
    
    if (flags & (0x2 | 0x1)) {
        return 0
    }
    
    ToolTip(ridd "`n" pInt)
    return 1
}

DoDrop(*) {
    Msgbox("Dropped")
}
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: WM_MENUGETOBJECT help

07 Mar 2020, 15:32

Hi kczx3, I have looked into this can tell you that you need to implement IDropTarget yourself (you return an instance via the message callback) and get the data from the IDataObject passed to the IDropTarget methods.
This isn't really a v2 specific question but that's why I typically try to use.
It is much easier to write this in v2 though ;). I can share my code when I clean up some loose ends, if you like.

Cheers.
User avatar
kczx3
Posts: 1648
Joined: 06 Oct 2015, 21:39

Re: WM_MENUGETOBJECT help

07 Mar 2020, 16:16

Interesting. I wouldn’t have come to that same conclusion myself. I was looking at the implementations by @just me that hopefully I can make work. Please do share what you come up with but I’ll also see if I can tinker this weekend on it.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: WM_MENUGETOBJECT help

10 Mar 2020, 04:04

Example, on :arrow: github.

Cheers.
User avatar
kczx3
Posts: 1648
Joined: 06 Oct 2015, 21:39

Re: WM_MENUGETOBJECT help

10 Mar 2020, 19:03

Helgef wrote:
10 Mar 2020, 04:04
Example, on :arrow: github.

Cheers.
Fantastic! I must say that this was much more implementation than I was expecting. There were obviously intricacies of iDropTarget that I was missing.

A few notes:
  • Are you sure the your Menu styles are being applied? I believe the offsets for fMask and dwStyle are 4 and 8, respectively (even on x64). You are using NumPut in multi-mode which states
    Each number is written at the next byte after the previous number, with no padding...
    so I think it is using 8 and 16 offsets for those if I am not mistaken.
  • In your example, there's a strange issue where dragging text seems to work the first time but after that when I activate the menu while dragging text the menu just appears and then disappears in the blink of an eye. Right-clicking on the Tray icon makes the menu appear though...
User avatar
kczx3
Posts: 1648
Joined: 06 Oct 2015, 21:39

Re: WM_MENUGETOBJECT help

10 Mar 2020, 19:48

Helgef wrote:
10 Mar 2020, 04:04
Example, on :arrow: github.

Cheers.
Also, there's some crazy voodoo going on in that iDropTarget static new method!
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: WM_MENUGETOBJECT help

11 Mar 2020, 10:22

Hi.

Regarding numput, I believe it is correct. As you quoted, there is no added padding, and 'uint' is 4 bytes. Example,

Code: Select all

b := bufferalloc(12)
numput 'uint', 1, 'uint', 2, 'uint', 3, b

msgbox numget(b, 0, 'uint') . '`n' 	; 1 
	. numget(b, 4, 'uint') . '`n' 	; 2
	. numget(b, 8, 'uint')			; 3
Please let me know if I misunderstood you. Admittedly, I think I will make more mistakes with the new numput paramter setup, than it will be useful to me. I'm not very fond of it, also the new order is somewhat awkward to me, I like the v1 order better, that is having the type last for both numget/put.

Regarding the repeated drop issue, I couldn't reproduce on either 32 or 64 bit builds. If you could give me a step by step example on how to reproduce I'd be grateful.

Regarding voodoo, well at least the work around for (helgef?) failure to use callbackcreate with variadic functions complicates it a bit. I could have sworn callbackcrate and variadic functions worked fine at some point but I might just have made some mistake. I will look into it some time. Please ask any question regarding anything else if unclear.

Cheers.
User avatar
kczx3
Posts: 1648
Joined: 06 Oct 2015, 21:39

Re: WM_MENUGETOBJECT help

11 Mar 2020, 13:09

Ok, I misinterpreted that on the docs then.

For me, these steps cause an issue.
1. Start script
2. Highlight text, then click and begin dragging without releasing mouse button.
3. Hit hotkey to display menu.
4. Drag mouse over first menu item and release to "drop" the text.
5. Select other text in same application as before (notice the menu closes)
6. Click and begin dragging as before.
7. Try to display menu using hotkey. It will sometimes briefly appear but you can see that window focus changes to menu and back to previous application if you spam the hotkey. If I right click the tray icon for the script then the menu will display and stay displayed.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: WM_MENUGETOBJECT help

11 Mar 2020, 13:25

Thanks, I'm AFK so I can't test now. Do you see this behavior only if you "drop" on the first menu item? Have you tried "dropping" on a "regular" menu, that is, just make a menu in a new script unrelated to my code, and take the above steps.

Cheers.
User avatar
kczx3
Posts: 1648
Joined: 06 Oct 2015, 21:39

Re: WM_MENUGETOBJECT help

11 Mar 2020, 13:42

Good call. Looks like it is the MNS_MODELESS style that is messing it up. I took my original code and removed both style and the menu worked fine. Then I added back in the MNS_MODELESS and the issue presented itself.

I wonder if that style is even necessary...
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: WM_MENUGETOBJECT help

11 Mar 2020, 14:02

Good, thanks for investigating.
I added back in the MNS_MODELESS and the issue presented itself.

I wonder if that style is even necessary...
0x20000000 | 0x40000000, ; MNS_DRAGDROP and MNS_MODELESS, the latter is required
@kczx3, I trusted you :terms:
Spoiler
User avatar
kczx3
Posts: 1648
Joined: 06 Oct 2015, 21:39

Re: WM_MENUGETOBJECT help

11 Mar 2020, 14:46

Hahahahaha. I assumed it had to be so that the menu would stay open if you clicked away from it. However, it seems that style doesn't have an effect on that. You have to be dragging the items/text prior to opening the menu regardless.
User avatar
kczx3
Posts: 1648
Joined: 06 Oct 2015, 21:39

Re: WM_MENUGETOBJECT help

11 Mar 2020, 14:54

@Helgef well actually it IS required. The drop callback never gets called if you don't have MNS_MODELESS :(.

Testing the issue some more I can see that if you make sure to click a menu item after dropping text/files onto it that it works as expected. If you drop text/files and then click away, the menu closes and it seems like maybe some kind of internal state for the menu to AHK is broken.
SAbboushi
Posts: 252
Joined: 08 Dec 2014, 22:13

Re: WM_MENUGETOBJECT help

27 Mar 2020, 14:11

Helgef wrote:
11 Mar 2020, 10:22
I could have sworn callbackcrate and variadic functions worked fine at some point but I might just have made some mistake
It was working back in 2018. I could have sworn it was working on versions through October 2018, but I see:
"Removed the special behaviour of variadic callback functions and added the & option (pass the address of the parameter list)" for v2.0-a090-ae96c4a (in March).
I suspect this means we can no longer use e.g.

Code: Select all

callbackcreate(cbcp*)
... right?

I'm trying to figure out how to fix my broken callbackcreates to variadic functions right now. Hopefully "& option" will fix it

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: kevinpak, songdg, vmech and 37 guests