Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

splitter window 1

Status
Not open for further replies.

serpem

Programmer
Feb 20, 2003
22
US
hello,
does anyone know how to dynamically change the view in a splitter windows'pane at runtime?
thanks
 
Here is part of a class i use to do that for me. This function gives the basic concept.

Code:
// Returns the old/replaced CView*
CView* CSplitterWndEx::ReplaceView(CView *pView, int row, int col)
{
	int paneID, oldID, newID;
	ASSERT( pView);
	ASSERT(::IsWindow( pView->m_hWnd));
	CWnd* pWnd = CSplitterWnd::GetPane(row,col);
	ASSERT(pWnd && pWnd->GetRuntimeClass()->IsDerivedFrom( RUNTIME_CLASS(CView)));

	// make sure all views are stored
	_views.SetAt( pView->GetRuntimeClass()->m_lpszClassName, pView);
	_views.SetAt( pWnd->GetRuntimeClass()->m_lpszClassName, (CView*)pWnd);

	paneID = CSplitterWnd::IdFromRowCol( row, col);
	oldID = pWnd->GetDlgCtrlID();
	newID = pView->GetDlgCtrlID();

	// Do not replace if it is the same view !
	if ( paneID != newID){
		pView->SetDlgCtrlID( paneID);
		pWnd->SetDlgCtrlID( newID);

		pWnd->ShowWindow(SW_HIDE);
		pView->ShowWindow(SW_SHOW);
		CSplitterWnd::RecalcLayout();
		pView->Invalidate();
	}

	return (CView*)pWnd;
}

In header file:
Code:
CTypedPtrMap<CMapStringToPtr, CString, CView*> _views;
 
PerFnurt, I was talking about &quot;changing&quot; as &quot;replacing&quot;.
Palbano, can you tell me how do I get get a pointer to the view I want to pass to your function, I usually deal with views through their runtime class and I don't know how to get a pointer. To show you what I mean, here is the code I usually use to replace views in my programs when I don't have a splitter window:
void CMainFrame::SelectView(CRuntimeClass* pNewViewClass,int val)
{
//get current view
CView* pOldView = GetActiveView();
CView* pNewView = NULL;

//if the new view had already been created, find it
CDocument* pDoc = GetActiveDocument();
POSITION pos = pDoc->GetFirstViewPosition();
while(pos && !pNewView)
{
CView* pView = pDoc->GetNextView(pos);
if(pView->IsKindOf(pNewViewClass))
pNewView = pView;
}

//else, create the new view
if(pNewView == NULL)
{
CCreateContext context;
context.m_pCurrentDoc = pDoc;

pNewView = (CView*)pNewViewClass->CreateObject();
pNewView->Create(NULL,NULL,0, CFrameWnd::rectDefault,this,val,&context);
pNewView->OnInitialUpdate();
}

//display the new view
SetActiveView(pNewView);
pNewView->ShowWindow(SW_SHOW);
pOldView->ShowWindow(SW_HIDE);

pOldView->SetDlgCtrlID(pNewView->GetDlgCtrlID());
pNewView->SetDlgCtrlID(AFX_IDW_PANE_FIRST);

RecalcLayout();

}
void CMainFrame::OnView1()
{
SelectView(RUNTIME_CLASS(CView1),1);
}
void CMainFrame::OnView2()
{
SelectView(RUNTIME_CLASS(CView2),2);
}

thanks.
 
Here is the function in my class that manages creating CViews or finding them in the map.

Code:
// Returns the already stored view or dynamically creates one stores it
// and returns it
CView* CSplitterWndEx::getStoredView(CRuntimeClass *pRTC, CDocument *pDoc)
{
	ASSERT( pRTC && pDoc);

	CView* pView = NULL;
	BOOL bFound = _views.Lookup( pRTC->m_lpszClassName, pView);
	if ( !bFound){
		// create the view and add it to the map
		pView = (CView*)pRTC->CreateObject();
		ASSERT(pView);
		CCreateContext cc;
		ZeroMemory(&cc, sizeof(cc));
		cc.m_pCurrentDoc = pDoc;
		if (pView->Create(NULL, &quot;&quot;, WS_CHILD, CRect(0,0,0,0), this,
			_nNextCtrlID++, &cc) ){

			pView->OnInitialUpdate();
			// views are mapped by their class name
			_views.SetAt( pRTC->m_lpszClassName, pView);
		}
	}

	ASSERT( pView);
	return pView;
}

-pete
 
everything is working fine. Thanks a lot!
 
First off, thanks for the code. It's helped me a lot.
Secondly, I want to ask, how can I replace a view with a nested splitter window (with 2 rows - CFormView top, CListView bottom)?

More info:
My program is split into 2 panes. The left pane doesn't change, but the right one does depending on the selection from the left pane, which is a CTreeView. At the moment, I'm able to change views without any problems. But now, instead of changing to a view, I want to change to a nested splitter window that has 2 views.

Any help would be appreciated. Thanks in advance.
 
Personally i would not nest splitter windows. It's more trouble that it is worth. In a case like yours i would use, actually have used, a view and handle the splitting of that view within itself.

Luckily for you Paul Dilascia has already written the code for handling all the issues of drawing splitter bars and dividing a view or dialog into anchored sizeable grids of controls. It works pretty much like the GridBag Layout in Java. Actually it is really awesome and best of all it’s free! A guy on CodeProject has even ported it to WTL it’s just that good.


-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top