CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    May 2012
    Posts
    16

    [RESOLVED] A little help with menu item plz!

    Hi,
    I'm trying to disable/enable an item menu, but half works because when I create my window the send message to disable is OK, but pressing my button does not enable and fail.

    Part of my code:
    Code:
    ...WndProc:
    case WM_CREATE:
    	EnableMenuItem(GetMenu(hwnd), ID_TEST_DBMANAGER, MF_DISABLED); // is OK
    	break;
    
    ...My Dialog:
    int lw,hw;
    case WM_COMMAND:
    	lw = LOWORD(wparam);
    	hw = HIWORD(wparam);
    
    	switch (lw)
    	{
    	case ID_MYBUTTON:
    		EnableMenuItem(GetMenu(hwnd), ID_TEST_DBMANAGER, MF_ENABLED);//DON'T WORK why?
    		break;
    	}
    output:
    Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped, 0 warnings
    it does not work? is too simple, I do not understand.

    VS2010 win32 - no MFC
    Thanks.

  2. #2
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: A little help with menu item plz!

    Just do the work in WM_INITMENUPOPUP message.
    It tells you that a (sub)menu is about to be shown, then you can do here custom actions like enabling/disabling menu items.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  3. #3
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: A little help with menu item plz!

    pressing my button does not enable and fail
    Please elaborate. "Does not enable" and "fail", are there two problems or a single one? What "fail" really means?

    Besides, your snippet explains practically nothing. Why "WndProc" and "My Dialog" use the same code? What are those window and dialog?
    Best regards,
    Igor

  4. #4
    Join Date
    May 2012
    Posts
    16

    Red face Re: A little help with menu item plz!

    Quote Originally Posted by Igor Vartanov View Post
    Please elaborate. "Does not enable" and "fail", are there two problems or a single one? What "fail" really means?

    Besides, your snippet explains practically nothing. Why "WndProc" and "My Dialog" use the same code? What are those window and dialog?
    Yes is the same problem, excuse my language

    Now I use as said ovidiucucu WM_INITMENUPOPUP:
    Code:
    SendMessage (hwnd,WM_INITMENUPOPUP,EnableMenuItem(GetMenu(hwnd), ID_ITEM1, MF_DISABLED),0);// to disable item
    
    AND
    SendMessage (hwnd,WM_INITMENUPOPUP,EnableMenuItem(GetMenu(hwnd), ID_ITEM1, MF_ENABLED),0);// to enable item
    This works well in WndProc but in my DlgProc command do nothing, is like not having hwnd, I remind I'm new to programming but I read a lot. thanks.

  5. #5
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: A little help with menu item plz!

    Well, I didn't suggest to send WM_INITMENUPOPUP but just to handle it.
    Again, this message is sent (by the system) when a drop-down menu is about to become active.
    It has no much sense to send it yourself.

    See WM_INITMENUPOPUP message.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  6. #6
    Join Date
    May 2012
    Posts
    16

    Re: A little help with menu item plz!

    Quote Originally Posted by ovidiucucu View Post
    Well, I didn't suggest to send WM_INITMENUPOPUP but just to handle it.
    Again, this message is sent (by the system) when a drop-down menu is about to become active.
    It has no much sense to send it yourself.

    See WM_INITMENUPOPUP message.
    Ok see my test code:

    Code:
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        switch(msg)
        {
    	case WM_CREATE://init my menu disable item
    		SendMessage (hwnd,WM_INITMENUPOPUP,EnableMenuItem(GetMenu(hwnd), ID_ITEM1, MF_DISABLED),0);//by default disabled to test
    		break;
    
    	case WM_COMMAND:
    		switch (LOWORD(wParam))
    		{
    		case ID_EXIT:
    			DestroyWindow(hwnd);
    			break;
    
    		case ID_ITEM2:
    			SendMessage (hwnd,WM_INITMENUPOPUP,EnableMenuItem(GetMenu(hwnd), ID_ITEM1, MF_DISABLED),0);
    			break;
    
    		case ID_ITEM3:// call enable item
    			SendMessage (hwnd,WM_INITMENUPOPUP,EnableMenuItem(GetMenu(hwnd), ID_ITEM1, MF_ENABLED), 0);
    			break;
    		}
    		break;
    
    	case WM_CLOSE:
    		DestroyWindow(hwnd);
    		break;
    
    	case WM_DESTROY:
    		PostQuitMessage(0);
    		break;
    
    	default:
    		return DefWindowProc(hwnd, msg, wParam, lParam);
    	}
        return 0;
    }
    works perfect in WndProc I continue to not understand

  7. #7
    Join Date
    May 2012
    Posts
    16

    Re: A little help with menu item plz!

    OOOOoooK I resolved ^^

    Not need use WM_INITMENUPOPUP just call GetParent(hwnd).

    Code:
    INT_PTR CALLBACK dlg1 (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    	switch (msg)
    	{
    	case WM_COMMAND:
    		switch (LOWORD(wParam))
    		{
    		case ID_BUTTON1://press my button to enable	
    			EnableMenuItem(GetMenu(GetParent(hwnd)), ID_ITEM1, MF_ENABLED);
    			EndDialog (hwnd,TRUE);
    			break;		
    
    	default:
    		return DefWindowProc(hwnd, msg, wParam, lParam);
    		}
    	}
        return 0;
    }
    Must be another way to do it, but this work for me, thanks.
    Last edited by v_nom70; June 8th, 2012 at 10:27 AM.

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: A little help with menu item plz!

    Quote Originally Posted by v_nom70 View Post
    Ok see my test code:
    Why are you still sending WM_INITMENUPOPUP?
    Code:
    	case WM_CREATE://init my menu disable item
    	SendMessage (hwnd,WM_INITMENUPOPUP,EnableMenuItem(GetMenu(hwnd), ID_ITEM1, MF_DISABLED),0);//by default disabled to test
    		break;
    You don't understand -- the WM_INITMENUPOPUP is sent by the system to you, and then you handle it. You don't go and create your own WM_INITMENUPOPUP message.

    Once again:

    http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx

    Please read the link above carefully, and then rewrite your code in accordance to what the information is stating. Your code now is still wrong, even though it seems to work for a window procedure.

    Regards,

    Paul McKenzie

  9. #9
    Join Date
    May 2012
    Posts
    16

    Re: A little help with menu item plz!

    Quote Originally Posted by Paul McKenzie View Post
    Once again:

    http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx

    Please read the link above carefully, and then rewrite your code in accordance to what the information is stating. Your code now is still wrong, even though it seems to work for a window procedure.

    Regards,

    Paul McKenzie
    I see but was what worked, now rewrite my code:
    Code:
    wndproc:
    	case WM_CREATE:
    		EnableMenuItem(GetMenu(hwnd), ID_ITEM1, MF_DISABLED);//by default disabled to test
    
    dlgproc:
    	case WM_COMMAND:
    		switch (LOWORD(wParam))
    		{
    		case ID_BUTTON1:			
    			EnableMenuItem(GetMenu(GetParent(hwnd)), ID_ITEM1, MF_ENABLED);
    			EndDialog (hwnd,TRUE);
    			break;
    is now better

  10. #10
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: A little help with menu item plz!

    Quote Originally Posted by v_nom70 View Post
    I see but was what worked, ...
    If by chance something was working in some situation, it does not obviously mean it's OK.
    To avoid further headaches, you can pay more attention to...
    • ...library documentation when you are using a library;
    • ...answers when you are asking a question.

    Additionally, can put a question as clear as possible.

    Finally, can avoid "all in one line" coding style.
    Make separate calls and eventually see returned values or error codes.
    For example, let's take this line:
    Code:
       EnableMenuItem(GetMenu(hwnd), ID_TEST_DBMANAGER, MF_ENABLED);//DON'T WORK why?
    Why? Can ask Mother Teresa or (re)write the code as follows:
    Code:
       HMENU hMenu = GetMenu(hwnd);
       if(NULL == hMenu)
       {
          OutputDebugStringW(L"The window has not a menu");
       }
       else
       {
          int nRet = EnableMenuItem(hMenu, ID_TEST_DBMANAGER, MF_ENABLED);
          if(-1 == nRet)
          {
             OutputDebugStringW(L"The menu item does not exist");
          }
          // ...
       }
    Of course, it's not absolutely necessary to fill all your code with this kind of tests. But if something is going wrong or is susceptible to fail, it's good to take it into consideration. Or, at least just make separate calls to see the returned values when stepping in DEBUG mode or, a little bit better put ASSERTs on returned values.
    Example:
    Code:
       HMENU hMenu = GetMenu(hwnd);
       _ASSERT(NULL != hMenu); // This window has not a menu, dude!
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  11. #11
    Join Date
    May 2012
    Posts
    16

    Re: [RESOLVED] A little help with menu item plz!

    wow ok, ok I'll remember that. regards.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured