Text box default contextmenu

This is a discussion on Text box default contextmenu within the Windows Programming forums, part of the Platform Specific Boards category; You know that default context (right-click) menu that comes with text boxes? The one that has Cut, Copy, Paste, and ...

  1. #1
    Chad Johnson
    Join Date
    May 2004
    Posts
    154

    Text box default contextmenu

    You know that default context (right-click) menu that comes with text boxes? The one that has Cut, Copy, Paste, and Select All? How can I add that same menu to my window's main menu bar without having to implement all the menu enables/disables myself? Is there a standard method?

  2. #2
    uh oh
    Join Date
    Jan 2005
    Location
    Ontario, CA
    Posts
    66
    Not that I know of. You will most likely just have to add it yourself, which you can also do on the fly using CreateMenu(), AppendMenu(), etc. functions. Do a search on MSDN in order to see how to do this.

  3. #3
    Chad Johnson
    Join Date
    May 2004
    Posts
    154
    Alright. Shouldn't be too difficult to do myself. I'm planning on just making a menu that's part of the parent's class, then when a right-click happens, I'll test the parent for a selection. Based on whether anything is selected, or if anything is in the clipboard, I'll enable/disable the menu items.

    If anybody knows of a better way, you best speak up.

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    I coded a simple text editor as one of my first C Windows programs. I found some code that could help you. Sorry for the wide post, poor formatting, etc:

    This is the code to handle WM_MENUSELECT:
    Code:
    	case WM_MENUSELECT:
    		//A menu has been selected but not clicked
    		//Make sure menuitems are enabled or not as needed
    		if (lParam != 0)  //a menu has been selected.
    			MenuSelect( LOWORD(wParam), lParam);
    
    		return TRUE;
    This is the MenuSelect function:
    Code:
    void MenuSelect(INT menu, LPARAM lParam) {
    //menu will be id of selected menu
    //lParam is handle to main menu.
    
    INT selection;
    BOOL noValidSelection;
    
    
    	switch ( menu ) {
    
    		case M_EDIT:
    
    			EnableMenuItem( (HMENU) lParam,M_EDIT_UNDO, !SendMessage(hEdit,EM_CANUNDO,0,0) );
    			EnableMenuItem( (HMENU) lParam,M_EDIT_PASTE, !IsClipboardFormatAvailable(CF_TEXT) );
    
    			//check if a valid selection is highlighted
    			selection = SendMessage(hEdit,EM_GETSEL,0,0);
    			noValidSelection = LOWORD(selection) == HIWORD(selection);
    
    			EnableMenuItem( (HMENU) lParam,M_EDIT_CUT, noValidSelection );
    			EnableMenuItem( (HMENU) lParam,M_EDIT_COPY, noValidSelection );
    			EnableMenuItem( (HMENU) lParam,M_EDIT_DEL, noValidSelection );
    
    			break;
    
    	} //end switch
    return;
    }
    This was how I implemented the standard edit menu commands:
    Code:
    	case M_EDIT_UNDO:
    		SendMessage(hEdit,EM_UNDO,0,0);
    		break;
    
    	case M_EDIT_CUT:
    		SendMessage(hEdit,WM_CUT,0,0);
    		break;
    
    	case M_EDIT_COPY:
    		SendMessage(hEdit,WM_COPY,0,0);
    		break;
    
    	case M_EDIT_PASTE:
    		SendMessage(hEdit,WM_PASTE,0,0);
    		break;
    
    	case M_EDIT_DEL:
    		SendMessage(hEdit,WM_CLEAR,0,0);
    		break;
    
    	case M_EDIT_SELALL:
    		SendMessage(hEdit,EM_SETSEL,0,-1);
    		break;
    Finally, this was the resource script for the menu and accelerators:
    Code:
    M_MAIN MENU
    BEGIN
        POPUP "&File"
            BEGIN
    	POPUP "&New"
    		BEGIN
    		MENUITEM "&Document", M_FILE_NEW
            	MENUITEM "&Window", M_FILE_WIN
    		END
    	MENUITEM "&Open", M_FILE_OPEN
            MENUITEM "&Save", M_FILE_SAVE
            MENUITEM "Save &As", M_FILE_SAVEAS
            MENUITEM SEPARATOR
            MENUITEM "&Refresh",  M_FILE_REFRESH
            MENUITEM SEPARATOR
            MENUITEM "E&xit", M_FILE_EXIT
            END
        POPUP "&Edit"
            BEGIN
            MENUITEM "&Undo", M_EDIT_UNDO
            MENUITEM SEPARATOR
            MENUITEM "Cu&t", M_EDIT_CUT
            MENUITEM "&Copy", M_EDIT_COPY
            MENUITEM "&Paste", M_EDIT_PASTE
            MENUITEM "De&lete", M_EDIT_DEL
            MENUITEM SEPARATOR
            MENUITEM "Select A&ll", M_EDIT_SELALL
            MENUITEM "Time/&Date   F5", M_EDIT_TIME
            MENUITEM SEPARATOR
            MENUITEM "&Word Wrap", M_EDIT_WORD
            MENUITEM "Change &Font", M_EDIT_FONT
    	END
        POPUP "&Search"
            BEGIN
            MENUITEM "&Find", M_SRCH_FND
            MENUITEM "Find &Next   F3", M_SRCH_FNDNXT
            MENUITEM SEPARATOR
            MENUITEM "&Replace", M_SRCH_RPLC
            END
    END
    ID_ACCEL ACCELERATORS
    BEGIN
        VK_F5, M_EDIT_TIME, VIRTKEY,
        VK_F3, M_SRCH_FNDNXT, VIRTKEY,
        83, M_FILE_SAVE, VIRTKEY, CONTROL
        78, M_FILE_NEW, VIRTKEY, CONTROL
        65, M_EDIT_SELALL, VIRTKEY, CONTROL
        79, M_FILE_OPEN, VIRTKEY, CONTROL
        70, M_SRCH_FND, VIRTKEY, CONTROL
    END

  5. #5
    Chad Johnson
    Join Date
    May 2004
    Posts
    154
    hey cool thanks. Yea that will help. I never thought of using a switch statement instead of separate functions either.

    One thing - what do those SendMessage statements do exactly?

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    288
    They add a message to the message queue of the target HWND...

    are you asking what WM_COPY and WM_PASTE, etc.. do? or what SendMessage(...) does?

  7. #7
    Chad Johnson
    Join Date
    May 2004
    Posts
    154
    both

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Obtaining a value from a text box
    By thetinman in forum Windows Programming
    Replies: 1
    Last Post: 11-23-2006, 04:50 PM
  2. Automatically enter text in a dialog box
    By leojose in forum Windows Programming
    Replies: 6
    Last Post: 12-13-2005, 10:59 AM
  3. Dynamically add statis text to a dialog box
    By earth_angel in forum Windows Programming
    Replies: 8
    Last Post: 06-23-2005, 01:28 PM
  4. Sending text to an edit box
    By ColdFire in forum Windows Programming
    Replies: 1
    Last Post: 09-24-2002, 07:46 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21