-
April 14th, 2010, 09:12 PM
#1
Adding an item to an existing CMenu
VS2005 C++ MFC
When I try to add a new Menu Item to an existing Menu, the new item never shows up. All I see are the existing menu items from the Resource.
Code:
// Load SysTrayMenu (WORKS OK, IDR_MENU1 is a Menu Resource with five items)
if(SysTrayMenu.LoadMenu(IDR_MENU1)==NULL) AfxMessageBox("LoadMenu Failed");
// Add a new item to the existing menu (FAILS)
MENUITEMINFO MenuItem;
ZeroMemory(&MenuItem, sizeof(MENUITEMINFO));
MenuItem.cbSize = sizeof(MENUITEMINFO);
MenuItem.fMask = MIIM_TYPE|MIIM_ID|MIIM_STATE; //MIIM_STRING|MIIM_ID;
MenuItem.wID = ID_POPUP_ADMIN;
MenuItem.fType = MFT_STRING;
MenuItem.fState = MFS_ENABLED;
MenuItem.cch = 6;
MenuItem.dwTypeData = "Admin";
SysTrayMenu.InsertMenuItem(3, &MenuItem, TRUE);
// Lets try again. Add a new item to the existing menu (FAILS AS WELL)
SysTrayMenu.InsertMenu(4, MF_STRING|MF_BYPOSITION|MF_ENABLED, ID_POPUP_ADMIN, "Admin");
// Has a migraine... lets try again (FAILS AS WELL)
SysTrayMenu.AppendMenu(MF_BYPOSITION, ID_POPUP_ADMIN, "Admin");
// Apparently we need to call this to redraw the menu
DrawMenuBar();
Any ideas?
Last edited by Anarchi; April 14th, 2010 at 10:50 PM.
-
April 15th, 2010, 01:46 AM
#2
Re: Adding an item to an existing CMenu
-
April 15th, 2010, 02:02 AM
#3
Re: Adding an item to an existing CMenu
Its a standard CMenu.
CMenu SysTrayMenu;
-
April 15th, 2010, 02:43 AM
#4
Re: Adding an item to an existing CMenu
Originally Posted by Anarchi
Its a standard CMenu.
CMenu SysTrayMenu;
So where is it declared and how is it used after LoadMenu/InsertMenu?
Victor Nijegorodov
-
April 15th, 2010, 05:59 PM
#5
Re: Adding an item to an existing CMenu
The CMenu is declared as a member in the main MFC Dialog class.
The menu is activated when right-clicking the system tray icon.
I use the CSysTrayIcon Class made by Prateek Kaul, found here:
http://www.codeguru.com/Cpp/misc/mis...int.php/c3891/
To popup the menu, I handle WM_SYSTRAYMSG in PreTranslateMessage and call:
SysTrayIcon.OnContextMenu(SysTrayMenu.GetSubMenu(0), ptMouse, this);
The above function contains:
Code:
BOOL CSysTrayIcon::OnContextMenu(CMenu* pContextMenu, CPoint ptMouse, CWnd* pWndMessageHandler)
{
// The window is brought forward, so that it starts
// receving messages, rather than currently active window.
// If we don't do this the messages of the context menu will
// go to the currently active window.
pWndMessageHandler->SetForegroundWindow();
BOOL bResult = pContextMenu->TrackPopupMenu(
TPM_LEFTALIGN | TPM_RIGHTBUTTON,
ptMouse.x,
ptMouse.y,
pWndMessageHandler
);
return bResult;
}
Do you think that System Tray Icon class may not be handling the menu modification properly?
Maybe I should try adding "DrawMenuBar()" to that function, rather than calling DrawMenuBar in InitDialog()
-
April 15th, 2010, 06:42 PM
#6
Re: Adding an item to an existing CMenu
I added the below line in that systemtrayicon class, but it didnt make a difference.
:rawMenuBar(AfxGetMainWnd()->m_hWnd);
By the way, when I call InsertMenuItem() in the main InitDialog function, its return value is successful.
-
April 16th, 2010, 03:50 PM
#7
Re: Adding an item to an existing CMenu
SysTrayMenu.InsertMenuItem(3, &MenuItem, TRUE);
. . .
To popup the menu, I handle WM_SYSTRAYMSG in PreTranslateMessage and call:
SysTrayIcon.OnContextMenu(SysTrayMenu. GetSubMenu(0), ptMouse, this);
Don't you see any problem with such insertion and further use?
Best regards,
Igor
-
April 17th, 2010, 09:00 PM
#8
Re: Adding an item to an existing CMenu
Originally Posted by Igor Vartanov
Don't you see any problem with such insertion and further use?
Interesting, why is that "GetSubItem" Thanks I'll look into it.
-
April 18th, 2010, 01:39 AM
#9
Re: Adding an item to an existing CMenu
Originally Posted by Anarchi
Interesting, why is that "GetSubItem" Thanks I'll look into it.
Where do you see (or have) GetSubItem?
I don't see any...
Besides, you don't need to call DrawMenuBar.
Last edited by VictorN; April 21st, 2010 at 02:31 AM.
Victor Nijegorodov
-
April 20th, 2010, 06:06 PM
#10
Re: Adding an item to an existing CMenu
Sorry I meant "GetSubMenu(0)"
I still haven't figured it out, maybe I'll just work around the problem and add a hotkey or something.
-
April 21st, 2010, 02:09 AM
#11
Re: Adding an item to an existing CMenu
You definitely need do some reading on menus in Windows.
What they used to call a popup menu is a submenu in fact.
Now, you show submenu 0 but add new menu item to the parent menu. So once again, don't you see any problem with this?
Last edited by Igor Vartanov; April 21st, 2010 at 02:11 AM.
Best regards,
Igor
-
April 21st, 2010, 07:46 PM
#12
Re: Adding an item to an existing CMenu
I'm confused, so do I need to add the new item to the submenu? (instead of the parent menu)
Why do Microsoft make things so complicated lol
-
April 22nd, 2010, 12:56 AM
#13
Re: Adding an item to an existing CMenu
Originally Posted by Anarchi
,,, Why do Microsoft make things so complicated lol
Well where did you see less "complicated" menus?
Victor Nijegorodov
-
April 22nd, 2010, 02:03 AM
#14
Re: Adding an item to an existing CMenu
Originally Posted by Anarchi
I'm confused, so do I need to add the new item to the submenu? (instead of the parent menu)
If I were you, I would already be re-reading MSDN and have made twenty tries instead of a doubtful asking here.
Why do Microsoft make things so complicated lol
Sorry, but complicated it looks only because of your lack of experience. Again, if I were you, I would withhold from judging something I do not completely understand.
Best regards,
Igor
-
April 22nd, 2010, 06:18 PM
#15
Re: Adding an item to an existing CMenu
Thanks for the help Igor.
I haven't had time to try again (very busy at work ) but I'll have another read of MSDN when I get a chance.
With a lot of Microsoft coding, you need to spend a huge amount of time understand it properly
They should KISS (keep it simple silly) or at least provide wrappers to simplify common tasks like they do in Visual Basic.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|