Hello dear forum friends.
I know that this subject has been dealt with before. I read the threads about this issue in this forum however I still couldn't make it work. So I'm bringing this up again and hopefully you'll find it in your hearts to forgive and help me. :)
I've made a simple dialog (MFC) which contains 2 edit boxes 2 buttons which I've made and the 2 automated buttons OK and Cancel.
What I would like is that when write text in the first edit box and hit ENTER the dialog would push the first button that I created.
I understand that in order to capture the ENTER with the edit box control I need to make a derived class of CEdit and there process the messages. Here's my code:
CMyEdit.h
#include "afxwin.h"
class CMyEdit : public CEdit
{
DECLARE_DYNAMIC(CMyEdit);
protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg UINT OnGetDlgCode();
afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);
afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
BOOL PreTranslateMessage( MSG* pMsg );
};
CMyEdit.cpp
#include "stdafx.h"
#include "CMyEdit.h"
IMPLEMENT_DYNAMIC(CMyEdit, CEdit)
BEGIN_MESSAGE_MAP(CMyEdit, CEdit)
ON_WM_GETDLGCODE()
ON_WM_CHAR()
ON_WM_KEYDOWN()
END_MESSAGE_MAP()
UINT CMyEdit::OnGetDlgCode()
{
// Assure we will receive Enter key
return CEdit::OnGetDlgCode() | DLGC_WANTALLKEYS;
}
void CMyEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
int i=0;
i++;
if (nChar == '\r')
AfxMessageBox(_T("Got Enter"));
CEdit::OnChar(nChar, nRepCnt, nFlags);
}
void CMyEdit::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
int i=0;
i++;
if (nChar == VK_RETURN)
AfxMessageBox(_T("Got Enter"));
CEdit::OnKeyDown(nChar, nRepCnt, nFlags);
}
BOOL CMyEdit::PreTranslateMessage(MSG *pMsg)
{
if (pMsg->message == WM_KEYDOWN)
{
if (pMsg->wParam == VK_RETURN)
AfxMessageBox(_T("Got Enter"));
}
return CEdit::PreTranslateMessage(pMsg);
}
For simplicity I'll settle for showing a message when ENTER is pressed.
As you can see I've tried to capture both OnChar and KeyDown messages. I've also tried the PreTranslate() function.
But it doesn't work!!
Is there something in the main dialog code that I'm forgetting?
main header:
#pragma once
#include "afxwin.h"
#include "CMyEdit.h"
// Ctry_ENTERDlg dialog
class Ctry_ENTERDlg : public CDialog
{
// Construction
public:
Ctry_ENTERDlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
enum { IDD = IDD_TRY_ENTER_DIALOG };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
afx_msg void OnOK();
// Implementation
protected:
HICON m_hIcon;
// Generated message map functions
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
private:
CMyEdit m_eFirst;
CEdit m_eSecond;
CButton m_bFirst;
CButton m_bSecond;
};
Thank you,
Gadi.