Addmenus

Hello, Cplusplus.

I have a problem when I Append the Menu Item
a get an error

the argument of type const char" is incompatible with parameter of type "LPCWSTR"
will add the word "File"

I will keep looking on the internet for the answer and looking at this post.


void Addmenus(HWND hWnd)
{
hMenu = CreateMenu();

AppendMenu(hMenu, MF_STRING, NULL, "File");

}



Tnx
just say
CString filestr = "File";
and put that in the last parameter and it should be ok.
LP means pointer. CW means wide char. So it wants a wide char pointer C style string here ... you get used to reading this gibberish after a while ... the M$ CString class is somewhere between a real string (std::string) and a C string (char array) and it will do what needs doing for you here while using their type that works with their weird functions.

I think there is a prefix you could add instead, _T or something weird, eg _T("file") or something like that can force the normal const char* to wide and it may work (if it accepts const in the parameter).
Last edited on
You are compiling as Unicode - hence the requirement for a LPCWSTR for the "file" param. AppendMenu() (and many other windows APIs) have different versions depending upon whether unicode is used or not (default is used). There's a couple of options here:

1) Specify "file" as a unicode string by the L prefix:

 
AppendMenu(hMenu, MF_STRING, NULL, L"File");


or use the _T macro to specify either unicode or ascii as per whether unicode is being used or not:

 
AppendMenu(hMenu, MF_STRING, NULL, _T("File"));


2) Use the ANSI version of the function:

 
AppendMenuA(hMenu, MF_STRING, NULL, "File");

Last edited on
Not every function in the Windows API has Unicode (wide char) or ANSI versions, but recommended when you use one you specify the version. Never let the compiler decide.

Either

AppendMenuW(hMenu, MF_STRING, NULL, L"File");

Or

AppendMenuA(hMenu, MF_STRING, NULL, "File");

If you are using a current version of Visual Studio it defaults to using the wide char/Unicode character set.

You can specify the ANSI version, though with modern Windows being 32-bit that incurs a conversion cost to translate from 32-bit to 16-bit.

Long story short, just use the wide char versions and character string encodings. It will save you a lot of grief. Unless you have very specific reasons for going ANSI in a block of code.
Last edited on
sold the puzel,

void AddMenus(HWND hwnd) {



HMENU hMenu = CreateMenu();
HMENU HfileMenu = CreateMenu();
HMENU hSubMenu = CreateMenu();



AppendMenuW(hSubMenu, MF_STRING, NULL, L"SubMenu Item");
AppendMenuW(HfileMenu, MF_STRING, IDM_FILE_NEW, L"New");
AppendMenuW(HfileMenu, MF_STRING, IDM_FILE_OPEN, L"Open");
AppendMenuW(HfileMenu, MF_SEPARATOR, NULL, NULL);
AppendMenuW(HfileMenu, MF_STRING, FILE_MENU_EXIT, L"Exit");
AppendMenuW(hMenu, MF_POPUP, (UINT_PTR)HfileMenu, L"File");
AppendMenuW(hMenu, MF_STRING, NULL, L"Help");

SetMenu(hwnd, hMenu);

}
Topic archived. No new replies allowed.