一个单文挡多视图问题 CUpView2,CFileInfoView都是继承CListView,现在要实现点击
CUpView2中一Item,就能把该Item中所有内容显示在CFileInfoView上。我的问题是不知道如何得到Item的内容,然后通过SendMessage(,,XXX)传递。XXX应该怎么实现!以下是部分程序!请帮忙!void CUpView2::OnClick(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
CListCtrl* pList=&GetListCtrl();
POSITION pos = pList->GetFirstSelectedItemPosition();
if (pos == NULL)
TRACE0("No items were selected!\n");
else
{
while (pos)
{
int nItem = pList->GetNextSelectedItem(pos);
TRACE1("Item %d was selected!\n", nItem);
// you could do your own processing on nItem here
//##########################
需要插入代码
//########################## CMainFrame* pMain = (CMainFrame *) AfxGetMainWnd();
CFileInfoView* pView = (CFileInfoView *)(pMain->m_wndSplitter2.GetPane(1,0)); pView->SendMessage(WM_MYMESSAGE,0,NULL);//NULL需要修改 }
} *pResult = 0;
}
CUpView2中一Item,就能把该Item中所有内容显示在CFileInfoView上。我的问题是不知道如何得到Item的内容,然后通过SendMessage(,,XXX)传递。XXX应该怎么实现!以下是部分程序!请帮忙!void CUpView2::OnClick(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
CListCtrl* pList=&GetListCtrl();
POSITION pos = pList->GetFirstSelectedItemPosition();
if (pos == NULL)
TRACE0("No items were selected!\n");
else
{
while (pos)
{
int nItem = pList->GetNextSelectedItem(pos);
TRACE1("Item %d was selected!\n", nItem);
// you could do your own processing on nItem here
//##########################
需要插入代码
//########################## CMainFrame* pMain = (CMainFrame *) AfxGetMainWnd();
CFileInfoView* pView = (CFileInfoView *)(pMain->m_wndSplitter2.GetPane(1,0)); pView->SendMessage(WM_MYMESSAGE,0,NULL);//NULL需要修改 }
} *pResult = 0;
}
再把获得DWORD数据作为WPARAM发送出去。
方法二: 同样还是保存结构数组(或者链表),并且把链表,或数组的每个元素的首地址用SetItemData() 保存在每个Item里面,然后点击后取了Index 以后取 ItemData, 取出来直接用它的首地址访问该结构。
另外,如果要对CListCtrl对象进行排序操作,必须保证每个Item有一个唯一的ItemData。因为MFC在对CListCtrl排序的时候需要用ItemData来定位每个Item。
typedef struct tagItemData
{
int type;
CString filename;
DWORD filesize;
int bl;
DWORD speed;
int threadnum;
CString url;
BOOL suc;
CString intro;
float dwtime;
}ItemData;
出现的问题
以下是部分代码
void CUpView2::OnClick(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
CListCtrl* pList=&GetListCtrl();
POSITION pos = pList->GetFirstSelectedItemPosition();
if (pos == NULL)
TRACE0("No items were selected!\n");
else
{
while (pos)
{
int nItem = pList->GetNextSelectedItem(pos);
TRACE1("Item %d was selected!\n", nItem);
// you could do your own processing on nItem here
CMainFrame* pMain = (CMainFrame *) AfxGetMainWnd();
CFileInfoView* pView = (CFileInfoView *)(pMain->m_wndSplitter2.GetPane(1,0));
//MessageBox("send message");
pView->SendMessage(WM_MYMESSAGE,nItem,NULL);
}
} *pResult = 0;
}
{
//保存结构数组
pItem[iItem]->filename=localfile;
pItem[iItem]->filesize=filesize;
pItem[iItem]->speed=speed;
pItem[iItem]->url=url;
pItem[iItem]->suc=suc;
pItem[iItem]->intro=intro;
pItem[iItem]->dwtime=dwtime;
pItem[iItem]->sResume=sResume;
MessageBox("save succeed");
DWORD dwData=(DWORD)pItem[iItem];
GetListCtrl().SetItemData(iItem,dwData);
}以下是接收消息的那个视图类函数void CFileInfoView::OnShow(WPARAM wParam, LPARAM lParam)
{ CMainFrame* pMain = (CMainFrame *) AfxGetMainWnd();
CUpView2* pView = (CUpView2 *)(pMain->m_wndSplitter2.GetPane(0,0));
CListCtrl* pList=&GetListCtrl();
DWORD dwData=pList->GetItemData(wParam);
MyItemData *pItem;
pItem=(MyItemData *)dwData;
if(pItem->suc)
{
MessageBox("0");
CString str;
str.Empty();
str=pItem->url;
GetListCtrl().SetItemText(0,1,str);
str.Empty();
str=pItem->filename;
GetListCtrl().SetItemText(1,1,str);
str.Empty();
str.Format("%d Byte",pItem->filesize);
GetListCtrl().SetItemText(4,1,str);
if(pItem->sResume)
GetListCtrl().SetItemText(5,1,"支持");
else
GetListCtrl().SetItemText(5,1,"不支持"); str.Empty();
str=pItem->intro;
GetListCtrl().SetItemText(6,1,str); str.Empty();
str.Format("%d s",pItem->dwtime*0.25);
GetListCtrl().SetItemText(7,1,str);
str.Empty();
str.Format("%d KB/s",pItem->speed*4/1000);
GetListCtrl().SetItemText(8,1,str);
}}
结构定义如下typedef struct tagItemData
{
CString filename;
DWORD filesize;
DWORD speed;
CString url;
BOOL suc;
CString intro;
float dwtime;
BOOL sResume;
}MyItemData;
能不能说明一下你的 SaveItem 在哪里调用的呢? 能看看你调用SaveItem 的代码吗? 偶估计你可能没有保存数据, 或者数据保存以后生存期太短,已经释放了。
指针对象的有效性可以用IsBadCodPtr()检查。
{
CListCtrl* pListCtrl=&GetListCtrl();
int iItem = pListCtrl->GetNextItem(0,LVNI_BELOW);
//添加状态
switch(type)
{
case 0:
pListCtrl->SetItemText(iItem,0,"未开始");
break;
case 1:
pListCtrl->SetItemText(iItem,0,"下载中");
break;
case 2:
pListCtrl->SetItemText(iItem,0,"已完成");
// ##########################
SaveItem(iItem,localfile,filesize,speed,url,suc,intro,dwtime,sResume);
// ##########################
break;
case 3:
pListCtrl->SetItemText(iItem,0,"未完成");
}
//添加文件名
pListCtrl->SetItemText(iItem,1,filename);
//添加文件大小
CString str;
str.Empty();
str.Format("%d KB",filesize/1000);
pListCtrl->SetItemText(iItem,2,str);
//添加已下文件比率
str.Empty();
str.Format("%d ",bl*100);
pListCtrl->SetItemText(iItem,3,str);
//添加下载速率
str.Empty();
str.Format("%d KBps" ,speed*4/1000);
pListCtrl->SetItemText(iItem,4,str);
//添加已用时
str.Empty();
str.Format("%d s",dwtime*0.25);
pListCtrl->SetItemText(iItem,5,str);
//添加剩余时间
//添加线程数
str.Empty();
str.Format("%d",threadnum);
pListCtrl->SetItemText(iItem,7,str);
//添加目标url
pListCtrl->SetItemText(iItem,8,url);
//添加有无错误
if(suc !=TRUE)
pListCtrl->SetItemText(iItem,9,"出错");
else
pListCtrl->SetItemText(iItem,9,"");
//添加注释
pListCtrl->SetItemText(iItem,10,intro);
}