How to Create a Custom View to Wrap Your Own Control
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

Comments
Abroad Rumor : gucci Looked as A Necessity In today's times
Posted by incockDak on 03/29/2013 08:07amNew 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
ReplyJust stop Protesting and complaining , Start Off your own special nike shoes Marketing Instead
Posted by expopmerm on 03/18/2013 01:59amShorter 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