                                                    Menus


--Adding A Menu To Another Application

There are meany useful program addons you can make that incorperate adding a
menu to another application.  Maybe you are making an application for AOL
that will let you use TABs in Edit controlls (Which would be nice, and I will
probably make now that I think of it) which you can turn on and off and
such through a menu.  You can use an API function called appendmenu to add
menu items to a menu window.  You would go about making a menu for another
app probably by hand, by maybe creating a popup menu, then adding
items, then appending it to a top level menu.  Well, to do this you would do
something like this...

Dim aol, x, a, b, c as Integer
aol = FindWindow(AOL Frame25", "America  Online")
a = GetMenu(aol)
x = CreatePopupMenu()
b = AppendMenuByString(x, MF_POPUP and TPM_LEFTALIGN, 69, "Example PopupMenu
Item")
c = AppendMenu(a, MF_STRING Or MF_POPUP, c, "&Example Top Level Menu")
Call DrawMenuBar(aol)

That will make a menu on the end of AOL, and to insert it somewhere you can
use InsertMenu, once you have built a menu for an outside application, you
are going to have to have some way of knowing when the user clicks on it, so
you can run the appropriat code.  Now, to do this, we are going to use almost
the same technique that you use to click an outside application's menu, which
I will describe first, since it is easier to see what is going on in creating
a click event for the menu.

--Running Outside Application's Menuitems

Menus do not behave like most other windows, and can be a real pain to deal
with, so here is some help in running menu items to an outside application.
When you click on a menu item, it sends a WM_COMMAND message to its parent
application.  For an example, if you were to click on new in the file menu, a
message is sent to "AOL Frame25" with a WM_COMMAND message that includes the
menu item ID.  To generate click events in another application's menu, we
must fool the program into thinking we clicked on something by sending it a
WM_COMMAND message.  Here is an example.

Dim aol, a, b, c as Integer
Dim d as Long
aol = FindWindow("AOL Frame25", "America  Online")
a = GetMenu(aol)
b = GetSubMenu(a, 0)
c = GetMenuItemID(b, 0)
d = SendMessage(aol, WM_COMMAND, c, 0&)

That will be the equivelant to clicking on File and going to New

Then, to intercept the messages from another application's menu to be sent to
your own...

--Interception Another Application's Menu Messages

Some time you may want to say an Email enhancement so whenever the user
chooses compose mail from the menu, you would recieve the message, and
display your own Email window or something.  Also, if you are adding a menu
to another application, you will want to watch for your own menu messsages.
You assigned each menu item an ID, just like any other menu item.  As
described above, when you use a menu, it sends a WM_COMMAND message to the
main form, like "AOL Frame25", so you will want to subclass that window, and
watch for the WM_COMMAND messages it recieves.  I use VBMessenger VBX for
subclassing, it has a message selector in the properties menu.  Select the
message WM_COMMAND, then in the form load event, or somewhere, tell it to
subclass the "AOL Frame25" window as described in an earlier issue of this
letter.  Then in the code for it, just like when you sent the message, for
the wParam it will have the menu ID, so remember what your menu IDs were that
you added, and if it is one of those, handle it accordingly, if it is not,
then simply ignore it.  

--Tracked Popup Menus

Have you ever right clicked somewhere on an application, and a menu item
appeared?  Well, windows95 put this in most Edit boxes, and many commercial
applications and many help files use this.  These are called tracked popup
menus.  They are submenus that can move to any position on the screen you
wish.  This is achived through the TrackPopupMenu function.  With this
function, you give it a handle to a popup menu, you could use your knowledge
on how to make submenus from before when I described how to add a menu to
another app to make a new submenu, and you can then give it the function the
handle of the menu as the hMenu parameter, then for the wFlags, you may make
the menu come up centered on where the right mouse click occured, left, or
center aligned.  then you will have to give it x and y screen coordinates, to
do this with the mouse, you would use the getcursorpos subroutine, and you
can simply give it those coordinate, and for the nReserved, pass it a 0,
then, you will want to pass it the handle of the window you would like it to
give commands to, then for the lpRect, it is easiest just to leave this at 0.

--Summary

With this information you should be able to add menus to another application,
make use of the menus, generate click events in another application's menu,
watch for all menu events, and even make your own tracked popup menus.

--A Summary On Menus, By:  JeR
