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

  • Thanks to wide spread cloud hosting and innovations small businesses can meet and exceed the legacy systems of goliath corporations. Explore the freedom to work how you want, with a phone system that will adapt to your evolving needs and actually save you lots of expense—read Get an Enterprise Phone System without High Cost and Complexity. The article clearly illustrates: The only hardware you'll need is phone equipment for advanced voice and fax. How to join all your employees, mobile devices, …

  • The nature of the financial services industry places a myriad of international compliance requirements on a company's IT team, as well as an expectation by its customers to deliver the high test levels of performance and reliability. To survive and thrive, businesses in the industry must not only keep pace with customer demand but gain competitive advantage. Those demands mean the IT team must be at the forefront of adopting emerging technologies

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date