CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 30

Thread: C++ PopUpMenu

  1. #1
    Join Date
    Aug 2009
    Posts
    219

    C++ PopUpMenu

    Hello,

    I'm trying to create a PopUpMenu, I'm not sure that's the real name. I mean the window that appears when u right click at this site for example. I want to have such a thing, which API should I use?

    And I want to create 2 seperates things, one when u right click in the ListView, and one when u click at another ListView.

    Thanks,

    NM

  2. #2
    Join Date
    Feb 2005
    Posts
    2,160

    Re: C++ PopUpMenu

    First you need the menu resource. Then you'll want to handle the WM_CONTEXTMENU message in your message loop. From there, it depends on your framework. Are you using MFC, some other framework, or straight WinAPI?

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: C++ PopUpMenu

    Also have a look at TrackPopupMenu(Ex) and other menu functions in MSDN.
    There is an MFC example of Context menu but you could very easy to adopt it to the plain Win32 type.
    Victor Nijegorodov

  4. #4
    Join Date
    Aug 2009
    Posts
    219

    Re: C++ PopUpMenu

    Quote Originally Posted by hoxsiew View Post
    First you need the menu resource. Then you'll want to handle the WM_CONTEXTMENU message in your message loop. From there, it depends on your framework. Are you using MFC, some other framework, or straight WinAPI?
    I code in pure api, what kind of resource do you mean? I dont use a .rc file to create my window.

    Thanjks, ill take a look at that.

    I can only find some things about recourse file with it, none in pure code.
    Last edited by NLscotty; July 28th, 2010 at 01:22 PM.

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: C++ PopUpMenu

    Then you must use Menu APIs to create context menu.
    Victor Nijegorodov

  6. #6
    Join Date
    Aug 2009
    Posts
    219

    Re: C++ PopUpMenu

    I came up with this

    Code:
    case WM_CREATE:
    			hmenu = CreatePopupMenu();
    			memset(&emii, 0, sizeof(MENUITEMINFO));
    			emii.cbSize = sizeof(MENUITEMINFO);
    			emii.fMask = MIIM_STRING | MIIM_FTYPE;
    			emii.fType = MIIM_STRING;
    			emii.dwTypeData = TEXT("Copy");
    			emii.cch = 4;
    			InsertMenuItem(hmenu, 0, TRUE, (LPCMENUITEMINFO) &emii);
    Code:
    if(wParam == IDC_BLOCK)
    			{
    				 TrackPopupMenu(hmenu,TPM_LEFTBUTTON,100,100,0,hWnd,0);
    x = GetLastError();
    if i use GetLasterror at the second code, error 1401:Invalid menu handle.

  7. #7
    Join Date
    Feb 2005
    Posts
    2,160

    Re: C++ PopUpMenu

    Where is hmenu declared? It is going to have to be global if you assign it in WM_CREATE then use it later in a different message handler.

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: C++ PopUpMenu

    And where does your second code block come from? And what is IDC_BLOCK?

    Note that you should NOT call GetLastError unless an API called just before has failed.
    Victor Nijegorodov

  9. #9
    Join Date
    Aug 2009
    Posts
    219

    Re: C++ PopUpMenu

    hemnu is declared in my wndproc (main window loop), so that shoudl be fine.

    IDC_BLOCK is name of a button of mine, so if its clicked, it should show me the popupmenu.
    It should not work at a button alter, but this was for test prupose.

  10. #10
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: C++ PopUpMenu

    Quote Originally Posted by NLscotty View Post
    hemnu is declared in my wndproc (main window loop), so that shoudl be fine..
    Sorry, but we cannot believe...
    Please, show your code (snippets) with all that declarations/definitions...
    Victor Nijegorodov

  11. #11
    Join Date
    Aug 2009
    Posts
    219

    Re: C++ PopUpMenu

    Code:
    HINSTANCE hInstance = NULL;
    LPCWSTR g_szClassName = TEXT("myWindowClassPEsend");
    LPCWSTR g_szClassName1 = TEXT("myWindowClassPErecv");
    
    HWND hWnd = 0;
    
    HWND RecvListView,hHookBut,hSendButton,hSendEdit,hIngBloc,hClean,hBlocIng,hBlock,hIngore,hBlcEd,hIngEd,hBlorIg;
    HWND RecvListView2,hHookBut2,hSendButton2,hSendEdit2,hUnhook2,hClean2;
    
    #define IDC_HOOK  101
    #define IDC_SEND  102
    #define IDC_EDITSEND 103
    #define IDC_INBL 106
    #define IDC_CLEAN 105
    #define IDC_LIST 107
    #define IDC_IGNOR 108
    #define IDC_BLIN 109
    #define IDC_BLOCK 110
    #define IDC_INGORE 111
    #define IDC_INGED 112
    #define IDC_BLCED 113
    
    #define IDC_HOOK2  101
    #define IDC_SEND2  102
    #define IDC_EDITSEND2 103
    #define IDC_UNHOOK2 106
    #define IDC_CLEAN2 105
    #define IDC_LIST2 107
    wchar_t szIgnored[MAX_PACKET_LEN], szBlocked[MAX_PACKET_LEN];
    
    LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    	HFONT HhFont = CreateFont(20, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_STRING_PRECIS, CLIP_DEFAULT_PRECIS, CLEARTYPE_QUALITY, VARIABLE_PITCH, TEXT("Calibri"));
    	HFONT hFont = CreateFont(15, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_STRING_PRECIS, CLIP_DEFAULT_PRECIS, CLEARTYPE_QUALITY, VARIABLE_PITCH, TEXT("Calibri"));
    	HFONT sFont = CreateFont(12, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_STRING_PRECIS, CLIP_DEFAULT_PRECIS, CLEARTYPE_QUALITY, VARIABLE_PITCH, TEXT("Calibri"));
    	InitCommonControls();
    	OleInitialize(0);
    	LV_COLUMN* lvc = new LV_COLUMN;
    	HMENU hmenu;
    	MENUITEMINFO emii;
    	int x = 0;
    	switch(msg)
        {
    		case WM_CREATE:
    			hmenu = CreatePopupMenu();
    			x = GetLastError();
    			memset(&emii, 0, sizeof(MENUITEMINFO));
    			emii.cbSize = sizeof(MENUITEMINFO);
    			emii.fMask = MIIM_STRING | MIIM_FTYPE;
    			emii.fType = MIIM_STRING;
    			emii.dwTypeData = TEXT("Copy");
    			emii.cch = 4;
    			InsertMenuItem(hmenu, 0, TRUE, (LPCMENUITEMINFO) &emii);
    
    
    
    
    
    			RecvListView = CreateWindowEx(WS_EX_STATICEDGE,WC_LISTVIEW,NULL,WS_VISIBLE | WS_CHILD | WS_BORDER | LVS_REPORT ,75, 10, 455, 200,hWnd,(HMENU)IDC_LIST,hInstance,NULL);
    			hHookBut = CreateWindowEx(WS_EX_LEFT, TEXT("Button"), TEXT("Hook"), WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON, 9, 10, 55, 22, hWnd, (HMENU)IDC_HOOK, hInstance, NULL);
    			hSendButton = CreateWindowEx(WS_EX_LEFT, TEXT("Button"), TEXT("Send"), WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON, 455, 213, 75, 26, hWnd, (HMENU)IDC_SEND, hInstance, NULL);
    			hClean = CreateWindowEx(WS_EX_LEFT, TEXT("Button"), TEXT("Clean"), WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON, 9, 32, 55, 22, hWnd, (HMENU)IDC_CLEAN, hInstance, NULL);
    			hIngBloc = CreateWindowEx(WS_EX_STATICEDGE,WC_LISTVIEW,NULL,WS_VISIBLE | WS_CHILD | WS_BORDER | LVS_REPORT ,37, 160, 35, 86,hWnd,(HMENU)IDC_INBL, hInstance, NULL);
    			hBlocIng = CreateWindowEx(WS_EX_STATICEDGE,WC_LISTVIEW,NULL,WS_VISIBLE | WS_CHILD | WS_BORDER | LVS_REPORT ,2, 160, 35, 86,hWnd, (HMENU)IDC_BLIN, hInstance, NULL);
    			hBlock = CreateWindowEx(WS_EX_LEFT, TEXT("Button"), TEXT("Block"), WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON, 9, 54, 55, 22, hWnd, (HMENU)IDC_BLOCK, hInstance, NULL);
    			hIngore = CreateWindowEx(WS_EX_LEFT, TEXT("Button"), TEXT("Ingore"), WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON, 9, 97, 55, 22, hWnd, (HMENU)IDC_INGORE, hInstance, NULL);
    			hSendEdit = CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("Edit"), TEXT("//Packet"), WS_TILED | WS_VISIBLE | WS_CHILD | ES_UPPERCASE, 76, 216, 365, 20, hWnd, (HMENU)IDC_EDITSEND, hInstance, NULL);
    			hBlcEd = CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("Edit"), TEXT("00"), WS_TILED | WS_VISIBLE | WS_CHILD | ES_UPPERCASE, 9, 77, 55, 18, hWnd, (HMENU)IDC_BLCED, hInstance, NULL);
    			hIngEd = CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("Edit"), TEXT("00"), WS_TILED | WS_VISIBLE | WS_CHILD | ES_UPPERCASE, 9, 120, 55, 18, hWnd, (HMENU)IDC_INGED, hInstance, NULL);
    			hBlorIg = CreateWindowEx(WS_EX_LEFT, TEXT("Button"), TEXT("ShowBlocked"), WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON, 9, 140, 55, 26, hWnd, (HMENU)IDC_IGNOR, hInstance, NULL);
    
    			SendMessage(hIngEd, WM_SETFONT, (WPARAM)hFont, TRUE);
    			SendMessage(hBlcEd, WM_SETFONT, (WPARAM)hFont, TRUE);
    			SendMessage(hBlock, WM_SETFONT, (WPARAM)hFont, TRUE);
    			SendMessage(hIngore, WM_SETFONT, (WPARAM)hFont, TRUE);
    			SendMessage(RecvListView, WM_SETFONT, (WPARAM)hFont, TRUE);
    			SendMessage(hHookBut, WM_SETFONT, (WPARAM)hFont, TRUE);
    			SendMessage(hSendEdit, WM_SETFONT, (WPARAM)hFont, TRUE);
    			SendMessage(hSendButton, WM_SETFONT, (WPARAM)HhFont, TRUE);
    			SendMessage(hClean, WM_SETFONT, (WPARAM)hFont, TRUE);
    			SendMessage(hBlocIng, WM_SETFONT, (WPARAM)sFont, TRUE);
    			SendMessage(hIngBloc, WM_SETFONT, (WPARAM)sFont, TRUE);
    	
    			ListView_SetExtendedListViewStyle(RecvListView, LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | 0x01000000 | LVS_EX_DOUBLEBUFFER | LVS_EX_TWOCLICKACTIVATE );
    			ListView_SetExtendedListViewStyle(hIngBloc, LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | 0x01000000 | LVS_EX_DOUBLEBUFFER | LVS_EX_TWOCLICKACTIVATE );
    			ListView_SetExtendedListViewStyle(hBlocIng, LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | 0x01000000 | LVS_EX_DOUBLEBUFFER | LVS_EX_TWOCLICKACTIVATE );
    			for (int i = 1; i <= 4; i++)
    			{
    				 InsertLVColumn(RecvListView, i, i, szColumnHeadings[i - 1], iColumnWidths[i - 1], (i == 4 ? false : true));
    			}
    			InsertLVColumn(hBlocIng, 1, 1, TEXT("Blc"), 31, true);
    			InsertLVColumn(hIngBloc, 1, 1, TEXT("Ign"), 31, true);
    			
    			SecureZeroMemory(szIgnored, MAX_PACKET_LEN * sizeof(wchar_t));
    			SecureZeroMemory(szBlocked, MAX_PACKET_LEN * sizeof(wchar_t));
    			break;
    
    
    		case WM_COMMAND:
    			if(wParam == IDC_HOOK)
    			{
    				CreateThread(0, 0, (LPTHREAD_START_ROUTINE)&InstallMapleStorySendHook, 0, 0, 0);
    				SetDlgItemTextA(hWnd,IDC_HOOK,"Unhook");
    			}
    			if(wParam == IDC_SEND)
    			{
    				char buffer[5000];
    				GetDlgItemTextA(hWnd,IDC_EDITSEND ,buffer,5000);
    				eraseAllBlanks(buffer);
                    msSendPacketA(buffer);
    			}
    			if(wParam == IDC_BLOCK)
    			{
    				 TrackPopupMenu(hmenu,TPM_LEFTBUTTON,100,100,0,hWnd,0);
    				 GetDlgItemText(hWnd, IDC_BLCED,szBlocked, MAX_PACKET_LEN);
    				 LVITEM lvi;
    	 
    				ZeroMemory(&lvi, sizeof(lvi));
    				lvi.mask = LVIF_TEXT;
    				lvi.iItem = SendMessage(RecvListView, LVM_GETITEMCOUNT, 0, 0); /* or set to 0 for testing */
    				lvi.iSubItem = 0;
    				lvi.pszText = szBlocked;
    				lvi.cchTextMax = lstrlen(lvi.pszText) + 1;
    				ListView_InsertItem(hBlocIng, &lvi);
    			}

  12. #12
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: C++ PopUpMenu

    Quote Originally Posted by NLscotty View Post
    Code:
    LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    	...
    	HMENU hmenu;
    Please re-read hoxsiew post # 7 above.
    When your WndProc is called with WM_COMMAND message, that hmenu contains garbage.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  13. #13
    Join Date
    Aug 2009
    Posts
    219

    Re: C++ PopUpMenu

    thanks that worked, which i define as weird, but well it works

    Next problem is that when i add 2 lines, they are next each other, and not under each other, it looks weird, and how can i make it that if people right clcik at a treeview, the window appears and that when they hit ignore, the value of the third colum in a char gets?

  14. #14
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: C++ PopUpMenu

    thanks that worked, which i define as weird
    Weird was your declaring it in function scope. Right after processing WM_CREATE the function returns, and your menu handle gets lost. Next time when you process button click you use uninitialized variable value and fail with no doubt.
    Best regards,
    Igor

  15. #15
    Join Date
    Aug 2009
    Posts
    219

    Re: C++ PopUpMenu

    I see, thanks for all the help, but

    How can i make it like that, when someone right click at a listview box, the window appears, and when they click at something the char from that colum gets in a char.

    Grz
    NM

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured