View in MFC SDI Programm ändern.

steff

Grünschnabel
Hallo.

Ich bin Neuling auf dem Gebiet von MFC und hab ma ne Frage.

Ich habe mir mit dem Assistenten eine SDI Anwendung erstellt und für CView den Klassentyp FormView gewählt.
Ich benutze das Dok/View Prinzip. Habe mir dazu Variablen in der CDoc angelegt.
Jetzt möchte ich dass man die CDoc mit verschiedenen Forms bearbeiten kann.

Deshalb habe ich einen weiteren Dialog im Resorceneditor erstellt.
Wie kann ich jetzt den aktuellen Dialog im View mit dem anderen tauschen.
Zum Beispiele über eine Menü oder Button Klasse?
 
Suche in der MSDN unter CDocument::AddView. Da gibt es als Beispiel-Code, wie man bei einer SDI-Anwendung einen View gegen einen anderen austauscht. Hier der Code aus der MSDN kopiert:

Code:
// The following example toggles two views in an SDI (single document
// interface) frame window. A design decision must be made as to
// whether to leave the inactive view connected to the document,
// such that the inactive view continues to receive OnUpdate
// notifications from the document. It is usually desirable to
// keep the inactive view continuously in sync with the document, even 
// though it is inactive. However, doing so incurs a performance cost,
// as well as the programming cost of implementing OnUpdate hints.
// It may be less expensive, in terms of performance and/or programming,
// to re-sync the inactive view with the document only with it is 
// reactivated. This example illustrates this latter approach, by 
// reconnecting the newly active view and disconnecting the newly 
// inactive view, via calls to CDocument::AddView and RemoveView.

BOOL CMainFrame::OnViewChange(UINT nCmdID)
// There is an ON_COMMAND_RANGE message map entry associated with
// OnViewChange:
// ON_COMMAND_RANGE( ID_VIEW_VIEW1, ID_VIEW_VIEW2, OnViewChange)
{
 CView* pViewAdd;
 CView* pViewRemove;
 CDocument* pDoc = GetActiveDocument();

 if((nCmdID == ID_VIEW_VIEW1) && (m_currentView == 1))
    return;
 if((nCmdID == ID_VIEW_VIEW2) && (m_currentView == 2))
   return;

 if (nCmdID == ID_VIEW_VIEW2)
 {
  if (m_pView2 == NULL)
  {
   m_pView1 = GetActiveView();
   m_pView2 = new CMyView2;

//Note that if OnSize has been overridden in CMyView2 
//and GetDocument() is used in this override it can 
//cause assertions and, if the assertions are ignored,
//cause access violation.
  
   m_pView2->Create(NULL, NULL, AFX_WS_DEFAULT_VIEW,
      rectDefault, this, AFX_IDW_PANE_FIRST + 1, NULL);
  }
   pViewAdd = m_pView2;
   pViewRemove = m_pView1;
   m_currentView= 2;
 }
 else
 {
  pViewAdd = m_pView1;
  pViewRemove = m_pView2;
  m_currentView= 1;
 }
     
// Set the child i.d. of the active view to AFX_IDW_PANE_FIRST,
// so that CFrameWnd::RecalcLayout will allocate to this 
// "first pane" that portion of   the frame window's client area 
// not allocated to control   bars.  Set the child i.d. of the 
// other view to anything other than AFX_IDW_PANE_FIRST; this
// examples switches the child id's of the two views.

 int nSwitchChildID = pViewAdd->GetDlgCtrlID();
 pViewAdd->SetDlgCtrlID(AFX_IDW_PANE_FIRST);
 pViewRemove->SetDlgCtrlID(nSwitchChildID);

 // Show the newly active view and hide the inactive view.

 pViewAdd->ShowWindow(SW_SHOW);
 pViewRemove->ShowWindow(SW_HIDE);

 // Connect the newly active view to the document, and
 // disconnect the inactive view.
 pDoc->AddView(pViewAdd);
 pDoc->RemoveView(pViewRemove);

 SetActiveView(pViewAdd);
 RecalcLayout();
}
 
Bekomm di Funktion nicht zum laufen

Hab die funktion als Memberfunktion im ClassView hinzugefügt und dann den Code kopiert hat mir VC++ 6 aber mit folgenden Fehlermeldungen Quitiert.


___________________________________________________________
E:\Media\Programmierung\MasterTools\MainFrm.cpp(115) : error C2065: 'ID_VIEW_VIEW1' : undeclared identifier
E:\Media\Programmierung\MasterTools\MainFrm.cpp(115) : warning C4018: '==' : signed/unsigned mismatch
E:\Media\Programmierung\MasterTools\MainFrm.cpp(115) : error C2065: 'm_currentView' : undeclared identifier
E:\Media\Programmierung\MasterTools\MainFrm.cpp(116) : error C2561: 'OnViewChange' : function must return a value
e:\media\programmierung\mastertools\mainfrm.h(33) : see declaration of 'OnViewChange'
E:\Media\Programmierung\MasterTools\MainFrm.cpp(117) : error C2065: 'ID_VIEW_VIEW2' : undeclared identifier
E:\Media\Programmierung\MasterTools\MainFrm.cpp(117) : warning C4018: '==' : signed/unsigned mismatch
E:\Media\Programmierung\MasterTools\MainFrm.cpp(118) : error C2561: 'OnViewChange' : function must return a value
e:\media\programmierung\mastertools\mainfrm.h(33) : see declaration of 'OnViewChange'
E:\Media\Programmierung\MasterTools\MainFrm.cpp(120) : warning C4018: '==' : signed/unsigned mismatch
E:\Media\Programmierung\MasterTools\MainFrm.cpp(122) : error C2065: 'm_pView2' : undeclared identifier
E:\Media\Programmierung\MasterTools\MainFrm.cpp(124) : error C2065: 'm_pView1' : undeclared identifier
E:\Media\Programmierung\MasterTools\MainFrm.cpp(124) : error C2440: '=' : cannot convert from 'class CView *' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
E:\Media\Programmierung\MasterTools\MainFrm.cpp(125) : error C2061: syntax error : identifier 'CMyView2'
E:\Media\Programmierung\MasterTools\MainFrm.cpp(130) : error C2227: left of '->Create' must point to class/struct/union
E:\Media\Programmierung\MasterTools\MainFrm.cpp(131) : error C2143: syntax error : missing ';' before '}'
.
.
. hier wiederholt sich die Letzte Fehlermeldung!so 100 mal.
.
.
.
E:\Media\Programmierung\MasterTools\MainFrm.cpp(131) : error C2143: syntax error : missing ';' before '}'
E:\Media\Programmierung\MasterTools\MainFrm.cpp(131) : fatal error C1003: error count exceeds 100; stopping compilation
Error executing cl.exe.

MasterTools.exe - 102 error(s), 3 warning(s)
_______________________________________________________________
Was habe ich falsch gemacht und was muss ich der Funktion übergeben wenn sie dann funktioniert

Danke im voraus Steff.
 
Yo, genau. Das Beispiel war auch nur ein Beispiel.

Die ID_VIEW_VIEWx sind die Dialoge, die du im Resource Editor zusammengebastelt hast (einen hast du ja schon). Die beiden m_pViewX sind Membervariablen, die die beiden View-Klassen beinhalten. Es ging auch eher um's Prinzip:

Mit CDocument::AddView kannst du einen zusätzlichen View ans Document hängen, mit CDocument::RemoveView kannst du einen View vom Document entfernen. Die als Beispiel aufgeführte Funktion macht nichts anderes, als jeweils einen View neu zu erstellen, ihn mit AddView einzuklinken, den alten mit RemoveView rauszunehmen und dann einige Update/Neuzeichen-Routinen aufzurufen. Was mir grade auffällt, dass hier der alte View gerade NICHT deleted wird, das sollte man dann evtl.auch noch einfügen (ganz zum Schluss, damit auch alle mitbekommen haben, dass es einen neuen View gibt).
 
Hallo zusammen,
nach langem hin und her habe ich es geschaff, neine Views zu tauschen.
Auch die Toolbar zu tauschen habe ich geschafft.
Aber wie kann ich das menü austauschen oder geht das nicht?
Ich will ja dann zu jeder View ein anderes menü einblenden!
 
Damit habe ich auch schon mal rumgespielt.

Ich glaube, es reicht ein MDIActivate-Aufruf (Member von Mainframe) mit dem neuen View. Das sollte das intern regeln.
 
Irgendwo fliegt ein CMainFrame rum. Davon wird eine Instanz erzeugt.

Deine App enthält eine Membervariable m_pMainWnd. Diese ist dummerweise nur ein CWnd-Typ, dieser wird aber in CDeineApp::InitInstance auf eine CMainFrame-Instanz gesetzt.

Von ausserhalb der App kann man jetzt sowas hässliches machen, wie das:

( (CMainFrame*)theApp.m_pMainWnd ) ->MDIActivate( pMeinView );


Ist hässlich, wer unbedingt will, kann auch die IsKindOf von MFC benutzen, aber da die einzige Zuweisung in InitInstance steht, ist das CWnd sicher ein CMainFrame.
 
Zurück