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();
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);
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);
pDC->SetBkMode(TRANSPARENT);
}
return hBrush;
}
afx_msg VOID CInsertEdit::OnPaint()
{
RECT rc;
CPaintDC dc(this);
GetClientRect(&rc);
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.