How to add Message for dynamically created control(CListCtrl)
-
I have created CListCtrl using Create()function. But I dont know to add Message like CLICK, DBILCLK for created control. Friends Kindly help me. Regards, S.Shanmugaraja
-
I have created CListCtrl using Create()function. But I dont know to add Message like CLICK, DBILCLK for created control. Friends Kindly help me. Regards, S.Shanmugaraja
-
Every dynamically created control have an ID, so use that ID and map your event like wizzard do for normal CListCtrl control ...
Ok, let's do some code sample :
// MyClass.h
#define IDC_LIST 1001CListCtrl m_List;
protected:
//{{AFX_MSG(CMyClass)
afx_msg void OnClickList(NMHDR* pNMHDR, LRESULT* pResult);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()// MyClass.cpp
BEGIN_MESSAGE_MAP(CMyClass, CView)
//{{AFX_MSG_MAP(CMyClass)
ON_NOTIFY(NM_CLICK, IDC_LIST, OnClickList)
//}}AFX_MSG_MAPint CMyClass::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if(CView::OnCreate(lpCreateStruct) == -1)return -1;
// TODO: Add your specialized creation code here
DWORD dwStyle = WS_CHILD | WS_VISIBLE | WS_TABSTOP | CS_DBLCLKS | LVS_REPORT;
BOOL bResult = m_List.Create(dwStyle,CRect(0,0,0,0),this,IDC_LIST);
return bResult ? 0 : -1;
// return 0;
}void CMyClass::OnClickList(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
*pResult = 0;
} -
Ok, let's do some code sample :
// MyClass.h
#define IDC_LIST 1001CListCtrl m_List;
protected:
//{{AFX_MSG(CMyClass)
afx_msg void OnClickList(NMHDR* pNMHDR, LRESULT* pResult);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()// MyClass.cpp
BEGIN_MESSAGE_MAP(CMyClass, CView)
//{{AFX_MSG_MAP(CMyClass)
ON_NOTIFY(NM_CLICK, IDC_LIST, OnClickList)
//}}AFX_MSG_MAPint CMyClass::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if(CView::OnCreate(lpCreateStruct) == -1)return -1;
// TODO: Add your specialized creation code here
DWORD dwStyle = WS_CHILD | WS_VISIBLE | WS_TABSTOP | CS_DBLCLKS | LVS_REPORT;
BOOL bResult = m_List.Create(dwStyle,CRect(0,0,0,0),this,IDC_LIST);
return bResult ? 0 : -1;
// return 0;
}void CMyClass::OnClickList(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
*pResult = 0;
}Its working good. In my case control Id is dynamic generated.
For Example:
pmyListCtrl->Create( WS_CHILD|WS_VISIBLE|WS_BORDER|LVS_REPORT,CRect(10,10,300,200), this, ControlID);So how can i find controlID? and how to pass this ID into message Map?
-
Its working good. In my case control Id is dynamic generated.
For Example:
pmyListCtrl->Create( WS_CHILD|WS_VISIBLE|WS_BORDER|LVS_REPORT,CRect(10,10,300,200), this, ControlID);So how can i find controlID? and how to pass this ID into message Map?
-
Its working good. In my case control Id is dynamic generated.
For Example:
pmyListCtrl->Create( WS_CHILD|WS_VISIBLE|WS_BORDER|LVS_REPORT,CRect(10,10,300,200), this, ControlID);So how can i find controlID? and how to pass this ID into message Map?
Simply you cannot have some random IDs for your control but you can have some range of IDs that can be used dynamically, Say from 1001(IDC_MYLIST_START) to 1101 (IDC_MYLIST_END) and use any of this IDs for you list control and make sure you did not have more than one list with the same ID in a given time. For this you have to use ON_NOTIFY_RANGE macro and your message map entry will look like,
ON_NOTIFY_RANGE(NM_CLICK, IDC_MYLIST_START, IDC_MYLIST_END, OnMyListClick)
And you message handler will be,
VOID OnMyListClick(UINT id, NMHDR * pNotifyStruct, LRESULT * result)
The variable "id" will have the list control ID which has been clicked.
Do your Duty and Don't expect the Result