Changing the text in a menu button from within a program?

I'm using MSVisual C++. In my program, I have a main window with a menu bar across the top. Alternate clicks of a button on the menu initiates or terminates a soundcard playing a pure tone. But there is a delay before the soundcard plays the tone, during which I would like to tell the user that the button click was processed (so it isn't clicked again). Is there a way to change the text label of the menu button from within the program to display the current state of either that the soundcard is going to play or instead that the program is waiting for a mouse click?

Comments

  • : I'm using MSVisual C++. In my program, I have a main window with a menu bar across the top. Alternate clicks of a button on the menu initiates or terminates a soundcard playing a pure tone. But there is a delay before the soundcard plays the tone, during which I would like to tell the user that the button click was processed (so it isn't clicked again). Is there a way to change the text label of the menu button from within the program to display the current state of either that the soundcard is going to play or instead that the program is waiting for a mouse click?
    :
    :

    get the HWND of the button then call SetWindowText().
  • Thanks! That led me to what I needed. It would speed up my understanding if you could offer an example of how this would work for a menu element (e.g., button) or point me to an example.

    : : I'm using MSVisual C++. In my program, I have a main window with a menu bar across the top. Alternate clicks of a button on the menu initiates or terminates a soundcard playing a pure tone. But there is a delay before the soundcard plays the tone, during which I would like to tell the user that the button click was processed (so it isn't clicked again). Is there a way to change the text label of the menu button from within the program to display the current state of either that the soundcard is going to play or instead that the program is waiting for a mouse click?
    : :
    : :
    :
    : get the HWND of the button then call SetWindowText().
    :

  • : Thanks! That led me to what I needed. It would speed up my understanding if you could offer an example of how this would work for a menu element (e.g., button) or point me to an example.
    :
    : : : I'm using MSVisual C++. In my program, I have a main window with a menu bar across the top. Alternate clicks of a button on the menu initiates or terminates a soundcard playing a pure tone. But there is a delay before the soundcard plays the tone, during which I would like to tell the user that the button click was processed (so it isn't clicked again). Is there a way to change the text label of the menu button from within the program to display the current state of either that the soundcard is going to play or instead that the program is waiting for a mouse click?
    : : :
    : : :
    : :
    : : get the HWND of the button then call SetWindowText().
    : :
    :
    :


    since your program has already created the button then it must also know the HWND handle. Then just simply call SetWindowText() -- see MSDN for description of the function.
    [code]
    SetWindowText(m_hWnd,"Some Text");
    [/code]
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/windows/windowreference/windowfunctions/setwindowtext.asp

  • Thanks, that helps a lot. I'll have to play with it some. At this point it isn't clear to me how I choose a particular button out of several that are on the menu.

    : : [code]
    : : SetWindowText(m_hWnd,"Some Text");
    : : [/code]
    : : http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/windows/windowreference/windowfunctions/setwindowtext.asp
    : :
    : :
    : [blue]Since menu element is not a button - it will not work for menu element. You need to use this one:
    :
    : http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/resources/menus/menureference/menufunctions/modifymenu.asp[/blue]
    :

  • [b][red]This message was edited by AsmGuru62 at 2006-3-23 19:52:40[/red][/b][hr]
    [blue]Here is function list to work with menus. Basically, you need to get the menu handle and find the item by its text or command ID.

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/resources/menus.asp
    [/blue]
  • [b][red]This message was edited by katman at 2006-4-11 15:21:8[/red][/b][hr]
    This helped me a lot, thanks. But I'm still having a problem. I was able to control HILITE/UNHILITE of the menu button, so that gives me more-or-less what I wanted in that I can indicate the state of the program. But what I really wanted was to change the button caption, and I couldn't figure out how to do that. I tried the MIIM_STRING, but that didn't work. Do I need to change the menu item to bitmap to change the caption? Or is there some other way to change the button caption? Or can it be done at all?

    : [b][red]This message was edited by AsmGuru62 at 2006-3-23 19:52:40[/red][/b][hr]
    : [blue]Here is function list to work with menus. Basically, you need to get the menu handle and find the item by its text or command ID.
    :
    : http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/resources/menus.asp
    : [/blue]
    :


  • : [blue]Since menu element is not a button - it will not work for menu element. You need to use this one:
    :

    you are right -- I thought he was talking about a button, not a menu item.
  • [b][red]This message was edited by katman at 2006-4-12 8:44:54[/red][/b][hr]
    Sorry if I wasn't clear, and thanks for your help. And just to clarify, I mean an element in the top menu bar of the main window (File, View, Help, and my custom options). Do you have any advice on changing the caption on one the menu elements? For example, if I have an elemen with the caption "On" and I want to change it on-the-fly to "Off." Is there a way to do that?

    : : [blue]Since menu element is not a button - it will not work for menu element. You need to use this one:
    : :
    :
    : you are right -- I thought he was talking about a button, not a menu item.
    :



  • Yes, that was a very helpful link. Except instead of ModifyMenu I used the newer SetMenuItemInfo function. Below is the code I used for the hilite/unhilite of a menu item with the caption "Start". This code works and goes a long way toward what I'm after. But how do I change the caption itself? I tried MF_STRING but couldn't get it to work. I may be close, but I'm stuck again. Thanks for your continued help and patience.

    HMENU hmenubar;
    MENUITEMINFO mii;
    mii.cbSize = sizeof(MENUITEMINFO);
    HWND hWnd = AfxGetMainWnd()->GetSafeHwnd();
    hmenubar = GetMenu(hWnd);
    mii.fMask = MIIM_STATE;
    if (Start)
    { mii.fState = MFS_HILITE; } // hilite the "Start" button
    else { mii.fState = MFS_UNHILITE; } // unhilite the "Start" button
    SetMenuItemInfo(hmenubar, IDC_START, FALSE, &mii);
    DrawMenuBar(hWnd);

    : [blue]Did you try this:
    :
    : http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/resources/menus/menureference/menufunctions/modifymenu.asp[/blue]
    :

  • [blue]Try this, please.
    I formatted it a little - can't read non-spaced code... sorry.[/blue]
    : [code]
    : HWND hWnd = AfxGetMainWnd ()->GetSafeHwnd ();
    : HMENU hmenubar = GetMenu (hWnd);
    : MENUITEMINFO mii;

    : mii.fMask = MIIM_STATE;
    : mii.cbSize = sizeof (MENUITEMINFO);

    : if (Start)
    : {
    : mii.fState = MFS_HILITE;
    : }
    : else
    : {
    : mii.fState = MFS_UNHILITE;
    : }

    : SetMenuItemInfo (hmenubar, IDC_START, FALSE, &mii);

    : [red]ModifyMenu (hmenubar, IDC_START,
    : MF_BYCOMMAND | MF_STRING, IDC_START, "NewItemText");[/red]

    : DrawMenuBar (hWnd);
    : [/code]

  • That works perfectly! Thanks!

    : [blue]Try this, please.
    : I formatted it a little - can't read non-spaced code... sorry.[/blue]
    : : [code]
    : : HWND hWnd = AfxGetMainWnd ()->GetSafeHwnd ();
    : : HMENU hmenubar = GetMenu (hWnd);
    : : MENUITEMINFO mii;
    :
    : : mii.fMask = MIIM_STATE;
    : : mii.cbSize = sizeof (MENUITEMINFO);
    :
    : : if (Start)
    : : {
    : : mii.fState = MFS_HILITE;
    : : }
    : : else
    : : {
    : : mii.fState = MFS_UNHILITE;
    : : }
    :
    : : SetMenuItemInfo (hmenubar, IDC_START, FALSE, &mii);
    :
    : : [red]ModifyMenu (hmenubar, IDC_START,
    : : MF_BYCOMMAND | MF_STRING, IDC_START, "NewItemText");[/red]
    :
    : : DrawMenuBar (hWnd);
    : : [/code]
    :
    :
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories