CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    May 1999
    Posts
    1

    how to disable paste in CEdit control

    I have been trying to disable paste in edit controls and to disable the default popup menu.
    could anyone help me please. I have to do this before tomorrow...


  2. #2
    Join Date
    May 1999
    Posts
    318

    Re: how to disable paste in CEdit control

    I try something in a small sample and it seems to work.

    Use the PreTranslate handler of the text box's window container.
    This method is the simpliest one. You can too make your own CEdit control and then make a new PreTranslate handler in the control.

    Here is a sample using the PreTranslate handler of the container window.


    BOOL CMyWindow::PreTranslateMessage(MSG* pMsg)
    {
    // If the text box have the focus ..
    if ( GetFocus()==&m_mycontrol )
    {
    // If CTRL+V or Shift+Insert
    if ( pMsg->message == WM_KEYDOWN &&
    ( (pMsg->wParam == 'V' && (GetKeyState(VK_CONTROL) & ~1) ) ||
    (pMsg->wParam == VK_INSERT && (GetKeyState(VK_SHIFT) & ~1) )
    )
    )
    {
    // Do not dispatch the message
    return TRUE;
    }
    }
    // Call the default handler
    return CDialog::PreTranslateMessage(pMsg);
    }








  3. #3
    Join Date
    May 1999
    Location
    Sydney, Australia
    Posts
    420

    Re: how to disable paste in CEdit control

    and if I right mouse click and do it via the paste command

    I think the best way is to handle the paste command itself in a subclassed edit control

    Sally


  4. #4
    Join Date
    May 1999
    Posts
    318

    Re: how to disable paste in CEdit control

    Ok you're right.

    Yes you're right.
    In this case subclass your own edit control and use the PreTranslate handler to not dispatch the WM_PASTE message.

    I don't try this so you have to test it.


    BOOL CMyEditControl::PreTranslateMessage(MSG* pMsg)
    {
    if ( pMsg->message == WM_PASTE )
    // Do not dispatch the message
    return TRUE;

    return CEdit::PreTranslateMessage(pMsg);
    }






  5. #5
    Join Date
    May 1999
    Posts
    82

    Re: how to disable paste in CEdit control

    No problem, just follow these steps!!! First you will need to create your own class derived from CEdit, lets call it CNoPasteEdit, like so...

    class CNoPasteEdit: public CEdit
    {
    public:
    CNoPasteEdit();
    ~CNoPasteEdit();
    protected:
    // This line will need to be added by hand because WM_PASTE is not available in
    // class wizard
    afx_msg void OnPaste(WPARAM wParam, LPARAM lParam);
    afx_msg void OnContextMenu(CWnd* pWnd, CPoint point);
    DECLARE_MESSAGE_MAP()
    };

    Then you will need to edit the .cpp file for this class like so

    CNoPasteEdit::CNoPasteEdit(){
    // Put any construction code here
    }

    CNoPasteEdit:~:CNoPasteEdit(){
    // Put any destruction code here
    }

    BEGIN_MESSAGE_MAP(CNoPasteEdit, CEdit)
    // This line is needed because there is no default macro for WM_PASTE messages
    // This line will also need to be added by hand
    ON_MESSAGE(WM_PASTE, OnPaste)
    ON_WM_CONTEXTMENU()
    END_MESSAGE_MAP()

    void CNoPasteEdit::OnPaste(WPARAM wParam, LPARAM lParam){
    // Put any code here you want to execute when the user right clicks on the edit
    // control. Just leave it blank to disable the menu
    }

    void CNoPasteEdit::OnContextMenu(CWnd* pWnd, CPoint point){
    // Put any code here you want to execute when the user tries to paste into the edit
    // conrtol. Just leave it blank to prevent pasting.
    }

    After creating this class you will need to go into class wizard and select your dialog class from the drop down list. Then switch over to the member variables tab, and double click the ID for the edit control you don't want to be able to paste. In the dialog that appears use whatever you want for the member name, just be sure to select CNoPasteEdit as the Variable Type. This sould be it. The edit control will no longer have a right click context menu or except pasting via Ctrl+V.


  6. #6
    Join Date
    Aug 1999
    Posts
    18

    Re: how to disable paste in CEdit control

    Is there some reason that the same code does not work to catch the WM_PASTE message inside a CRichEditCntrl? I have pasted the code into my derived CRichEditCntrl, but when I hit cntrl-v, I do not catch the WM_PASTE message in OnPaste. Any suggestions would be appreciated.


  7. #7
    Join Date
    Dec 1999
    Location
    Cincinnati, Ohio
    Posts
    195
    I need to disable cut/paste sometime and other times allow it. If I understand this all correctly, all I need to do to disable the cut/paste is to do nothing when OnPaste/OnContextMenu. What should I do to allow them?

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