Dynamic Menu Items in C

kishan66
kishan66 used Ask the Experts™
on
hi all,

How to create Menu Items at runtime in C-Windows programming.

example :- if one my function returrns userNmaes  then  each Username has to be added to my Menu "Name".

Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2009

Commented:
What kind of menu are you talking about ? A GUI menu ? If so, it's probably useful to specify what GUI toolkit you are using, but you'll generally be able to modify the contents of a menu at runtime, so there should be no problem to do what you want.

Author

Commented:
hi Infinity08,

i have created Menu intially through Menu resource.
is that wht you need??

Thanks
Top Expert 2009

Commented:
>> i have created Menu intially through Menu resource.

Which GUI toolkit are you using ? Which compiler ?
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Author

Commented:
vc++ 2008
Top Expert 2009

Commented:
And which GUI toolkit ? I'm not likely to be able to help you with VC++ 2008, since I have no experience with that. But listing the information is still necessary for other people to be able to help you.

Btw, you posted this question in the C zone, but you are using a C++ compiler ... Is this question about C or C++ ?

Author

Commented:
its pure C.
im only using VS208 IDE but file extension only 'C'. like 'main.c'

i dont think it matters a lot.
you can suggest me with your code....n i can try it out.
will let you know if it doesnt work.

Thank u.
Top Expert 2009

Commented:
>> i dont think it matters a lot.

It does ... If you want to compile C code as C code, you should use a C compiler, not a C++ compiler ;)


Anyway :

>> you can suggest me with your code....n i can try it out.

I can't suggest any code if I don't even know what GUI toolkit you are using ... Let alone that it's likely that I won't even have experience with your specific GUI toolkit ...

So, first you'll have to clarify what GUI toolkit you are using (if you don't know, just post your code, and I'm sure someone will be able to tell you what you're using). And then I, or likely someone else can help you further with the specifics of adding menu items dynamically.

In any case, this is the kind of thing you can easily look up in your GUI toolkit documentation ... So, that might be another option for you.
masheikSoftware Engineer

Commented:
If it is C then
typedef struct menu
{
 char name[255];
 char type[255];
};
int main()
{
menu MenuItems
  get the value from function and store it in menuItems
 MenuItem.Name=getvalue() ;
printf("1. %s ",MenuItems.Name);
.....
 

Author

Commented:

i have a handler to my MENU
myMenu = GetMenu(hwnd);

and some existing Menu iTems
FILE->open,close
USERS->user1,...

how can i add it to my Menu item to USERS->....
masheikSoftware Engineer

Commented:
The following code may help you ..
I am not tested the code,not sure it 'll run..
something like this ...
//This is a example code;
#include<stdio.h>
#include<conio.h>
#include<string.h>
//Menu Item structure
structure Menu
{
 char UserName[255];
 char OtherItems[255];
};
//fucntion prototype declaration
char[] getUsername()
//main
int main()
{
 struct Menu MenuItem;
 //...do what ever
 MenuItem.UserName=getUsername();
 printf("Menu Items\n");
 printf("1. %s\n",MenuItem.UserName);
 getch();
}
char[] getUsername()
{
 return "User";
}

Open in new window

masheikSoftware Engineer

Commented:
oh ... you mentioned it is pure C so that i gave that exampe...
If it is MFC , i dont know how to do it ..
may be this link can help..

http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/8fa7b89d-0402-4a45-9b2e-acf257d1c50d

can you add other zones Visual C++,Visual studio , MFC ,if you want to add more than
three zones click the request attention and make a request to the moderators to add more zones
Top Expert 2009
Commented:
masheik, what's the point of posting a suggestion if you don't even know if it works ?

Your code will not compile (especially not in C). On top of that, it doesn't even address the question (did you read the question and the follow-up posts ?).
masheikSoftware Engineer

Commented:
i saw the comment no.24906254 after i posted that example
Top Expert 2009

Commented:
>> If it is MFC

Is it MFC, kishan66 ? Or simply Win32 GUI ? Or something else ?
If so, do what masheik suggested, and add the appropriate zones to your question to attract the right experts :

        http://www.experts-exchange.com/help.jsp?hi=373

The C zone is for C related questions, and not for Visual C or MFC or any other platform specific questions ;)
Top Expert 2009

Commented:
>> i saw the comment no.24906254 after i posted that example

It was already established earlier that this was for a GUI menu ;)

Author

Commented:
Hey guys,

i dont u'stand y is the confusion created.

i have mentioned clearly in my first post "C-Windows programming".
i apologize if i created the confusion.

i repeat
--Its pure C - WIN32 ..and i am using VS2008 IDE.
--created Menu Resource file ....
-- just want to add menu items Dynamically(runtime)..to existing Menu Items


 
Top Expert 2009

Commented:
>> i repeat

Well, you should know that "C Windows programming" can mean many different things. I assume your last post means that you confirm it's not MFC or anything else more fancy, and that it's simply Win32 API GUI code ... right ? If so, you might be well served to ask to add some more appropriate zones, like the Visual C++ zone, in order to get experts in here that have experience with Windows GUI programming.

Author

Commented:
Hi Infinity,

i wish to add VC++ zone...but last time when i did that...people gave me reply using C++.
anyways...i wll consider your suggestion.
Top Expert 2009

Commented:
>> but last time when i did that...people gave me reply using C++.

As long as you make clear that you want C code, that should be fine ;) The reason you'd want to add the Visual C++ zone, is because this is specific to the Visual compiler (or to Windows programming in general), and less specific to C, so you're more likely to get the right experts there than here in the C zone.
Author of the Year 2009
Commented:
It is very straight-forward in a Win32 API-only program.  See the attachment.
This assumes that the menubar has three items (say, File  Help  Users) so you want to add ti the submenu whose position identifier is 3 (the first is zero, etc.)
Get the HMENU handl for that submenu, then call
    AppendMenu
   http://msdn.microsoft.com/en-us/library/ms647616(VS.85).aspx
See:
   Menus  
    http://msdn.microsoft.com/en-us/library/ms646977(VS.85).aspx
for an overview od functions and structures for Windows Menus.
-- Dan

void AddItemToMenu( HWND hWnd )
{
	HMENU hmMain=GetMenu(hWnd);
	HMENU hmUsers= GetSubMenu( hmMain, 2 );
 
	char szNewItem[]="&Billy Bob";
 
	BOOL fRet= AppendMenu(
		hmUsers, 
		MF_STRING,         // menu-item options
		1001,              // identifier, menu, or submenu
		szNewItem          // menu-item content
	);
}

Open in new window

Author

Commented:
Hi DanDolins,
Thanks for that..
it works!!!

but my "szNewItem" doesnt show up as a string ..in menu....its weird chars..
though i converted it into LPCSTR..

my code....

	HMENU hMnuShow = GetMenu (hWnd);
HMENU hMnuUser = GetSubMenu(hMnuShow ,0);
	char szNewName[] = "&NewUser";
	char *ptr = szNewName;
	
	LPCTSTR strUserName = (LPCTSTR) ptr;
 
	BOOL fRect = AppendMenu(hMnuUser , MF_STRING ,MNU_USER_VALUE , ptr);

Open in new window

Author of the Year 2009

Commented:
It must be a UNICODE issue.  You've converted the pointer, but not the data to which it points.  Try this:
TCHAR szNewName[] = L"&NewUser";
 BOOL fRect = AppendMenu(hMnuUser , MF_STRING ,MNU_USER_VALUE , szNewName);

Author

Commented:
Hey DanRollins,

Thanks for the response..

your solution above works fine for a string litreral.
can you please even mention how to handle the same issue(unicode) when i have to read from a function parameter...which is a  char *.

i tried but..could not succed.....this is what i need..
as u can see in the code... i have a function addUsers(char *name)...
void AddUserToMenu(HWND hWnd , char *uname)
{
      HMENU hMnuShow = GetMenu (hWnd);
     HMENU hMnuUser = GetSubMenu(hMnuShow ,0);
        
   // now how should i use this "uname" in my AppendMenu       
         
        BOOL fRect = AppendMenu(hMnuUser , MF_STRING ,MNU_USER_VALUE , uname);
 
  

Open in new window

Author of the Year 2009

Commented:
Here's the deal:  
If AppendMenu is expecting a UNICODE string, it is because your project settings have specified a UNICODE build.  In that case, I'd expect that strings that come in as parameters to the function are already UNICODE strings.
Alas, that appears to not be the case.  Since the incoming string is a pointer to a string of 8-bit characters, you need to either convert it to UNICODE, or call the version of the API function that expects 8-bit text.
The "quick" fix is to call AppendMenuA(...) rather than AppendMenu().
An alternative to that would be to make your entire program as a non-UNICODE build.  Just change your build settings "Project Defaults / Character Set" from...
   Use Unicode Character Set
...to...
  Use Multi-Byte Character Set
After that, all API functions (such as AppendMenu) will automatically use the "A" version and will expect 8-bit characters.
If you want to go the other route (convert the parameter string from 8-bit to 16-bit), just say so and I'll show you how.  But having a program that needs to convert back and forth all of the time is a pain and I don't recommend it.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial