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 microsoft.public.vc.mfc 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

protected:
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
 DWORD dwStyle = WS_CHILD | WS_VISIBLE | WS_TABSTOP |
 LVS_REPORT;

 // 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);
}//OnSize

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() 
{
 CView::OnInitialUpdate();
 m_ListCtrl.Init();
}//OnInitialUpdate

Downloads

Download demo project - 36 Kb


Comments

  • 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=http://growth-management.alachua.fl.us/comprehensive_planning/gucci.html]バック[/url] Honest blog post lets out Ten brand-new stuff around gucci that noone is speaking about. [url=http://growth-management.alachua.fl.us/comprehensive_planning/saclongchamp.php]Sac longchamp soldes[/url] DuoWyqOezPrx [url=http://running-nike0.seesaa.net/]nike ランニング[/url]PcdEonQsnDuy [[url=http://free-nike-nikeo.seesaa.net/]free nike[/url]FryDcoFzrCjm [url=http://xn--nike-ul4c5c5fyqb.seesaa.net/]ナイキスニーカー[/url]NmuJeyCrkPak [url=http://nikejapan0.seesaa.net/]ナイキスニーカー[/url]QqrTucJvaRam [url=http://nikesneakersjp.seesaa.net/]nike スニーカー[/url]GskRegTbdOtx [url=http://nikegolf00.seesaa.net/]ナイキ[/url]CeuGehKrsFff [url=http://nikeshoes00.seesaa.net/]nike シューズ[/url]GybMesTziEgn [url=http://sneaker-adidas-jp.seesaa.net/]スニーカー adidas[/url]DrvUnqKjaQsp

    Reply
  • 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=http://www.nikejapan.asia/]nike air[/url] Magical Formula For adidas shoes [url=http://www.adidasjapan.biz/]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=http://www.guccijp.asia/]グッチ 財布[/url] New chloe bags Guide Explains Simple Tips To Rule The chloe bags Marketplace [url=http://www.chloejp.biz/]クロエ 財布[/url] Couple of securely-protected chanel bags tricks described in clear details. [url=http://www.chaneljp.biz/]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=http://www.adidasjapan.asia/]adidas シューズ[/url] A nice double twist on nike shoes [url=http://www.nikejp.biz/]nike スニーカー[/url] Exactly how to learn all the things there is to find relating to nike shoes in nine easy ways.

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

Top White Papers and Webcasts

  • The explosion in mobile devices and applications has generated a great deal of interest in APIs. Today's businesses are under increased pressure to make it easy to build apps, supply tools to help developers work more quickly, and deploy operational analytics so they can track users, developers, application performance, and more. Apigee Edge provides comprehensive API delivery tools and both operational and business-level analytics in an integrated platform. It is available as on-premise software or through …

  • Live Event Date: September 10, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild". This loop of continuous delivery and continuous feedback is …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds