Can somebody please explain the methods attach() and detach(), that are part of Cmenu and Cimagelist classes in Aurora?
I know how to use them to return the handle of an object, but I do not understand why a special method is required for that, instead of accessing the handle as a member variable of the class.
Haim
You detach a handle so the class doesn't free it when it goes out of scope. If you have a CMenu instance in a subroutine then the destructor of CMenu is called when the subroutine exits. That destructor frees the handle making it invalid. Detaching gives you the ownership of the handle.
Thanks,
Does this mean that handle is duplicated? Can the same menu be "detached" several times for usage with more than on window? ???
Haim
A menu can be attached to any number of windows, but if you destroy one window, the menu will be destroyed in all other windows too, since you use same handle for all windows.
After CMenu.Detach() the internal class HMENU variable is set to zero, so the destructor does not call DestroyMenu(), and you cannot detach it again.
CMenu.Attach() you can use for any menu if you want to work with menu using a class instead api, but remember to detach the handle before destroying the CMenu class if you want the handle to be valid longer than CMenu.
Thanks Paul and Sapero for solving the Detach() mistery for me :)
Haim