如何在MFC CRichEditCtrl中处理CTRL V事件

huangapple go评论52阅读模式
英文:

How to handle CTRL V event in MFC CRichEditCtrl

问题

我已经创建了一个示例的MFC应用程序,其中只包含一个富文本编辑控件。
我想要处理这个编辑控件的CTRL V(键盘粘贴)事件。
当我右键单击并检查属性中的事件时,我看不到任何可以处理CTRL V的事件。
我尝试过ENM_PROTECTED但没有运气。

英文:

I have created sample MFC application with only one rich edit control in it.
I want to handle CTRL V (keyboard paste) event for this edit control.
when I right click and check events in property, I don't see any event which can handle CTRL V.
I tried ENM_PROTECTED but no luck.

答案1

得分: 0

这个控件会自己处理Ctrl+V,所以你需要更精确地说明:你想要处理什么?

如果你想为粘贴操作引入自己的代码,你可以在视图或对话框中添加一个PreTranslateMessage处理程序。你可以在那里处理所有键盘消息,然后再将其传递给控件。

英文:

I remember that this controls handles Ctrl+V by itself. So you have to be more precise: What do you want to handle?

If you want to introduce your own code for the Paste you may add a PreTranslateMessage Handler to the View or Dialog. You can handle all Keyboard message there before it is handed out to the controls.

答案2

得分: 0

谢谢您的建议,xMRi。
我已经尝试过了,它有效,但仍然没有解决我的确切目的。
下面是捕获CTRL-V消息的代码。

BOOL myRichEditCtrl::PreTranslateMessage(MSG* pMsg)
{
    if (GetKeyState(VK_CONTROL) & 0x8000 &&
        GetKeyState('V') & 0x8000 &&
        GetFocus() == this)
    {
        // 在此处处理消息(键盘输入?)
    } 
    return CRichEditCtrl::PreTranslateMessage(pMsg);
}
英文:

Thank you for the suggestion, xMRi.
I have tried it, and it works but still does not solve my exact purpose.
Below is the code that captures CTRL-V messages.

BOOL myRichEditCtrl::PreTranslateMessage(MSG* pMsg)
{
    if (GetKeyState(VK_CONTROL) & 0x8000 &&
        GetKeyState('V') & 0x8000 &&
        GetFocus() == this)
        {
	      //process messages here (keys ??) 
        } 
        return CRichEditCtrl::PreTranslateMessage(pMsg);
}

答案3

得分: 0

你可以使用Accelerators来处理Ctrl-V按键,这可以阻止默认的粘贴行为,允许你将自己选择的文本粘贴到Rich Edit控件中。

以下是必要的步骤:

在resource.h文件中添加:

#define IDR_ACCELERATOR1                156
#define ID_RICH_CTRL_V                  1001

在.rc文件中添加:

IDR_ACCELERATOR1 ACCELERATORS
BEGIN
    "V",            ID_RICH_CTRL_V,         VIRTKEY, CONTROL
END

在对话框头文件中添加:

HACCEL m_hAccel;
afx_msg void OnRichCtrlV();
virtual BOOL PreTranslateMessage(MSG* pMsg);

在对话框初始化中添加:

BOOL CMFCApplication4Dlg::OnInitDialog()
{
    m_hAccel = LoadAccelerators(AfxGetInstanceHandle(),
        MAKEINTRESOURCE(IDR_ACCELERATOR1));
    ...

BEGIN_MESSAGE_MAP中添加:

ON_COMMAND(ID_RICH_CTRL_V, &CMFCApplication4Dlg::OnRichCtrlV)

以及相关的方法:

void CMFCApplication4Dlg::OnRichCtrlV()
{
    // 处理Ctrl V按键的操作

    CWnd *hwnd = GetDlgItem(IDC_RICHEDIT21);
    hwnd->SetWindowTextW(L"在这里覆盖粘贴的文本");
}

添加以下的PreTranslateMessage:

BOOL CMFCApplication4Dlg::PreTranslateMessage(MSG* pMsg)
{
    if (m_hAccel)
    {
        if (::TranslateAccelerator(m_hWnd, m_hAccel, pMsg))
        {
            return(TRUE);
        }
    }
    
    return CDialog::PreTranslateMessage(pMsg);
}
英文:

You can handle Ctrl-V keypress using Accelerators, this prevents the default paste behaviour and allows you to paste text of your choice into the Rich Edit Control instead.

The following code was tested with an MFC single dialog app with the name CMFCApplication4Dlg, but this should give you everything required to get it working in your application.

Here are the steps required below:

In resource.h add:

#define IDR_ACCELERATOR1                156
#define ID_RICH_CTRL_V                  1001

In .rc file add:

/////////////////////////////////////////////////////////////////////
//
// Accelerator
//

IDR_ACCELERATOR1 ACCELERATORS
BEGIN
    "V",            ID_RICH_CTRL_V,         VIRTKEY, CONTROL
END

In your dialog header add:

HACCEL m_hAccel;
afx_msg void OnRichCtrlV();
virtual BOOL PreTranslateMessage(MSG* pMsg);

In your dialog initilization add:

BOOL CMFCApplication4Dlg::OnInitDialog()
{
    m_hAccel = LoadAccelerators(AfxGetInstanceHandle(),
	    MAKEINTRESOURCE(IDR_ACCELERATOR1));
...

Add to the following to your BEGIN_MESSAGE_MAP:

ON_COMMAND(ID_RICH_CTRL_V, &CMFCApplication4Dlg::OnRichCtrlV)

And the associated method:

void CMFCApplication4Dlg::OnRichCtrlV()
{
    // Ctrl V was pressed handle here

	CWnd *hwnd = GetDlgItem(IDC_RICHEDIT21);
    hwnd->SetWindowTextW(L"Overide Pasted Text Here");
}

Add the following PreTranslateMessage:

BOOL CMFCApplication4Dlg::PreTranslateMessage(MSG* pMsg)
{
    if (m_hAccel)
    {
	    if (::TranslateAccelerator(m_hWnd, m_hAccel, pMsg))
	    {
		    return(TRUE);
	    }
    }

	return CDialog::PreTranslateMessage(pMsg);
}

答案4

得分: -1

在经过长时间在Google上的搜索后,我终于找到了一段代码。

MFC的CRichEditCtrl不允许用户直接覆盖OnKeyDown函数。这是一个受保护的成员,所以我必须创建自己的CRichEditCtrl,它继承自CRichEditCtrl,然后覆盖OnKeyDown函数。但是,我仍然无法处理CTRL+V事件。

class myRichEditCtrl : public CRichEditCtrl //继承自CWnd
{
  //覆盖下面的函数以处理按键按下事件
  //这个函数是CWnd的一部分
  afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
}

void myRichEditCtrl::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
   //在这里处理按键
   if ((nChar == 'v' || nChar == 'V') && (GetKeyState(VK_CONTROL) < 0))
   {
      // 仍然无法工作,当我按下ctrl+V时,nChar的值是17,为什么?
   }
   
   //如果需要,调用默认的处理
   CRichEditCtrl::OnKeyDown(nChar, nRepCnt, nFlags);
}
英文:

After spending hours on Google, I finally found a piece of code.

MFC Cricheditctrl does not let the user override the OnKeyDown function directly. 
It is a protected member, so I have to create my own CrichEditCtrl, which inherits from Cricheditctrl, and then override OnKeyDown.
but I am still unable to handle CTRL V event.

class myRichEditCtrl : public CRichEditCtrl //derived from CWnd
{
  //override below function to handle key down event
  // this function is part of CWnd 
  afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
}
 
void myRichEditCtrl::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
   //process keys here
   if ( (nChar == &#39;v&#39; ||  nChar == &#39;V&#39;) &amp;&amp; (GetKeyState(VK_CONTROL) &lt;0))
   {
      // still not working nChar value is 17 when I press ctrl V, Why ??
   }
   
   //call default if needed
    CRichEditCtrl::OnKeyDown(nChar, nRepCnt, nFlags);
}

huangapple
  • 本文由 发表于 2023年2月24日 14:22:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75553210.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定