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

  • U.S. companies are desperately trying to recruit and hire skilled software engineers and developers, but there's simply not enough quality talent to go around. In response, companies often resort to inferior solutions -- hiring substandard developers and engineers, recruiting talent on a part-time or temporary basis, poaching people from competitors, or burdening an already stressed IT staff for more of their labor. Fortunately, there's a better solution. Read this white paper to learn the business value of …

  • Lenovo recommends Windows 8 Pro. "I dropped my laptop getting out of the taxi." This probably sounds familiar to most IT professionals. If your employees are traveling, you know their devices are in for a rough go. Whether it's a trip to the conference room or a convention out of town, any time equipment leaves a user's desk it is at risk of being put into harm's way. Stay connected at all times, whether at the office or on the go, with agile, durable, and flexible devices like the Lenovo® …

Most Popular Programming Stories

More for Developers

RSS Feeds

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