Click to See Complete Forum and Search --> : Switching between views in SDI doc-view using Button


N. Jothi
08-20-2000, 12:09 PM
HI

How to switch between two views in SDI using a button control. That is I
have a button on each views. When I press the button in one view the other
view should be displayed & vice-versa. Please response to this.

N. Jothi

Christopher Thomas McGinlay
08-21-2000, 02:04 PM
"N. Jothi" wrote:
>
> HI
>
> How to switch between two views in SDI using a button control. That is I
> have a button on each views. When I press the button in one view the other
> view should be displayed & vice-versa. Please response to this.
>
> N. Jothi

Hello N.

I've been working on the same problem and have so far come up with the
following function in my CMainFrame class. I've used the IDD_ resource
symbols for the views to identify and exchange them. This function
appears to work, but I'm sure our fellows here in the newsgroup can
suggest alternatives. I call SwitchToView() from an OnMyButtonClick()
handler function in the already-open view, although you could equally
use a menu handler.

As I say, it works, but I'm not a 100% happy with it - I don't like
using 'new' to create the view since the views are supposed to have
protected constructors - let me know if you find a way round this!!!!
(By the way, if you try my horrible hack here, you'll have to change the
specifier for your view constructors as public: instead of protected:

It's a shameful hack I suspect!

Regards

Chris

//returns ID of previous view. 0 if fails
UINT CMainFrame::SwitchToView(UINT nID)
{
CView* pOldView = GetActiveView();
CView* pNewView = (CView*) GetDlgItem(nID); //view to switch to

//Test to see whether view needs to be created or if it already exists
if (pNewView==NULL)
{
switch(nID)
{
case IDD_PUPIL_FORM: //pupil view
pNewView = (CView*) new CPupilView;
break;
case IDD_REPORT_FORM: //report view
pNewView = (CView*) new CReportView;
break;
case IDD_MARK_FORM: //mark view
pNewView = (CView*) new CMarkView;
break;
case IDD_ENROLLMENT_FORM:
pNewView = (CView*) new CEnrollmentView; //enrollment view
break;
default:
AfxMessageBox("Switch to view: View doesn't exist!");
return NULL; //failure
}

//Switching code for new view (once only for each view)
CCreateContext context;
context.m_pCurrentDoc = pOldView->GetDocument();

pNewView->Create(NULL,NULL,0L,CFrameWnd::rectDefault,this,nID,&context);
pNewView->OnInitialUpdate();
}

SetActiveView(pNewView,TRUE);
pOldView->ShowWindow(SW_HIDE);
pNewView->ShowWindow(SW_SHOW);
CRuntimeClass* pRTC = pOldView->GetRuntimeClass();
pOldView->SetDlgCtrlID(
pRTC == RUNTIME_CLASS(CPupilView) ? IDD_PUPIL_FORM :
pRTC == RUNTIME_CLASS(CReportView) ? IDD_REPORT_FORM :
pRTC == RUNTIME_CLASS(CMarkView) ? IDD_MARK_FORM :
pRTC == RUNTIME_CLASS(CEnrollmentView) ? IDD_ENROLLMENT_FORM : NULL );
ASSERT(pOldView->GetDlgCtrlID());

UINT nOld = m_nCurrentViewID;

pNewView->SetDlgCtrlID(AFX_IDW_PANE_FIRST);
m_nCurrentViewID = nID;
RecalcLayout(TRUE);
return nOld;
}


--
Ascent Software Limited Registered in Scotland: SC201671
ascent@zetnet.co.uk Bank Of Scotland Buildings
01950 422 431 Lerwick, Shetland, ZE1 0EB

Mike Ramirez
10-31-2000, 12:07 PM
Christopher Thomas McGinlay <ascent@zetnet.co.uk> wrote:
>"N. Jothi" wrote:
>>
>> HI
>>
>> How to switch between two views in SDI using a button control. That is
I
>> have a button on each views. When I press the button in one view the other
>> view should be displayed & vice-versa. Please response to this.
>>
>> N. Jothi
>
>Hello N.
>
>I've been working on the same problem and have so far come up with the
>following function in my CMainFrame class. I've used the IDD_ resource
>symbols for the views to identify and exchange them. This function
>appears to work, but I'm sure our fellows here in the newsgroup can
>suggest alternatives. I call SwitchToView() from an OnMyButtonClick()
>handler function in the already-open view, although you could equally
>use a menu handler.
>
>As I say, it works, but I'm not a 100% happy with it - I don't like
>using 'new' to create the view since the views are supposed to have
>protected constructors - let me know if you find a way round this!!!!
>(By the way, if you try my horrible hack here, you'll have to change the
>specifier for your view constructors as public: instead of protected:
>
>It's a shameful hack I suspect!
>
>Regards
>
>Chris
>
>//returns ID of previous view. 0 if fails
>UINT CMainFrame::SwitchToView(UINT nID)
>{
>CView* pOldView = GetActiveView();
>CView* pNewView = (CView*) GetDlgItem(nID); //view to switch to
>
>//Test to see whether view needs to be created or if it already exists
>if (pNewView==NULL)
>{
> switch(nID)
> {
> case IDD_PUPIL_FORM: //pupil view
> pNewView = (CView*) new CPupilView;
> break;
> case IDD_REPORT_FORM: //report view
> pNewView = (CView*) new CReportView;
> break;
> case IDD_MARK_FORM: //mark view
> pNewView = (CView*) new CMarkView;
> break;
> case IDD_ENROLLMENT_FORM:
> pNewView = (CView*) new CEnrollmentView; //enrollment view
> break;
> default:
> AfxMessageBox("Switch to view: View doesn't exist!");
> return NULL; //failure
> }
>
> //Switching code for new view (once only for each view)
> CCreateContext context;
> context.m_pCurrentDoc = pOldView->GetDocument();
>
>pNewView->Create(NULL,NULL,0L,CFrameWnd::rectDefault,this,nID,&context);
> pNewView->OnInitialUpdate();
> }
>
> SetActiveView(pNewView,TRUE);
> pOldView->ShowWindow(SW_HIDE);
> pNewView->ShowWindow(SW_SHOW);
> CRuntimeClass* pRTC = pOldView->GetRuntimeClass();
> pOldView->SetDlgCtrlID(
> pRTC == RUNTIME_CLASS(CPupilView) ? IDD_PUPIL_FORM :
> pRTC == RUNTIME_CLASS(CReportView) ? IDD_REPORT_FORM :
> pRTC == RUNTIME_CLASS(CMarkView) ? IDD_MARK_FORM :
> pRTC == RUNTIME_CLASS(CEnrollmentView) ? IDD_ENROLLMENT_FORM : NULL );
> ASSERT(pOldView->GetDlgCtrlID());
>
> UINT nOld = m_nCurrentViewID;
>
> pNewView->SetDlgCtrlID(AFX_IDW_PANE_FIRST);
> m_nCurrentViewID = nID;
> RecalcLayout(TRUE);
> return nOld;
>}
>
>
>--
>Ascent Software Limited Registered in Scotland: SC201671
>ascent@zetnet.co.uk Bank Of Scotland Buildings
>01950 422 431 Lerwick, Shetland, ZE1 0EB

I found a class on CodeGuru (http://codeguru.earthweb.com/) which added a
class derived from CDocTemplate that was designed to work with multiple views
related to one document.

I was real happy with it. If you dig around there you'll be able to find
it. (BTW, there is a sample with the class that doesn't work with VC++ 5.0)