Add custom MENUITEM to a POPUP?

Hi I have a POPUP

1
2
3
4
5
6
7
8
9
10
11
12
13

MAIN_MENU MENU
{
     POPUP "&Manager"
     {
         MENUITEM "New Royal Python", MENU_NEW_ROYAL_PYTHON
         POPUP "&Select Python"
         {

         }
     }

}


I basically want to give the POPUP an ID so I can talk with it but it does not seem to be accepting a second parameter. Once I have done that I need to send a message to it to ask it to add a new item do any of you know the messages I need to send for this?

Thanks,
Dan
bump
bump --
This is not C++. What sort of response were you looking for?
-.- It involves the Windows API this is the best place I can post for this. Not to mention you can code in C, C++ to get this job done. Does anyone who has better things to do than post argumentative comments know how to accomplish this task please?
It involves the Windows API this is the best place I can post for this.

Not, say, the Windows Programming forum, rather than the General C++ Programming forum?


Not to mention you can code in C, C++ to get this job done.

What you have posted is neither C nor C++.


Does anyone who has better things to do than post argumentative comments know how to accomplish this task please?

My comment was not argumentative. It was an observation that the what you posted is not on topic here. When confronted with a post that is obviously off-topic, that is bumped repeatedly by the OP without adding any extra information or context, I assume the OP is unaware of the faux pas he or she is committing and bring his or her attention to it.

Your response, however, might be classified as dismissive and argumentative, which leads me to believe that I may have been wrong to give you the benefit of the doubt and that, in fact, you were aware the post was off-topic and instead of researching the correct place to post (or the subject matter of the post) you just posted it "wherever" and bumped it repeatedly in the hopes that someone would happen across it and give you a solution to your problem. This would indicate you believe your time is more precious than those who frequent this forum expecting to encounter on-topic posts and responses and is rather rude.

For someone who claims to be able to "teach":
http://www.cplusplus.com/forum/jobs/135217/
your disregard for others and inability to google or even look at forum names doesn't really recommend you.

this is similar to resource file, but it wont compile in c/cpp.

Typically it would look like this:
1
2
3
4
5
6
7
8
9
10
11
12
MAIN_MENU MENU
BEGIN
 POPUP "&Manager"
	BEGIN
	 MENUITEM "New Royal Python", ID1
		 POPUP "&Select Python"
		 BEGIN
		 //in w32 popop cannot be empty, so you insert new elements through ID2
			MENUITEM "text", ID2
		 END
	 END
END
Thanks tath. Is their any way at all I can communicate with the POPUP via the WindowsAPI as these items need to be added at run time
Last edited on
I wrote example for you:
resource.h
1
2
3
4
5
6
7
8
#define IDR_MAINMENU 100
#define ID_EXIT_PROGRAM 101
#define ID_ABOUT 102
#define ID_DYNAMIC_MENU_ITEMS 1000
#define ID_DYNAMIC 104

#define IDR_ADD_BUTTON 105
#define IDR_REMOVE_BUTTON 106 


resource.rc
1
2
3
4
5
6
7
8
9
10
11
#include "resource.h"

IDR_MAINMENU MENU DISCARDABLE 
BEGIN
    POPUP "&File"
        BEGIN
            MENUITEM "E&xit",   ID_EXIT_PROGRAM
        END
        MENUITEM "&Dynamic Menu",   ID_DYNAMIC
        MENUITEM "&About",   ID_ABOUT
END


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <windows.h>
#include <string>
#include <vector>
#include "resource.h"
#include <sstream>

template <typename T>
std::wstring NumberToString ( T Number )
  {
    std::wostringstream out;
    out << Number;
    return out.str();
  }

LRESULT CALLBACK WinProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);

HWND g_hMainWindow = 0;
HWND g_hButtonAdd = 0;
HWND g_hButtonRemove = 0;

HMENU g_hMainMenu = 0;
HMENU g_hDynamicMenu = 0;

std::vector<HMENU> g_dynamicItems;
HMENU g_dynamicId = (HMENU)ID_DYNAMIC_MENU_ITEMS;

int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrevInst,LPSTR lpCmdLine,int nShowCmd)
{ 
    MSG msg = {0};
    WNDCLASSEX wClass = {0};

    wClass.cbClsExtra =        NULL;
    wClass.cbSize =            sizeof(WNDCLASSEX);
    wClass.cbWndExtra =        NULL;
    wClass.hbrBackground =    (HBRUSH)COLOR_WINDOW;
    wClass.hCursor =        LoadCursor(NULL,IDC_ARROW);
    wClass.hIcon =            NULL;
    wClass.hIconSm =        NULL;
    wClass.hInstance =        hInst;
    wClass.lpfnWndProc =    static_cast<WNDPROC>( WinProc);
    wClass.lpszClassName =    TEXT( "Main Window Class");
    wClass.lpszMenuName =    MAKEINTRESOURCE(IDR_MAINMENU);
    wClass.style =            0;//CS_HREDRAW | CS_VREDRAW;

    RegisterClassEx(&wClass);

    //main window
    g_hMainWindow = CreateWindowEx(NULL,
            TEXT("Main Window Class"),
            TEXT("Main Window"),
            WS_OVERLAPPEDWINDOW,
            200,
            200,
            600,
            400,
            NULL,
            NULL,
            hInst,
            NULL);

    //add item button
    g_hButtonAdd = CreateWindowEx(
            WS_EX_CLIENTEDGE,
            TEXT("BUTTON"),
            TEXT("Add Item"),
            WS_TABSTOP|WS_VISIBLE|WS_CHILD,
            100,
            5,
            70,
            30,
            g_hMainWindow,
            (HMENU)IDR_ADD_BUTTON,
            hInst,
            NULL);

    //remove item button
    g_hButtonRemove = CreateWindowEx(
            WS_EX_CLIENTEDGE,
            TEXT("BUTTON"),
            TEXT("Remove Last Item"),
            WS_TABSTOP|WS_VISIBLE|WS_CHILD,
            200,
            5,
            150,
            30,
            g_hMainWindow,
            (HMENU)IDR_REMOVE_BUTTON,
            hInst,
            NULL);

    ShowWindow( g_hMainWindow, SW_SHOWNORMAL);

    g_hMainMenu = GetMenu( g_hMainWindow);

    while( GetMessage(&msg,NULL,0,0) ) //TODO add >0 for error checking
    {
        TranslateMessage( &msg );
        DispatchMessage( &msg );
    }

    return 0;
}

LRESULT CALLBACK WinProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {        
        case WM_COMMAND:
            if ( LOWORD(wParam) >= ID_DYNAMIC_MENU_ITEMS) 
            {
                //case ID_DYNAMIC_MENU_ITEMS:
                for (unsigned i=0; i< g_dynamicItems.size(); i++)
                {
                    if (LOWORD(wParam) == (WORD)g_dynamicItems.at(i))
                    {
                        std::wstring buffer=L"selected Element ";
                        buffer += NumberToString(i);
                        MessageBox( g_hMainWindow, buffer.c_str(), L"test", 0);
                    }
                }
            }
            else
            {
                switch ( LOWORD(wParam))
                {
                    case IDR_ADD_BUTTON:
                        {
                            //create dynamic popup menu
                            if ( g_hDynamicMenu!=0)
                                DestroyMenu(g_hDynamicMenu);

                            g_hDynamicMenu = CreatePopupMenu();

                            //here add as many items at run time, as you want
                            //i used vector, but you can list files, strings, etc...
                            g_dynamicItems.push_back(++g_dynamicId);

                            for (unsigned i=0; i< g_dynamicItems.size(); i++)
                            {
                                std::wstring buffer=L"Element ";
                                buffer += NumberToString(i);
                                InsertMenu( g_hDynamicMenu, -1, 
                                    MF_BYPOSITION | MF_STRING, 
                                    (UINT_PTR)g_dynamicItems.at(i), 
                                    buffer.c_str());
                            }

                            //Add popup menu to main menu
                            ModifyMenu( g_hMainMenu, 1, 
                                MF_BYPOSITION | MF_POPUP | MF_STRING, 
                                (UINT_PTR)g_hDynamicMenu, L"&Dynamic Menu");
                        }
                        return 0;
                    case IDR_REMOVE_BUTTON:
                        {
                            if ( g_dynamicItems.size() > 0)
                            {
                                //remove last item
                                RemoveMenu(g_hDynamicMenu, 
                                    (UINT)g_dynamicItems.at(g_dynamicItems.size()-1), 
                                    MF_BYCOMMAND);
                                g_dynamicItems.pop_back();
                                g_dynamicId--;
                            }
                        }
                        return 0;
                    case ID_ABOUT:
                        MessageBox( g_hMainWindow, L"tath", L"About", 0);
                        return 0;
                        return 0;
                    case ID_EXIT_PROGRAM:
                        PostQuitMessage(0);
                        return 0;
                }
            }
            break;

        case WM_CREATE:
            break;

        case WM_DESTROY:
        {
            PostQuitMessage(0);
            return 0;
        }
    }

    return DefWindowProc( hWnd, msg, wParam, lParam);
}


this is just prove of concept, so i didnt play with error checks and stuff
Topic archived. No new replies allowed.