Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been trying to get the answer of the same question in another question. but it didn't. May be I am not clever enough............ anyway. I have made an sample application for the expert to understand what point I am missing...

here is the code:
#include <afxwin.h>
#define ID						0x8000
#define ID_CAPTION_1			ID
#define ID2						ID_CAPTION_1+1
#define ID_STEXT_1				ID2+1
#define ID_INSERTEDIT_CTRL		ID_STEXT_1+1
#define CAPTIONWIDTH		120
#define TEXTWIDTH			200
#define HEIGHT				24
#define INSERTWINDOWCLASS L"INSERTWINDOWCLASS"
#define NEXTHEIGHT 41
class CInsertEdit :
	public CWnd
{
DECLARE_DYNAMIC(CInsertEdit)
protected:
	CString				Label;
	CStatic				sUserName;
	CEdit				UserName;
protected:
	virtual		BOOL	PreCreateWindow(CREATESTRUCT& cs);
	afx_msg		int		OnCreate(LPCREATESTRUCT lpCreateStruct);
	void				PreSubclassWindow();
	afx_msg		HBRUSH	OnCtlColor(CDC* pDC,CWnd* pWnd,UINT nCtlColor);
	afx_msg		VOID	OnPaint();
	
	afx_msg		void	GetCustomerName();
public:
	CInsertEdit();
	~CInsertEdit(void);
	BOOL				Create(CWnd* pParentWnd, const RECT &rect, UINT nID, DWORD dwStyle);
private:
	BOOL				RegisterWindowClass();
	
	DECLARE_MESSAGE_MAP()
};

class Window1:public CFrameWnd
{
	CInsertEdit *m_pInsertEdit;
public:
	Window1()
	{
		Create(NULL,L"Create Problem");
		m_pInsertEdit=new CInsertEdit();
		m_pInsertEdit->Create(this,CRect(160,0,500,500), ID_INSERTEDIT_CTRL, WS_CHILD|WS_VISIBLE|WS_BORDER|WS_TABSTOP);
	}
};
class MainWnd:public CWinApp
{
	Window1 *wnd;
public:
	BOOL InitInstance()
	{
		wnd=new Window1();
		m_pMainWnd=wnd;
		m_pMainWnd->ShowWindow(1);
		return 1;
	}
};

BEGIN_MESSAGE_MAP( CInsertEdit, CWnd)
	ON_WM_CTLCOLOR()
	ON_WM_CREATE()
	ON_WM_PAINT()
	ON_COMMAND(ID_STEXT_1,GetCustomerName)
END_MESSAGE_MAP()
IMPLEMENT_DYNAMIC(CInsertEdit, CWnd)
CInsertEdit::CInsertEdit()
{
	RegisterWindowClass();
	return;
}
BOOL CInsertEdit::RegisterWindowClass()
{
	 WNDCLASS wndcls;
    HINSTANCE hInst = AfxGetInstanceHandle();
	//FevColor=new CBrush(RGB(159,182,205));
     if (!(::GetClassInfo(hInst, INSERTWINDOWCLASS, &wndcls)))
    {
		memset(&wndcls, 0, sizeof(WNDCLASS));
        wndcls.style            = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
        wndcls.lpfnWndProc      = ::DefWindowProc;
        wndcls.cbClsExtra       = wndcls.cbWndExtra = 0;
        wndcls.hInstance        = hInst;
        wndcls.hIcon            = NULL;
        wndcls.hCursor          = AfxGetApp()->LoadStandardCursor(IDC_ARROW);
        wndcls.hbrBackground    = (HBRUSH) (0);//(HBRUSH) RGB(255,255,255);
        wndcls.lpszMenuName     = NULL;
        wndcls.lpszClassName    = INSERTWINDOWCLASS;
		wndcls.cbWndExtra		= NULL;
        if (!AfxRegisterClass(&wndcls))
        {
            AfxThrowResourceException();
            return FALSE;
        }
    }
    return TRUE;
}
CInsertEdit::~CInsertEdit(void)
{
}
BOOL CInsertEdit::PreCreateWindow(CREATESTRUCT& cs)
{	
	return CWnd::PreCreateWindow(cs);
}
void CInsertEdit::PreSubclassWindow()
{
	CWnd::PreSubclassWindow();
	try
	{
		if (GetSafeHwnd())
		{
		}
		else
		{     
         ASSERT(FALSE);
		}
	}
	catch (CException *e)
	{
		e->ReportError();
		e->Delete();
	}
}
BOOL CInsertEdit::Create(CWnd* pParentWnd, const RECT& rect, UINT nID, DWORD dwStyle)
{
	return CWnd::Create(INSERTWINDOWCLASS,L"",dwStyle,rect,pParentWnd,nID);
}
int CInsertEdit::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	Label=L"New Customer";
	int next=NEXTHEIGHT;
	
	next=next+5+HEIGHT;
	sUserName.Create(L"Customer Name : ",SS_RIGHT|WS_VISIBLE|WS_CHILD,CRect(1,next+4,CAPTIONWIDTH,next+HEIGHT),this,ID_CAPTION_1);
	UserName.Create(WS_VISIBLE|WS_CHILD|WS_BORDER,CRect(CAPTIONWIDTH+5,next,CAPTIONWIDTH+5+TEXTWIDTH,next+HEIGHT),this,ID_STEXT_1);
	next=next+5+HEIGHT;
   return 0;
}
	afx_msg HBRUSH CInsertEdit::OnCtlColor(CDC* pDC,CWnd* pWnd,UINT nCtlColor)
	{
		HBRUSH hBrush=CWnd::OnCtlColor(pDC, pWnd,nCtlColor);
		if(pWnd->GetDlgCtrlID()>ID && pWnd->GetDlgCtrlID()<=ID+6)
		{
			pDC->SetTextColor(1262987);//brown
			pDC->SetBkMode(TRANSPARENT);
		}
		return hBrush;
		
	}
	afx_msg VOID CInsertEdit::OnPaint()
	{
		RECT rc;
		CPaintDC dc(this); // device context for painting
		GetClientRect(&rc);
		//dc.Rectangle(0,0,rc.right,41);
		TEXTMETRIC tm;
		dc.FillSolidRect(0,0,rc.right,41,RGB(128,65,30));
		RECT crc;
		crc.left=0;
		crc.top=0;
		crc.bottom=40;
		crc.right=rc.right;
		dc.DrawText(Label,wcslen(Label),&crc,DT_VCENTER|DT_SINGLELINE|DT_CENTER);
	}
	afx_msg void CInsertEdit::GetCustomerName()
	{
		wchar_t cst[256];
		UserName.GetWindowTextW(cst,256);
		MessageBox(cst,L"Test",MB_OK);
	}
MainWnd mnd;


I was hopping to get an response in function GetCustomerName().

but, its not working. compiling is fine. But not getting the general command am hopping. it can be keystroke or mouse click....

Please let me know why and what to do..

Few Issues:
1. MFC application.
2. Entire code hard coded.
Posted
Updated 1-Jul-11 23:52pm
v2

ON_COMMAND(ID_STEXT_1,GetCustomerName)

Which control (menu or toolbar) is connected to this command id? From what I can see you have nothing in your program that will send this command id to your window.
 
Share this answer
 
My Bad....

I Should have use ON_EN_CHANGE macro to get change action. Finally got my solution. Thanks a lot to Richard MacCutchan, you helped me to think clearly to find my way out
 
Share this answer
 
Comments
Richard MacCutchan 3-Jul-11 3:06am    
Happy to help.
You may find this article on MFC messages useful.
http://msdn.microsoft.com/en-us/library/0812b0wa%28v=VS.100%29.aspx[^]

Inside Resource.h (usually generated by Visual C++):
#define    ID_MYCMD      100


Inside the class declaration:
afx_msg void OnMyCommand();


Inside the message map definition:
ON_COMMAND(ID_MYCMD, OnMyCommand)


In the implementation file:
void CMyClass::OnMyCommand()
{
    // handle the command

}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900