以扫码枪回车作为区分, 下次输入时自动清除
// MyEdit.h : header file
#pragma once
#define WM_MYINPUT_MSG (WM_USER + 1000)
/
// CMyEdit window
class CMyEdit : public CEdit
{
DECLARE_DYNAMIC(CMyEdit)
// Construction
public:
CMyEdit();
virtual ~CMyEdit();
public:
BOOL m_bPostMessageMode; //消息模式?
// Attributes
protected:
BOOL m_bAutoClearFlag; //自动清除标记
protected:
virtual BOOL PreTranslateMessage(MSG* pMsg);
};
// MyEdit.cpp : implementation file
//
#include "stdafx.h"
#include "MyEdit.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/
// CMyEdit
IMPLEMENT_DYNAMIC(CMyEdit, CEdit)
CMyEdit::CMyEdit()
{
m_bAutoClearFlag = TRUE;
//消息模式?
//消息模式时,回车时将给父窗口发送WM_MYINPUT_MSG消息
//非消息模式,回车时父窗口将收到IDOK点击消息
m_bPostMessageMode = TRUE;
}
CMyEdit::~CMyEdit()
{
}
/
// CMyEdit message handlers
BOOL CMyEdit::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
if(pMsg->message == WM_KEYDOWN)
{
if(m_bAutoClearFlag) //需要清除?
{
SetWindowText(_T(""));
m_bAutoClearFlag = FALSE;
}
if(pMsg->wParam == VK_RETURN) //回车
{
m_bAutoClearFlag = TRUE; //标记为下次清除
if(m_bPostMessageMode) //消息模式
{
//检查输入非空时发送消息给父窗口
CString szInput;
GetWindowText(szInput);
CWnd *pParent = GetParent();
if(pParent && (!szInput.IsEmpty()))
{
WPARAM wParam = GetDlgCtrlID(); //控件ID
LPARAM lParam = (LPARAM)m_hWnd; //控件窗口句柄
pParent->PostMessage(WM_MYINPUT_MSG, wParam, lParam);
}
//阻止继续生成IDOK消息
return TRUE;
}
}
}
return CEdit::PreTranslateMessage(pMsg);
}
测试用例
#include "MyEdit.h"
class CDlgTest10Dlg : public CDialog
{
protected:
CMyEdit m_MyEdit;
virtual void OnOK();
virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam);
……
//子类化控件
void CDlgTest10Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_EDIT1, m_MyEdit);
}
//处理IDOK
void CDlgTest10Dlg::OnOK()
{
CString szInput;
m_MyEdit.GetWindowText(szInput);
AfxMessageBox(szInput);
//CDialog::OnOK();
}
//消息方式接收数据
LRESULT CDlgTest10Dlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
// TODO: Add your specialized code here and/or call the base class
if(message == WM_MYINPUT_MSG)
{
UINT uID = (UINT)wParam;
HWND hEdit = (HWND)lParam;
CString szInput;
::GetWindowText(hEdit, szInput.GetBuffer(1024), 1024);
szInput.ReleaseBuffer();
TRACE(_T("%u = %s\n"), uID, (LPCTSTR)szInput);
}
return CDialog::WindowProc(message, wParam, lParam);
}