CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Feb 2005
    Location
    Indiana
    Posts
    261

    Overriding CListCtrl in CListView

    I have my own CListCtrl class and want to use it in my CListView derived class. What is best way to do this? Thanks in advance.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Overriding CListCtrl in CListView

    Instead of a CListView view, why not use a CFormView view, put your control in it and size the control to the CFormView?

  3. #3
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Overriding CListCtrl in CListView

    Instead of a CListView view, why not use a CFormView view, put your control in it and size the control to the CFormView?
    Who said anything about size ?

    What is best way to do this?
    Like you derive any other class.

  4. #4
    Join Date
    Feb 2005
    Location
    Indiana
    Posts
    261

    Re: Overriding CListCtrl in CListView

    Quote Originally Posted by Arjay
    Instead of a CListView view, why not use a CFormView view, put your control in it and size the control to the CFormView?
    Originally when I posted a problem I was having I had a CMDIChildWnd that just contained a CListCtrl inside and everyone said to use CListView so that's what I'm attempting now. I guess since everyone told me to do it this way on here originally that's why I did it this way. If I was just going to use CFormView and resize it I may as well keep my original code that resized it to the CMDIChildWnd cause it worked. Although using CFormView may be better than just putting it in the CMDIChildWnd since it follows the doc/view model better.

    Basic answer I guess cause people on here told me to use CListView.

    Quote Originally Posted by Skizmo View Post
    Who said anything about size ?
    Like you derive any other class.
    Yeah but I want to derive from the CListCtrl that is in the CListView. Do I use SubclassWindow()? I already have the CListCtrl class done and it works just want to use that in my CListView class. Thanks both of you and eager to hear more before I move forward.

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Overriding CListCtrl in CListView

    Quote Originally Posted by Skizmo View Post
    Who said anything about size ?
    Presumably the OP would want the CListCtrl to resize when the view resized, right?

  6. #6
    Join Date
    Jun 2006
    Posts
    645

    Re: Overriding CListCtrl in CListView

    Or may be the following...

    OP might be a little confused over the functionality provided by CListView class and CListCtrl.
    Here is some detailed explanation, and if wrong please correct me...

    Using CListCtrl in a CFormView and sizing it with the parent container that is the CFormView dervied class is one approach. In this, all the notifications to the list control and its items would be send through the CFormView which happens to be the parent. Any interaction with the document class and its variables, has to be via CFormView derived class. Contrarily, CListView is an independent View class, that directly interacts with the document class. So the notifications can be sent directly to the underlying listctrl object and class level interaction with document class members is possible via document class pointer.

    CListCtrl is inherited from CWnd class where as CListView is derived from CCtrlView. The view word here makes the difference and emphasizes that the class holds the data to be displayed in a view in an SDI / MDI application.

    CListCtrl is instantiated and directly can access its items and sub items. A CListView gets a reference to the control object which is a List Control and Invalidates the view or the document data using this control.

    Last time I worked with both of them was in VS 2005. If I am not wrong, both approaches above would offer you similar functionality if you can work around them. I am not sure, but some of the styles present in the CListCtrl class are not available to CListView. Some notification handlers are different, update functions are different and most importantly paint intervals and device contexts are different.

    Again, as I said, this is what I have known so far....and hope that helps

    Regards,
    Bhushan

  7. #7
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Overriding CListCtrl in CListView

    Have a look at how MFC 'transformed' a CListCtrl into a CListView

    then use the same principle to transform your CMyListCtrl into a CMyListView.

    the same principle can be applied to turn any control type into a view type (with some reservations).

  8. #8
    Join Date
    Feb 2005
    Location
    Indiana
    Posts
    261

    Re: Overriding CListCtrl in CListView

    Will look into that. Time to pull out the MFC Internals book and go through MFC code as well. Thanks.

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Overriding CListCtrl in CListView

    Quote Originally Posted by left1none View Post
    Originally when I posted a problem I was having I had a CMDIChildWnd that just contained a CListCtrl inside and everyone said to use CListView so that's what I'm attempting now. I guess since everyone told me to do it this way on here originally that's why I did it this way. If I was just going to use CFormView and resize it I may as well keep my original code that resized it to the CMDIChildWnd cause it worked. Although using CFormView may be better than just putting it in the CMDIChildWnd since it follows the doc/view model better.
    It may have sort of looked like it worked, but if you want to take full advantage of the doc/view architecture, you should use some kind of view class. CMDIChildWnd has a specific purpose as do views and it's best not to mix them up. If you don't need doc/view, perhaps a dialog based app would be more appropriate.

  10. #10
    Join Date
    Feb 2005
    Location
    Indiana
    Posts
    261

    Re: Overriding CListCtrl in CListView

    Quote Originally Posted by OReubens View Post
    Have a look at how MFC 'transformed' a CListCtrl into a CListView

    then use the same principle to transform your CMyListCtrl into a CMyListView.

    the same principle can be applied to turn any control type into a view type (with some reservations).
    Ok looked into it and it seems easy enough but what do I put for WC_LISTVIEW. I mean I don't know my CListCtrl derived window class name.
    Code:
    _AFXCVIEW_INLINE CListView::CListView()
    : CCtrlView(WC_LISTVIEW, AFX_WS_DEFAULT_VIEW)
    { }

  11. #11
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Overriding CListCtrl in CListView

    Quote Originally Posted by left1none View Post
    I don't know my CListCtrl derived window class name.
    The class name of a derived class is the same as the base class.
    Victor Nijegorodov

  12. #12
    Join Date
    Feb 2005
    Location
    Indiana
    Posts
    261

    Re: Overriding CListCtrl in CListView

    Quote Originally Posted by VictorN View Post
    The class name of a derived class is the same as the base class.
    I came to the same conclusion because I called AfxMessageBox() to display the classname in my custom CListCtrl and it came up as "SysListView" which I knew was the CListCtrl in MFC.

    So if I use that for a class name then how do I make sure when I derive from CCtrlView that it uses my CListCtrl derived class instead of the standard CListCtrl? I'm guessing it's this line
    Code:
    _AFXCVIEW_INLINE CListCtrl& CListView::GetListCtrl() const
    	{ return *(CListCtrl*)this; }
    But I don't see where CCtrlView calls this method anywhere in the MFC source code.

    So are you saying to just override CCtrlView, pass "WC_LISTVIEW" in the constructor and just override GetListCtrl to return my CEnhancedListCtrl and it'll use my CListCtrl in my derived view?

  13. #13
    Join Date
    Feb 2005
    Location
    Indiana
    Posts
    261

    Re: Overriding CListCtrl in CListView

    Actually just tried what I mentioned in previous post and it just created a CListView not with my derived class. hmmmm.

    Jack

  14. #14
    Join Date
    Feb 2005
    Location
    Indiana
    Posts
    261

    Re: Overriding CListCtrl in CListView

    I would still love to know how to do this but I found a work around. I took out all my OnCustomDraw() and OnEraseBkgd() code out and put it in my CListView and it works. But if anyone figures out how to turn a custom control class into a view I would love to figure that out for the future. Thanks.

  15. #15
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Overriding CListCtrl in CListView

    Quote Originally Posted by left1none View Post
    I came to the same conclusion because I called AfxMessageBox() to display the classname in my custom CListCtrl and it came up as "SysListView" which I knew was the CListCtrl in MFC.

    So if I use that for a class name then how do I make sure when I derive from CCtrlView that it uses my CListCtrl derived class instead of the standard CListCtrl? I'm guessing it's this line
    Code:
    _AFXCVIEW_INLINE CListCtrl& CListView::GetListCtrl() const
    	{ return *(CListCtrl*)this; }
    Exactly!
    So if your derivred class were called CMyListCtrl then this GetListCtrl() method should look like:
    Code:
    _AFXCVIEW_INLINE CMyListCtrl& CListView::GetListCtrl() const
    	{ return *(CMyListCtrl*)this; }
    Victor Nijegorodov

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured