Search code examples
c++winapiwin32gui

Displaying menu items in multiple columns


I am trying to improve the user interface for a Train Conductor game I am working on.

One of the things I am doing is displaying the list of vehicles in a popup menu. I want to use multiple columns instead of a single long column.

The menu's list of vehicles pops up when the right mouse button is clicked.

I am not sure how to approach this, I'm sure the answer is simple and I'm just not seeing it.

How it looks right now

image

How I want it to look

image

The following is a code snippet of the part which appends the list to the menu:

Guide::GetTrainList(&TrainList); //this receives the list of the trains

if(TrainList.size() > 0) //this will tell the code to continue if the trains exist (decided by the player which trains to play with)
{
    for(int j = 0; j < TrainList.size(); j++)
    {
        CString FollowTrain = TrainList[j]->GetMenuName();
        FollowTrain.Append((m_FollowTrain != NULL && m_FollowTrain == TrainList[j])?L" (Followed)":L"");
        GoToTrainMenu.AppendMenu(MF_STRING, Counter++, TrainList[j]->GetMenuName());
        FollowTrainMenu.AppendMenu(MF_STRING, Counter++, FollowTrain);
        MoveTrainMenu.AppendMenu(MF_STRING, Counter++, TrainList[j]->GetMenuName());
    }

    PopupMenu.AppendMenu(MF_POPUP, (unsigned int)GoToTrainMenu.Detach(), GetStringFromResource(GOTOTRAIN));
    PopupMenu.AppendMenu(MF_POPUP, (unsigned int)FollowTrainMenu.Detach(), GetStringFromResource(FOLLOWTRAIN));
    PopupMenu.AppendMenu(MF_POPUP, (unsigned int)MoveTrainMenu.Detach(), GetStringFromResource(MOVETRAIN));
}

Solution

  • Add MF_MENUBARBREAK or MF_MENUBREAK to the flags to create menu columns:

    HMENU hMenu = CreatePopupMenu();
    AppendMenuA(hMenu, MF_STRING, 1, "Foo");
    AppendMenuA(hMenu, MF_STRING|MF_MENUBREAK, 2, "Bar");
    AppendMenuA(hMenu, MF_STRING, 3, "Baz");
    AppendMenuA(hMenu, MF_STRING, 4, "A");
    AppendMenuA(hMenu, MF_STRING|MF_MENUBARBREAK, 5, "B");
    AppendMenuA(hMenu, MF_STRING, 6, "C");
    UINT cmd = TrackPopupMenuEx(hMenu, TPM_RETURNCMD|TPM_RIGHTBUTTON, pt.x, pt.y, hWnd, NULL);