code_junkie

How to programmatically select popup menu item in Windows?

Nov 12th, 2011
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. //in my case, the menu is a popup from a tree control created with:
  2. CMenu menu;
  3. menu.CreatePopupMenu();
  4. //add stuff to the menu...
  5. pTreeCtrl->SetMenu(&menu);
  6. m_hMenu = menu.GetSafeHmenu();
  7. CPoint pt;
  8. GetCursorPos(&pt);
  9. menu.TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON, pt.x, pt.y, _pTreeCtrl);
  10. menu.Detach();
  11. m_hMenu = NULL;
  12.  
  13. CRect rc;
  14. GetMenuItemRect(pTreeCtrl->GetSafeHwnd(), m_hMenu, targetMenuItemIndex, &rc);
  15. if(FALSE == rc.IsRectEmpty())
  16. {
  17. CPoint target = rc.CenterPoint();
  18. //this closes the menu
  19. ::PostMessage(pTreeCtrl->GetSafeHwnd(), WM_CANCELMODE, 0, 0);
  20. DestroyMenu(m_hMenu);
  21. m_hMenu = NULL;
  22. //now simulate the menu click
  23. ::PostMessage(pTreeCtrl->GetSafeHwnd(), WM_COMMAND, targetMenuItemID, 0);
  24. }
Add Comment
Please, Sign In to add comment