Skip to content

Instantly share code, notes, and snippets.

@katahiromz
Created November 23, 2024 03:55
Show Gist options
  • Save katahiromz/07b6713caa6565b330ddb0cd5a505e47 to your computer and use it in GitHub Desktop.
Save katahiromz/07b6713caa6565b330ddb0cd5a505e47 to your computer and use it in GitHub Desktop.
ポップアップメニューを表示
// ポップアップメニューを表示
INT ShowPopupMenu(HWND hwnd, INT iSubMenu)
{
// CreatePopupMenu/AppendMenuかLoadMenuでメニューを作成
HMENU hMenu = ::LoadMenu(m_hInst, MAKEINTRESOURCE(IDR_POPUPMENU));
HMENU hSubMenu = ::GetSubMenu(hMenu, iSubMenu);
::SetForegroundWindow(hwnd);
POINT pt;
::GetCursorPos(&pt);
auto flags = TPM_RETURNCMD | TPM_LEFTALIGN | TPM_RIGHTBUTTON;
INT nCmdID = (INT)::TrackPopupMenu(hSubMenu, flags, pt.x, pt.y, 0, hwnd, nullptr);
::PostMessage(hwnd, WM_NULL, 0, 0);
if (nCmdID != 0 && nCmdID != -1)
::PostMessage(hwnd, WM_COMMAND, nCmdID, 0);
// メニューを破棄
::DestroyMenu(hMenu);
return nCmdID;
}
// WM_CONTEXTMENU
void OnContextMenu(HWND hwnd, HWND hwndContext, UINT xPos, UINT yPos)
{
ShowPopupMenu(hwnd, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment