簡単なWin32メニューのポップアップ
1 2 3 4 5 6 7 8 9 10 11 12 13 |
HWND hWnd = CreateSimpleWindow(WndProc); ghPopup = CreatePopupMenu(); InsertMenu(ghPopup, 0, MF_BYCOMMAND, 100, L"<DUMMY>"); POINT curPos; GetCursorPos(&curPos); SetForegroundWindow(hWnd); UINT cmd = TrackPopupMenu(ghPopup, TPM_RETURNCMD, curPos.x, curPos.y, 0, hWnd, NULL); |
オーナードローを有効にしてメッセージを処理する
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
#include <Windows.h> #include "../../../../lsMisc/CreateSimpleWindow.h" using namespace Ambiesoft; HMENU ghPopup; WCHAR szT[512]; LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { case WM_MEASUREITEM: { MEASUREITEMSTRUCT* mis = (MEASUREITEMSTRUCT*)lParam; GetMenuString(ghPopup, mis->itemID, szT, _countof(szT), MF_BYCOMMAND); mis->itemHeight = 16; mis->itemWidth = 64; return TRUE; } break; case WM_DRAWITEM: { DRAWITEMSTRUCT* dis = (DRAWITEMSTRUCT*)lParam; GetMenuString(ghPopup, dis->itemID, szT, _countof(szT), MF_BYCOMMAND); DrawText(dis->hDC, szT, lstrlen(szT), &dis->rcItem, 0); return TRUE; } break; } return DefWindowProc(hWnd, msg, wParam, lParam); } int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow) { HWND hWnd = CreateSimpleWindow(WndProc); ghPopup = CreatePopupMenu(); InsertMenu(ghPopup, 0, MF_BYCOMMAND, 100, L"<DUMMY>"); // Enable OwnerDraw MENUITEMINFO mii; ZeroMemory(&mii, sizeof(mii)); mii.cbSize = sizeof(mii); mii.fMask = MIIM_TYPE; GetMenuItemInfo(ghPopup, 100, FALSE, &mii); mii.cbSize = sizeof(mii); mii.fMask = MIIM_TYPE; mii.fType |= MFT_OWNERDRAW; SetMenuItemInfo(ghPopup, 100, FALSE, &mii); POINT curPos; GetCursorPos(&curPos); SetForegroundWindow(hWnd); UINT cmd = TrackPopupMenu(ghPopup, TPM_RETURNCMD, curPos.x, curPos.y, 0, hWnd, NULL); return 0; } |