如何把 CMyListView 取自 CMyListCtrl 。

解决方案 »

  1.   

    还有一个方法;
    得到此控件的窗口句柄(CWnd *),然后强制转换为(CMyListCtrl *);
      

  2.   

    在这个类中调用:
    GetListCtrl()就会得到指向CListCtrl的指针。
      

  3.   

    GetListCtrl() 是引用不是得到指针。to  sizhi(街机时代) 如果CMyListCtrl中分配了数据那么不是产生错误吗?我的意思是CMyListView 中的ListCtrl(窗口) 由CMyListCtrl产生。
      

  4.   

    你的主窗口是CMyListView你当然可以用类的方法GetListCtrl()得到引用了(实际上就是对控件的引用),但是假如我用CMyListView创建了个MultipleDoctemplate的话,我又想在其中放入一个treeCtrl,对其操作需要CTreeCtrl & 此时就只有直接获取了,这很常见“如果CMyListCtrl中分配了数据那么不是产生错误吗?”
    说具体点,行吗?
      

  5.   

    "我的意思是CMyListView 中的ListCtrl(窗口) 由CMyListCtrl产生。"
    这是什么意思?
      

  6.   

    明确的说就是:在Dialog中我们可以使用继承于ListCtrl的控件。在文档视图结果中我如何使用继承于ListCtrl呢?
      

  7.   

    响应View的OnCreate直接建立呀CMyListCtrl myctrl;
    myctrl.Create(LVS_REPORT,&rect,this, IDC_MYID);
      

  8.   

    我明白了,不过恐怕不行,因为CListView的CListCtrl就是它自己,GetListCtrl的内部实现是这样的return *(CListCtrl*)this;
      

  9.   

    不知道有没有CListCtrl的数据直接拷贝的函数,CListView可以直接当一个CListCtrl用的,呵呵,好有意思!
      

  10.   

    CListCtrl &r= GetListCtrl();
      

  11.   

    在你的ListView中可以用GetListCtrl取得ListCtrl的控制变量
      

  12.   

    我想我明白你的意思了,你是想象在Dialog中可以使用派生控件一样将CListView中的CListCtrl用派生类替代?在Dialog中可以使用CMyListCtrl.SubclassDlgItem( IDC_LIST1, this) 语句将控件子类化,不过这种方法对于CListView行不通。因为CListView里的CListCtrl并不是从属于View,而就是View本身!所以在文档视图中如果要自定义新的ListCtrl特性,你应该在你自己的View类里边直接定义,事实上和派生CListCtrl差不多的。
      

  13.   

    需要响应的消息和需要重载的函数都是一样的。
    这进一步说明了CListView实际上在消息响应方面就是用的CListCtrl的消息处理。
      

  14.   

    你把文档基于CListview,再在视类中用
    CListCtrl& m_Listview = (CListCtrl&) GetListCtrl();不是可以了。
    下面是一个对该CListCtrl的显示操作:
    CDaoFieldInfo fieldInfo;
    int nFields;
    CDaoTableDef td(m_pDatabase);
    try
    {
    td.Open(m_strTableName);
    nFields=td.GetFieldCount();
    for(int j=0;j<nFields;j++){
    td.GetFieldInfo(j,fieldInfo);
    int nWidth=m_Listview.GetStringWidth(fieldInfo.m_strName)+30;
    m_Listview.InsertColumn(j,fieldInfo.m_strName,LVCFMT_LEFT,nWidth);
    }
    }
    catch(CDaoException* e)
    {
    e->ReportError();
    e->Delete();
    return;
    }
    td.Close();
    //读取表数据
    int nItem=0;
    m_pImageList = new CImageList();
    m_pImageList->Create(IDB_IMAGELIST, 16, 1, RGB(0,0,0));
    try
    {
    CString strSelect=(_T("Select * From["));
    strSelect+=m_strTableName;
    strSelect+=_T("]");
    m_pRecordset->Open(dbOpenDynaset,strSelect);
    while(!m_pRecordset->IsEOF()){
    COleVariant var;
    var=m_pRecordset->GetFieldValue(0);
    m_Listview.InsertItem(nItem,CCrack::strVARIANT(var));
    for(int i=0;i<nFields;i++){
    m_Listview.SetImageList(m_pImageList, LVSIL_SMALL);
    var=m_pRecordset->GetFieldValue(i);
    m_Listview.SetItemText(nItem,i,CCrack::strVARIANT(var));
    }
    nItem++;
    m_pRecordset->MoveNext();
    }
    我的PreCreateWindow是这样:
    BOOL CJwglView::PreCreateWindow(CREATESTRUCT& cs)
    {
    cs.style |= LVS_REPORT;
    return CListView::PreCreateWindow(cs);
    }
      

  15.   

    这里for语句改成这样会更好。
    m_Listview.SetImageList(m_pImageList, LVSIL_SMALL);
    for(int i=1;i<nFields;i++){ var=m_pRecordset->GetFieldValue(i);
    m_Listview.SetItemText(nItem,i,CCrack::strVARIANT(var));
    }
      

  16.   

    一个笨办法是:CMyListView从CView中继承,在CMyListView::OnCreate(...)创建,在CMyListView::OnSize()中平铺
      

  17.   

    CFormView为基类
    CListCtrl m_list=GetListCtrl();
      

  18.   

    http://msdn.microsoft.com/msdnmag/issues/01/11/c/default.aspxMSDN Magazine 2001C++ Q&A Understanding CControlView, Changing Scroll Bar Color in MFC Apps 
     
    Paul DiLascia 
    Download the code for this article: CQA0111.exe (201KB)
     

    I made a custom control derived from CWnd, and now I want to use it as a view. My first solution was to embed the control into the view and handle OnSize in the view to position the control over the client area. The problem is that mouse messages go to the control and cannot be overridden in the view. The keystroke messages go to the view and must be manually forwarded to the control.
          I read about CCtrlView as a base class for common controls. I've even managed to write the view around it (I believe that you wrote about this in an issue of MSJ), but I could not get it to work with my CWnd-based control. Can this be done, and how? 
    A ......