How to change the text of a softkey menu?

To change the text of a softkey menu item, all you have to do is pretend that they are buttons. Well, they are specialized buttons in any case. So in order to change its property (in this case the ‘text’) you will have to use the TBBUTTONINFO structure along with TB_GETBUTTONINFO and TB_SETBUTTONINFO messages. Lets dive into the code:



Note: Error checking is omitted for obvious reasons.



    TCHAR szText[128] = TEXT("");

    TBBUTTONINFO tbi;

    ZeroMemory(&tbi, sizeof(tbi));



    tbi.cbSize = sizeof(tbi);

    tbi.dwMask = TBIF_TEXT | TBIF_COMMAND;

    tbi.pszText = szText;

    tbi.cchText = sizeof(szText);

    tbi.idCommand = IDM_MARK_UNMARK;



    SendMessage(g_hWndMenuBar, TB_GETBUTTONINFO, (WPARAM)IDM_MARK_UNMARK, (LPARAM)&tbi);



    if (!wcscmp(szText, L"Mark"))

    {

        wcscpy(szText, L"Unmark");

    }

    else

    {

        wcscpy(szText, L"Mark");

    }



    SendMessage(g_hWndMenuBar, TB_SETBUTTONINFO, (WPARAM)IDM_MARK_UNMARK, (LPARAM)&tbi);

So we fill up the TBBUTTONINFO structure and then send TB_GETBUTTONINFO message to the menu bar. After this call returns, szText will contain the text of the menu item. We switch the contents of szText and then send a TB_SETBUTTONINFO message. And the text on the menu item changes.

 

Couple of seasons back I was working on an application which let the user "Mark" and "Unmark" dates on a calendar control. It was for a smartphone, so I thought this technique is quite handy and just qualifies for a post.



Here is a video of it in action,

 

Leave a Reply

Your email address will not be published. Required fields are marked *