How to Create a Custom View to Wrap Your Own Control

Environment: VC++ 5.0, SP3, Win95

One of the questions that I see posted to frequently is "How do I use my CListCtrl (or CTreeCtrl, CListBox, etc.) -derived control with a CListView (or CTreeView, etc.)?" As Zafir Anjum pointed out in his article "How do I use a derived CListCtrl with a CListView?", you don't. He provided the option of deriving your class from CListView or CTreeView, instead of the corresponding control class, and adding all of the desired customization to your newly derived view class.

Zafir's approach works with controls for which MFC provides a view class. However, if MFC doesn't provide a corresponding view class, or if you want to use your nice new control in both a view and a dialog, that solution is less than perfect. While you can use a CView-derived class in a dialog, it's cumbersome. Instead, I suggest that you leave all of the customization in the control-derived class and simply roll your own custom CView-derived class for it. It's really quite easy to do. The following example uses a CListCtrl-derived class.

Step 1: Derive a custom class from CView

This is easily done with the Class Wizard. Simply click on the "Add" button and choose "New class...". Select CView as the base class and enter a name for your derived class.

Step 2: Add a member variable of your custom control class

CMyListCtrl m_ListCtrl;

Step 3: Override the OnCreate() function of your CView-derived class

Override this function and add code to create your control.

// When OnCreate is called for the view, we create the
// CMyListCtrl instance that will occupy the client area 
// of the view.

int CMyListView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
 if (CView::OnCreate(lpCreateStruct) == -1)
 return -1;

 // Create the style

 // Create the list control.  Don't worry about specifying
 // correct coordinates.  That will be handled in OnSize()
 BOOL bResult = m_ListCtrl.Create(dwStyle, CRect(0,0,0,0), 
 this, IDC_LIST1); 

return (bResult ? 0 : -1);
} //OnCreate

Step 4: Override the OnSize() function of your CView-derived class

Override this function and add code to resize the control so that it occupies the entire client area of the CView-derived class.

// Override OnSize to resize the control to match the view
void CMyListView::OnSize(UINT nType, int cx, int cy) 
 CView::OnSize(nType, cx, cy);
 if (::IsWindow(m_ListCtrl.m_hWnd))
  m_ListCtrl.MoveWindow(0, 0, cx, cy, TRUE);

Step 5 (Optional): Override the OnInitialUpdate() function of your CView-derived class

If you wish, override this function and call any member functions of your control needed to initialize it. I usually create a member function in my control named Init() that reads in any data and adds it to the control. This function can be called either from CMyView::OnInitialUpdate() or CMyDialog::OnInitDialog().

void CMyListView::OnInitialUpdate() 


Download demo project - 36 Kb


  • Abroad Rumor : gucci Looked as A Necessity In today's times

    Posted by incockDak on 03/29/2013 08:07am

    New gucci Publication Tells Very Best Way To Rule The longchamp Market [url=]バック[/url] Honest blog post lets out Ten brand-new stuff around gucci that noone is speaking about. [url=]Sac longchamp soldes[/url] DuoWyqOezPrx [url=]nike ランニング[/url]PcdEonQsnDuy [[url=]free nike[/url]FryDcoFzrCjm [url=]ナイキスニーカー[/url]NmuJeyCrkPak [url=]ナイキスニーカー[/url]QqrTucJvaRam [url=]nike スニーカー[/url]GskRegTbdOtx [url=]ナイキ[/url]CeuGehKrsFff [url=]nike シューズ[/url]GybMesTziEgn [url=]スニーカー adidas[/url]DrvUnqKjaQsp

  • Just stop Protesting and complaining , Start Off your own special nike shoes Marketing Instead

    Posted by expopmerm on 03/18/2013 01:59am

    Shorter story unwraps the unquestionable info regarding nike shoes and ways in which it may harm customers.|A quick post demonstrates to you all cogs and wheels linked with nike shoes coupled with something you ought to do straight away.}[url=]nike air[/url] Magical Formula For adidas shoes [url=]adidas スニーカー[/url] Researches-- nike shoes Will certainly Have A Significant role In Any Organization Development- gucci May Have An Essential role In Almost Any Management [url=]グッチ 財布[/url] New chloe bags Guide Explains Simple Tips To Rule The chloe bags Marketplace [url=]クロエ 財布[/url] Couple of securely-protected chanel bags tricks described in clear details. [url=]chanel バッグ[/url] Methods to discover pretty much everything there is to know around chanel in Few easy ways.The Slack Male's Path To The nike shoes Achievement [url=]adidas シューズ[/url] A nice double twist on nike shoes [url=]nike スニーカー[/url] Exactly how to learn all the things there is to find relating to nike shoes in nine easy ways.

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Learn How A Global Entertainment Company Saw a 448% ROI Every business today uses software to manage systems, deliver products, and empower employees to do their jobs. But software inevitably breaks, and when it does, businesses lose money -- in the form of dissatisfied customers, missed SLAs or lost productivity. PagerDuty, an operations performance platform, solves this problem by helping operations engineers and developers more effectively manage and resolve incidents across a company's global operations. …

  • Live Event Date: December 18, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this upcoming webcast …

Most Popular Programming Stories

More for Developers

RSS Feeds