Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the menu handle

Tags:

winapi

I created menus and sub menus using the resource editor in visual studio. I now want to add items to one of the menus during run time. I was going to use the InsertMenuItem function but I don't know how to get access to the HMENU variable.

like image 708
Student123 Avatar asked Feb 22 '26 12:02

Student123


1 Answers

LoadMenu seems is what you need. Use it to load menu from resource editor, something like this:

HMENU yourMenu = LoadMenu( hInst,  // variable where you stored your HINSTANCE
    MAKEINTRESOURCE(IDM_MENU1) );  // replace IDM_MENU1 with the ID of your menu

Here are lots of useful examples, you may find very useful. Some of them address your issue, and some might be useful to you in the future. I would study the Example of Menu-Item Bitmaps section if I were you...

If you need a menu handle that is already assigned to a window then use GetMenu as member arx said. Something like this:

HMENU yourMenu = GetMenu(hWnd);  // hWnd is the HWND of the window that owns your menu

Do not forget to destroy the menu when it is no longer needed ( usually upon window destruction ) with DestroyMenu.

This example might help you as well. This is very good introductory tutorial for Win32, I suggest you to read it ( just go to the home page and download both PDF and .zip file with code examples ).

As I have said before, your question is not entirely clear, so if you have further questions leave me a comment.

Hopefully this answer solved your problems. Best regards.

like image 127
AlwaysLearningNewStuff Avatar answered Feb 24 '26 20:02

AlwaysLearningNewStuff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!