CTabView - CView's HWND is NULL
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: CTabView - CView's HWND is NULL

  1. #1
    Join Date
    Aug 2000
    Location
    Virginia, US
    Posts
    158

    CTabView - CView's HWND is NULL

    I'm using the Feature Pack CTabView, and I noticed that when the view is added using AddView with the following code CView's HWND is NULL when I try and access each of the CView1,CView2, etc.

    Code:
    int CMyTabView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
    {
        if (CTabView::OnCreate(lpCreateStruct) == -1)
            return -1;
        AddView (RUNTIME_CLASS (CView1), _T("Simple"),		100);
        AddView (RUNTIME_CLASS (CView2), _T("Pair"),		101);
        AddView (RUNTIME_CLASS (CView3), _T("Data"),		102);
    
        return 0;
    }
    
    I think the user has to click the view's tab before the CTabView assigns an HWND.  Is there a way to force CTabview to give each CView an HWND when the view is added?

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,517

    Re: CTabView - CView's HWND is NULL

    I guess the behavior of CTabView::AddView is the same as the one of CPropertySheet::AddPage.
    So there is no need to create a window being later shown (after a user selects it or you call CTabView::SetActiveView) before it will be needed to be shown.
    Why do you need to have each CView already created when the view is added?
    Victor Nijegorodov

  3. #3
    Join Date
    Aug 2000
    Location
    Virginia, US
    Posts
    158

    Re: CTabView - CView's HWND is NULL

    Quote Originally Posted by VictorN View Post
    I guess the behavior of CTabView::AddView is the same as the one of CPropertySheet::AddPage.
    So there is no need to create a window being later shown (after a user selects it or you call CTabView::SetActiveView) before it will be needed to be shown.
    Why do you need to have each CView already created when the view is added?
    I have a clistctrl on the view that I want to update from a database. The database updated the view before it is displayed. So I want the view to have an HWND so I can access its functions.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,517

    Re: CTabView - CView's HWND is NULL

    Why not update "a clistctrl n the view" in the CView::OnInitialUpdate override? It is called before the view is displayed.
    Victor Nijegorodov

  5. #5
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,039

    Re: CTabView - CView's HWND is NULL

    According to the code below all views are created immediately inside AddView and later can be accessible via document object:

    Code:
    int CTabView::AddView(CRuntimeClass* pViewClass, const CString& strViewLabel, int iIndex /*= -1*/, CCreateContext* pContext/* = NULL*/)
    {
    	ASSERT_VALID(this);
    	ENSURE(pViewClass != NULL);
    	ENSURE(pViewClass->IsDerivedFrom(RUNTIME_CLASS(CView)));
    
    	CView* pView = DYNAMIC_DOWNCAST(CView, pViewClass->CreateObject());
    	ASSERT_VALID(pView);
    
    	if (!pView->Create(NULL, _T(""), WS_CHILD | WS_VISIBLE, CRect(0, 0, 0, 0), &m_wndTabs, (UINT) -1, pContext))
    	{
    		TRACE1("CTabView:Failed to create view '%s'\n", pViewClass->m_lpszClassName);
    		return -1;
    	}
    
    	CDocument* pDoc = GetDocument();
    	if (pDoc != NULL)
    	{
    		ASSERT_VALID(pDoc);
    
    		BOOL bFound = FALSE;
    		for (POSITION pos = pDoc->GetFirstViewPosition(); !bFound && pos != NULL;)
    		{
    			if (pDoc->GetNextView(pos) == pView)
    			{
    				bFound = TRUE;
    			}
    		}
    
    		if (!bFound)
    		{
    			pDoc->AddView(pView);
    		}
    	}
    
    	m_wndTabs.InsertTab(pView, strViewLabel, iIndex);
    
    	int nTabs = m_wndTabs.GetTabsNum();
    	return nTabs - 1;
    }
    Best regards,
    Igor

  6. #6
    Join Date
    Aug 2000
    Location
    Virginia, US
    Posts
    158

    Re: CTabView - CView's HWND is NULL

    Thanks Igor, that makes sense. Where does that code come from?

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,517

    Re: CTabView - CView's HWND is NULL

    From <your VS folder>\VC\atlmfc\src\mfc\afxtabview.cpp
    Victor Nijegorodov

  8. #8
    Join Date
    Aug 2000
    Location
    Virginia, US
    Posts
    158

    Re: CTabView - CView's HWND is NULL

    Got it, I added the function to my CMyTabView class and now I have the HWND.

    Thanks

  9. #9
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,517

    Re: CTabView - CView's HWND is NULL

    Quote Originally Posted by cbpetro View Post
    Got it, I added the function to my CMyTabView class and now I have the HWND.
    But which AddView did you call in the CMyTabView::OnCreate() handler?
    Victor Nijegorodov

  10. #10
    Join Date
    Aug 2000
    Location
    Virginia, US
    Posts
    158

    Re: CTabView - CView's HWND is NULL

    Quote Originally Posted by VictorN View Post
    But which AddView did you call in the CMyTabView::OnCreate() handler?
    The AddView code that Igor provided, I had to change my AddView call from...


    AddView (RUNTIME_CLASS (CView1), _T("Simple"), 100);

    to...

    AddView (RUNTIME_CLASS (CView1), _T("Simple"), 100 , NULL);

  11. #11
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,517

    Re: CTabView - CView's HWND is NULL

    Do you have your own AddView method in CMyTabView class?
    Otherwise there would be no difference in these two calls
    Code:
    AddView (RUNTIME_CLASS (CView1), _T("Simple"),	100);
    and
    Code:
    AddView (RUNTIME_CLASS (CView1), _T("Simple"),	100 , NULL);
    because the forth parameter of CTabView::AddView has default value NULL:
    CTabView::AddView


    Adds a view to the tab control.
    Code:
    int AddView(
       CRuntimeClass* pViewClass,
       const CString& strViewLabel,
       int iIndex=-1,
       CCreateContext* pContext=NULL 
    );
    Victor Nijegorodov

  12. #12
    Join Date
    Aug 2000
    Location
    Virginia, US
    Posts
    158

    Re: CTabView - CView's HWND is NULL

    Quote Originally Posted by VictorN View Post
    Do you have your own AddView method in CMyTabView class?
    Otherwise there would be no difference in these two callsbecause the forth parameter of CTabView::AddView has default value NULL:
    Correct, but when I use the default AddView I never have the HWND of each CView, unless I call OnActivateView or something as I am not use the doc/view architecture. If I use the custom AddView, I see the CView being created and I can customize the AddView further by adding each CView to a CSimpleArray in my CMyTabView class.

    Now I have access to each CView in CMyTabView from anywhere in my program.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center