怎么向CListView中动态插入项?我在CListView主框架弹出对话框,在对话框中输入CListView每个子项的内容,一旦点击对话框中的“添加”按钮时,CListView能立即显示刚才增加的项,如何实现?我的CListView初始化代码如下:
void CTestView::OnInitialUpdate()
{
CListCtrl& theCtrl = GetListCtrl();
theCtrl.InsertColumn(0, _T("信息标题"), LVCFMT_LEFT);
theCtrl.InsertColumn(1, _T("信息类型"), LVCFMT_LEFT);
theCtrl.InsertColumn(2, _T("时间"), LVCFMT_LEFT);
theCtrl.InsertColumn(3, _T("信息明细"), LVCFMT_LEFT);
theCtrl.SetExtendedStyle(theCtrl.GetExtendedStyle() | LVS_EX_FULLROWSELECT);
SetWindowLong(theCtrl.m_hWnd, GWL_STYLE, GetWindowLong(theCtrl.m_hWnd, GWL_STYLE) | LVS_REPORT); }
我写了一个如下的函数:
void CTestView::OnUpdate(CString strMsg)
{
CTime dtDatetime ;
CListCtrl& theCtrl = GetListCtrl();
dtDatetime = dtDatetime.GetCurrentTime(); // 格式化时间格式
theCtrl.InsertItem(0, strMsg); // 信息标题。第三个参数为信息类型索引号
theCtrl.SetItemText(0, 1, strMsg); // 信息类型
theCtrl.SetItemText(0, 2, dtDatetime.Format("%Y-%m-%d %H:%M:%S")); // 日期时间
theCtrl.SetItemText(0, 3, strMsg); // 信息主体
Invalidate(); }
在对话框“添加”按钮中通过CTestView的指针pTestView来调用这个函数即
pTestView->OnUpdate("fghfghfg");来向CTestView中添加项,可是添加不上啊,有哪位高人能帮我实现这个功能啊?谢谢了
void CTestView::OnInitialUpdate()
{
CListCtrl& theCtrl = GetListCtrl();
theCtrl.InsertColumn(0, _T("信息标题"), LVCFMT_LEFT);
theCtrl.InsertColumn(1, _T("信息类型"), LVCFMT_LEFT);
theCtrl.InsertColumn(2, _T("时间"), LVCFMT_LEFT);
theCtrl.InsertColumn(3, _T("信息明细"), LVCFMT_LEFT);
theCtrl.SetExtendedStyle(theCtrl.GetExtendedStyle() | LVS_EX_FULLROWSELECT);
SetWindowLong(theCtrl.m_hWnd, GWL_STYLE, GetWindowLong(theCtrl.m_hWnd, GWL_STYLE) | LVS_REPORT); }
我写了一个如下的函数:
void CTestView::OnUpdate(CString strMsg)
{
CTime dtDatetime ;
CListCtrl& theCtrl = GetListCtrl();
dtDatetime = dtDatetime.GetCurrentTime(); // 格式化时间格式
theCtrl.InsertItem(0, strMsg); // 信息标题。第三个参数为信息类型索引号
theCtrl.SetItemText(0, 1, strMsg); // 信息类型
theCtrl.SetItemText(0, 2, dtDatetime.Format("%Y-%m-%d %H:%M:%S")); // 日期时间
theCtrl.SetItemText(0, 3, strMsg); // 信息主体
Invalidate(); }
在对话框“添加”按钮中通过CTestView的指针pTestView来调用这个函数即
pTestView->OnUpdate("fghfghfg");来向CTestView中添加项,可是添加不上啊,有哪位高人能帮我实现这个功能啊?谢谢了
指针获得如下:
CTestApp *pApp=(CTestApp *)AfxGetApp();
pApp->pView->OnUpdate(CGoalVar::strMsgSend);
程序走到
void CTestView::OnUpdate(CString strMsg)
{
.........................// 其它代码省略 theCtrl.InsertItem(0, strMsg); //程序走到此处中断
.......................// 其它代码省略
}
pApp->pView->OnUpdate(CGoalVar::strMsgSend);
来向CTestView中添加项,程序走到pApp->pView->OnUpdate(CGoalVar::strMsgSend); 产生中断,
CTestApp *pApp=(CTestApp *)AfxGetApp();
pApp->pView
{
.........................// 其它代码省略 theCtrl.InsertItem(0, strMsg); //程序走到此处中断
.......................// 其它代码省略
}
函数里的 theCtrl.InsertItem(0, strMsg); 这一句引起的,这里不能用InsertItem吗?中断时中断箭头指向如下代码:
AFXCMN_INLINE BOOL CListCtrl::GetItem(LVITEM* pItem) const
{ ASSERT(::IsWindow(m_hWnd)); return (BOOL) ::SendMessage(m_hWnd, LVM_GETITEM, 0, (LPARAM)pItem); }
付:
//////////////////////////////////////
void CTestView::OnUpdate(CString strMsg)函数如下:
void CTestView::OnUpdate(CString strMsg)
{
CTime dtDatetime ;
CListCtrl& theCtrl = GetListCtrl();
dtDatetime = dtDatetime.GetCurrentTime(); // 格式化时间格式
theCtrl.InsertItem(0, strMsg); // 信息标题。第三个参数为信息类型索引号
theCtrl.SetItemText(0, 1, strMsg); // 信息类型
theCtrl.SetItemText(0, 2, dtDatetime.Format("%Y-%m-%d %H:%M:%S")); // 日期时间
theCtrl.SetItemText(0, 3, strMsg); // 信息主体
Invalidate(); }
// 视图中:
void CTestView::OnDlg()
{
CTestDlg dlg(this); // 对话框的构造函数就可以传递一个父窗口指针,将视图类对象作为对话框的父窗口是最自然和方便的事情
dlg.DoModal();
}// 对话框中:
void CTestDlg::OnBnClickedAdd()
{
CTestView* pView = reinterpret_cast<CTestView*>(GetParent());
ASSERT(pView);
if ( NULL == pView )
{
AfxMessageBox( _T("没有父窗口") );
return;
}
pView->InsertItem(...) //等等随便操作视图
}
LVITEM lvi; int item = ListCtrl.GetItemCount(); lvi.mask = LVIF_TEXT;
lvi.iItem = item; lvi.iSubItem = 0;
//lvi.pszText = LPWSTR(line->Col[0].GetBuffer());
CString id;
id.Format(_T("%d"),ListCtrl.GetItemCount() + 1);
lvi.pszText = LPWSTR(id.GetBuffer(0));
ListCtrl.InsertItem(&lvi); int i;
for (i=1; i<MSG_COL_SIZE; i++)
{
lvi.iSubItem = i;
lvi.pszText = LPWSTR(line->Col[i].GetBuffer());
ListCtrl.SetItem(&lvi);
}