使次要的CEdit控件不要专注于输入击键 [英] Make sublcassed CEdit control not to lose focus on enter hit key

查看:93
本文介绍了使次要的CEdit控件不要专注于输入击键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我正在使用CEdit控件单行,仅在输入Enter命中时才需要处理数据,目前我已经通过使用PreTranslateMessage中的WM_KEYDOWN捕获了输入命中,并且它可以正常工作,但我也捕获了OnKillFocus事件因此,当控件失去焦点时,我可以将文本重置为我拥有的某些默认文本.

问题在于,kill focus消息总是总是先出现在带有回车键的WM_KEYDOWN消息中,当用户按下Enter键时,如何使kill focus事件发生?

谢谢

这是用户点击ENTER时我正在使用的代码,生成WM_KEYDOWN,但同时调用了OnKillFocus.

Hello

i am using a CEdit control single line inwhich i need to process the data only on enter key hit, currently i have catch the enter hit by using the WM_KEYDOWN in the PreTranslateMessage and it works but i am catching the OnKillFocus event too so when the control loses focus i can reset the text to some default text i have.

Problem is that the kill focus message always comes first that the WM_KEYDOWN message with the enter key, how can i make the kill focus event ocurr when user hits enter?

Thanks

this is the code i am using now when user hits ENTER WM_KEYDOWN gets generated but at the same time OnKillFocus is called.

BOOL CEditExt::PreTranslateMessage(MSG* pMsg)
{
    if( pMsg->message == WM_KEYDOWN && (pMsg->wParam == VK_ESCAPE || pMsg->wParam == VK_RETURN || pMsg->wParam == VK_TAB))
    {
        bool abortEdit = false;
        if(pMsg->wParam != VK_RETURN)
        {
            abortEdit = true;
        }

        EndEdit(abortEdit);
        if(abortEdit == true) return 0;
    }
        
    return CEdit::PreTranslateMessage(pMsg);
}

void CEditExt::OnKillFocus(CWnd* pNewWnd)
{
    CEdit::OnKillFocus(pNewWnd);

    EndEdit(true);
}


void CEditExt::EndEdit(bool cancelEdit)
{
	ShowWindow(SW_HIDE);

	CString text;
	GetWindowTextW(text);

	InfoEdit info;			
	info.setText(text);

	::SendMessage(  this->GetParent()->GetSafeHwnd(), M_ENDEDIT, (WPARAM)&info, cancelEdit );

	SetWindowTextW(_T(""));
}



哦,不!我刚刚发现呼叫



Oh no!, i just figured out that the call

ShowWindow(SW_HIDE);

正在调用killfocus事件,所以我只是将呼叫移至

was calling the killfocus event so i just move that call down

SetWindowTextW

并正在工作

:p

and is working now

:p

推荐答案

我不确定我是否完全理解您的问题.

但是我确实知道ShowWindow(SW_HIDE);语句将导致控件失去焦点.
尝试注释掉此行,看它是否按预期工作.
I am not entirely sure I understand your problem.

But I do known that the statement ShowWindow(SW_HIDE); will cause the control to lose its focus.
Try commenting out this line as see if it works as expected.


使用SetWindowLong函数设置ES_WANTRETURN:
编辑控件样式 [
Set ES_WANTRETURN using the SetWindowLong function:
Edit Control Styles[^]

Best regards
Espen Harlinn


这篇关于使次要的CEdit控件不要专注于输入击键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆