MrDoomMaster
April 15th, 2005, 07:19 PM
I have a couple of buttons that I want to make a menu appear at the location of the hotspot on the cursor when a user clicks them.
My specific situation is that I'm creating a game called Battle Boats (much like battle ship). A user clicks the button called "Add Boat" (left click) and a context menu should appear immediately allowing them to select the type of boat they wish to place.
Any help? Thanks!
Bond
April 15th, 2005, 08:06 PM
In the "Add Button" handler, just get the coords of your button using GetWindowRect() or GetClientRect() and ClientToScreen() and then use LoadMenu(), GetSubMenu(), and TrackPopupMenu() to display a menu resource at that location.
souldog
April 16th, 2005, 05:52 AM
to be more specific here is an example that does not use resources
HMENU menu(::CreatePopupMenu());
if(!menu)
{
ACCESS_ERROR_LOG::REPORT_ERROR_MESSAGE(ACCESS_RESOURCE_MANAGER::LOAD_STRING(IDS_FAILED_TO_CREATE_BORDER_STYLE_SUBMENU), CONTROL_PANEL_OBJECTS::MODULE_NAME, ERROR_LOG::EM_POPUP_AND_FILE);
return false;
}
::AppendMenu(menu, MF_STRING, IDM_BS_NONE, ACCESS_RESOURCE_MANAGER::LOAD_STRING(IDS_NONE).c_str());
::AppendMenu(menu, MF_STRING, IDM_BS_SUNKEN, ACCESS_RESOURCE_MANAGER::LOAD_STRING(IDS_SUNKEN).c_str());
::AppendMenu(menu, MF_STRING, IDM_BS_LINE, ACCESS_RESOURCE_MANAGER::LOAD_STRING(IDS_LINE).c_str());
POINT p;
::GetCursorPos(&p);
::TrackPopupMenu(menu, TPM_LEFTALIGN|TPM_LEFTBUTTON, p.x, p.y, 0, hwnd, 0);
hwnd is the handle of the window that will process the command in a WM_COMMAND handler