Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help needed: Handling dynamic popup menu items 1

Status
Not open for further replies.

imagenetics

Programmer
Dec 8, 2003
66
0
0
PL
Hi there!

I'd call myself rather a fan of programming, than a programmer, and most of my knowledge concerning C++ is based on C++Builder. During Christmas break I decided to finally try some programming in pure WinAPI.

I wrote a tiny application which sits in the shell notification area an provides a popup menu with all available drives. Divided with separators when drive type changes - I'm that good! ;) Each of these items are added dynamically before popping up a menu and are meant to simply open an exlorer's window with the contents of target drive. Everything through building a menu and adding the drives is done. Opening the corresponding windows? Here my programming stops.

How to define a unique ID for each menu item and process messages sent by this ID (or any other solution)? This cannot be done by reading item's string. Those are not drive letters but their explorer's equivalents read with SHGetFileInfo(...).

In C++Builder this was not a problem at all. I simply saved drive letter string in a hint property and a custom OnClick event (the code below) which passed the hint to ShellExecute(...).

Code:
void __fastcall TFormMain::MenuItemClick(TObject *Sender)
{
	TMenuItem *ClickedItem = dynamic_cast<TMenuItem *>(Sender);

	if(ClickedItem)
	{
		ShellExecute(0, "open", ClickedItem->Hint.c_str(), "", "", SW_SHOWNORMAL);
	}
}

I'd be very grateful for help in doing the same thing in WinAPI. A sample source code would be very appreciated. Thank you.
 
When you create a popup menu and insert items using InsertMenu or InsertMenuItem function, you specify a menu item Id, to uniquely identify each item in the menu.

When you show your menu using TrackPopupMenu function and user clicks a menu item, Windows sends a WM_COMMAND message to your window. You should handle this message in your window procedure and identify the menu item which was clicked.

See WM_COMMAND for more info.

Based on this info, you can show the contents of the selected drive in Explorer.

If you do not want to handle WM_COMMAND message in your window procedure, there is an alternate method. Call the PeekMessage function immediately after the TrackPopupMenu function and look for the WM_COMMAND message in the message queue. In this way you get the message info pending in the queue (if any). Again you can examine the message parameters to identify the menu item and open the contents of the selected drive.

See some VB code in thread222-556072 using the same method. If you do VB and have VB installed, you will find it useful.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top