Help creating simple view swapping 
Author Message
 Help creating simple view swapping

I'm trying to create a Simple Views that a sub classes of CView that I can
swap in and out as I needed.  Unfortunately, MFC View creation looks like a
real mess.  All I want to do is something like this:

myView = new MyView(parentView);
parentView.place(myView, 0, 0);
myView.setEnabled(true);
myView.setVisible(true);

Why is it so hard in to do in MFC?  Any body has any clean solutions to this
approach?
TIA



Sun, 14 Mar 2004 02:51:22 GMT  
 Help creating simple view swapping

Quote:

> I'm trying to create a Simple Views that a sub classes of CView that I can
> swap in and out as I needed.  Unfortunately, MFC View creation looks like a
> real mess.  All I want to do is something like this:

> myView = new MyView(parentView);
> parentView.place(myView, 0, 0);
> myView.setEnabled(true);
> myView.setVisible(true);

> Why is it so hard in to do in MFC?  Any body has any clean solutions to this
> approach?
> TIA

The view is a child of the CMainFrame window, which makes it fit within
the available area after toolbars, status bars, etc. are positioned.  A
lot of code under the hood supports this, and supports dynamic creation
of the view and linking it to the document.  That's why it's "so hard" -
you have to learn how MFC needs it to be done.

See the VSWAP32 sample code for the view swapping technique.

--
Scott McPhillips [VC++ MVP]



Sun, 14 Mar 2004 09:47:41 GMT  
 Help creating simple view swapping
Thanks Scott.  I'm looking at the example, but its only swapping views at
the top level, which seems relatively easy.  What I'm trying to do is to
have a top level view which will swap other sub views in and out (like in
MSN Messenger) based on user cliks on icons.  I need it to happen on a
Single Doc document (not Dialog based, because thats what the requirements
are).

I tried creating a SwapView() method and tried this after creating the view

void CMAPView::SwitchView(EView viewIdx)
{
 TRACE("CMAPView::SwitchView(%d)\n", viewIdx);

 CView* curViewPtr = mCurrentView;

 CView* newViewPtr = NULL;

#if 0
CView* newViewPtr = mViewArr[viewIdx];
#else
 switch(viewIdx) {

  case FIRST_VIEW:
   newViewPtr = new FirstView();
   break;

  case SECOND_VIEW:
   newViewPtr  = new SecondView();
   break;
 }

 CCreateContext context;
 context.m_pCurrentDoc = this->GetDocument();

 newViewPtr->Create(NULL, NULL, WS_BORDER, CFrameWnd::rectDefault, this,
viewIdx, &context);
 newViewPtr->OnInitialUpdate();
#endif

 if(curViewPtr != NULL) {
  curViewPtr->ShowWindow(SW_HIDE);
  curViewPtr->SetParent(NULL);
  curViewPtr->SetOwner(NULL);
 }

 newViewPtr->SetParent(this);
 newViewPtr->EnableWindow(TRUE);
 newViewPtr->SetOwner(this);
 newViewPtr->SetActiveWindow();
 newViewPtr->SetWindowPos(this, 50, 50, 600, 400, SWP_DRAWFRAME);

 newViewPtr->ShowWindow(SW_SHOW);

 mCurrentView = newViewPtr;

 ((CFrameWnd*)GetTopWindow())->RedrawWindow();

Quote:
}

but this dosent work and I dont get the sub view I expect to see.

I said it is "hard to do in MFC" because I dont see a single method that
facilates adding/removing a sub view to a given view.  Well designed
frameworks I have used like Java Swing, PowerPlant and MacApp has methods to
facilatate this very easily.  MFC looks like a nice improvement over using
WinAPIs, but it sucks big time compared to real frameworks.  The amout of
hacking that you have to do in MFC is mind boggling.

Anyways, thanks for the info.  I guess I have to learn MFC well enough to
tackle this problem.

Thanks,
Mahesh.


Quote:

> > I'm trying to create a Simple Views that a sub classes of CView that I
can
> > swap in and out as I needed.  Unfortunately, MFC View creation looks
like a
> > real mess.  All I want to do is something like this:

> > myView = new MyView(parentView);
> > parentView.place(myView, 0, 0);
> > myView.setEnabled(true);
> > myView.setVisible(true);

> > Why is it so hard in to do in MFC?  Any body has any clean solutions to
this
> > approach?
> > TIA

> The view is a child of the CMainFrame window, which makes it fit within
> the available area after toolbars, status bars, etc. are positioned.  A
> lot of code under the hood supports this, and supports dynamic creation
> of the view and linking it to the document.  That's why it's "so hard" -
> you have to learn how MFC needs it to be done.

> See the VSWAP32 sample code for the view swapping technique.

> --
> Scott McPhillips [VC++ MVP]



Mon, 15 Mar 2004 09:17:05 GMT  
 Help creating simple view swapping

Quote:

> Thanks Scott.  I'm looking at the example, but its only swapping views at
> the top level, which seems relatively easy.  What I'm trying to do is to
> have a top level view which will swap other sub views in and out (like in
> MSN Messenger) based on user cliks on icons.  I need it to happen on a
> Single Doc document (not Dialog based, because thats what the requirements
> are).

> I tried creating a SwapView() method and tried this after creating the view
> <code that doesn't work snipped>
> but this dosent work and I dont get the sub view I expect to see.

> I said it is "hard to do in MFC" because I dont see a single method that
> facilates adding/removing a sub view to a given view.  Well designed
> frameworks I have used like Java Swing, PowerPlant and MacApp has methods to
> facilatate this very easily.  MFC looks like a nice improvement over using
> WinAPIs, but it sucks big time compared to real frameworks.  The amout of
> hacking that you have to do in MFC is mind boggling.

> Anyways, thanks for the info.  I guess I have to learn MFC well enough to
> tackle this problem.

> Thanks,
> Mahesh.

You're right, a view-in-a-view is not easy.  Two suggestions:

If you would be happy putting dialog templates on your top-level view
just create several modeless dialogs and turn off their titlebar and (if
you like) border styles.  This swaps controls - the same trick is used
in the tabbed dialog windows to swap the controls when a tab is clicked.

If you want a real view in a view copy and adapt the code from
CSplitterWnd CreateView and the functions it calls.  Once you get it
appearing you will find that the parent window must manage the view's
size/position in the parent's OnSize handler (something CFrameWnd
normally does for views).  This gives excellent results, but there's no
documentation to guide you.  Good hacking!

--
Scott McPhillips [VC++ MVP]



Mon, 15 Mar 2004 10:56:41 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Help: Is it possible to swap views

2. Help - Using Create to dynamiclly create a CFormView view

3. SIMPLE: Byte Swapping on Signed & Unsigned Shorts

4. Swapping Views programmatically

5. Swapping Views

6. HELP: Removing DOC/VIEW (aka want simpler MFC CWinApp CMainFrame only app)

7. Retaining swapped view control states...

8. Swapping Views in a pane of CSplitterWnd

9. Dynamically swapping views in an SDI app

10. Swapping Views

11. Retaining swapped view control states...

12. Swap pointers to swap data and how?

 

 
Powered by phpBB® Forum Software