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?
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?
Re: CTabView - CView's HWND is NULL
Quote:
Originally Posted by
VictorN
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.
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.
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;
}
Re: CTabView - CView's HWND is NULL
Thanks Igor, that makes sense. Where does that code come from?
Re: CTabView - CView's HWND is NULL
From <your VS folder>\VC\atlmfc\src\mfc\afxtabview.cpp
Re: CTabView - CView's HWND is NULL
Got it, I added the function to my CMyTabView class and now I have the HWND.
Thanks
Re: CTabView - CView's HWND is NULL
Quote:
Originally Posted by
cbpetro
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? :confused:
Re: CTabView - CView's HWND is NULL
Quote:
Originally Posted by
VictorN
But which AddView did you call in the CMyTabView::OnCreate() handler? :confused:
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);
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
Quote:
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:
Quote:
CTabView::AddView
Adds a view to the tab control.
Code:
int AddView(
CRuntimeClass* pViewClass,
const CString& strViewLabel,
int iIndex=-1,
CCreateContext* pContext=NULL
);
Re: CTabView - CView's HWND is NULL
Quote:
Originally Posted by
VictorN
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.