Click here to Skip to main content
15,928,281 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralCListCtrl Pin
munguia21-May-04 1:22
munguia21-May-04 1:22 
GeneralRe: CListCtrl Pin
David Crow21-May-04 3:00
David Crow21-May-04 3:00 
GeneralRe: CListCtrl Pin
Member 52815521-May-04 3:12
Member 52815521-May-04 3:12 
GeneralRe: CListCtrl Pin
Andrew Quinn AUS21-May-04 3:19
Andrew Quinn AUS21-May-04 3:19 
GeneralDifferent views in SDI Pin
dart1321-May-04 0:36
dart1321-May-04 0:36 
GeneralRe: Different views in SDI Pin
*Dreamz21-May-04 0:46
*Dreamz21-May-04 0:46 
GeneralRe: Different views in SDI Pin
dart1321-May-04 2:27
dart1321-May-04 2:27 
GeneralRe: Different views in SDI Pin
Andrew Quinn AUS21-May-04 0:55
Andrew Quinn AUS21-May-04 0:55 
Hi there,

Create your first view as normal:

CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
    IDR_MAINFRAME,
    RUNTIME_CLASS(CMyDoc),
    RUNTIME_CLASS(CMainFrame),       // main SDI frame window
    RUNTIME_CLASS(CForm1));
AddDocTemplate(pDocTemplate);


Then, still in InitInstance, store an array (or obarray) to hold the other views...

CView* pActiveView = ((CFrameWnd*) m_pMainWnd)->GetActiveView();

m_pViews[0] = pActiveView;
m_pViews[1] = (CView*) new CForm2;
m_pViews[2] = (CView*) new CForm3;

CDocument* pCurrentDoc = ((CFrameWnd*) m_pMainWnd)->GetActiveDocument();

// Initialize a CCreateContext to point to the active document.
// With this context, the new view is added to the document
// when the view is created in CView::OnCreate().
CCreateContext newContext;
newContext.m_pNewViewClass = NULL;
newContext.m_pNewDocTemplate = NULL;
newContext.m_pLastView = NULL;
newContext.m_pCurrentFrame = NULL;
newContext.m_pCurrentDoc = pCurrentDoc;

// The ID of the initial active view is AFX_IDW_PANE_FIRST.
// Incrementing this value by one for additional views works
// in the standard document/view case but the technique cannot
// be extended for the CSplitterWnd case.
UINT viewID[3];
viewID[1] = AFX_IDW_PANE_FIRST + 1;
viewID[2] = AFX_IDW_PANE_FIRST + 2;
CRect rect(0, 0, 0, 0); // gets resized later

// Need to cast pointers to have correct Create functions called
for ( int nView=1; nView<NUMVIEWS; nView++ )
{
    // Create the new view. In this example, the view persists for
    // the life of the application. The application automatically
    // deletes the view when the application is closed.
    m_pViews[nView]->Create(NULL, NULL,
            (AFX_WS_DEFAULT_VIEW & ~WS_VISIBLE),
        // views are created with the style of AFX_WS_DEFAULT_VIEW
        // In MFC 4.0, this is (WS_BORDER | WS_VISIBLE | WS_CHILD)
                    rect, m_pMainWnd,
            viewID[nView], &newContext);
}

// When a document template creates a view, the WM_INITIALUPDATE
// message is sent automatically. However, this code must
// explicitly send the message to the extra views as follows.
((CForm2*)m_pViews[1])->OnInitialUpdate();
((CForm3*)m_pViews[2])->OnInitialUpdate();


Here's a function to help you switch dynamically between the views...

CView* CMyApp::SwitchView( UINT nIndex, BOOL bSaveData /*=TRUE*/ )
{
    ASSERT( nIndex >=0 && nIndex < NUMVIEWS );
    
    CView* pNewView = m_pViews[nIndex];
    
    CView* pActiveView = ((CFrameWnd*) m_pMainWnd)->GetActiveView();
    
    if ( !pActiveView )    // No currently active view
        return NULL;
    
    if ( pNewView == pActiveView )    // Already there
        return pActiveView;
        
    // Update Doc's data if needed
    // Don't change view if data valiation fails
    if(bSaveData)
    {
        // this will give you the ability to save the contents of the Form to the document
        // or wherever - implement SaveActiveViewsData() however you want...
	if  (!SaveActiveViewsData() )
	{
  	   return pActiveView;
	}
    }

    // exchange view window ID's so RecalcLayout() works
    UINT temp = ::GetWindowLong(pActiveView->m_hWnd, GWL_ID);
    ::SetWindowLong(pActiveView->m_hWnd, GWL_ID,::GetWindowLong(pNewView->m_hWnd, GWL_ID));
    ::SetWindowLong(pNewView->m_hWnd, GWL_ID, temp);

    // Display and update the new current view - hide the old one    
    pActiveView->ShowWindow(SW_HIDE);
    ((CFormView*)pNewView)->ResizeParentToFit(FALSE);
    ((CFrameWnd*) m_pMainWnd)->SetActiveView(pNewView);
    pNewView->ShowWindow(SW_SHOW);

    return pActiveView;
}


Any questions, I'll be happy to help.

Hope this helps,
Andy
GeneralRe: Different views in SDI Pin
dart1321-May-04 2:25
dart1321-May-04 2:25 
GeneralRe: Different views in SDI Pin
dart1326-May-04 10:52
dart1326-May-04 10:52 
GeneralRe: Different views in SDI Pin
*Dreamz21-May-04 1:33
*Dreamz21-May-04 1:33 
GeneralLooking for Skilled VC++ Coder, IE Toolbar Developer Pin
Ajay Chadha21-May-04 0:25
Ajay Chadha21-May-04 0:25 
Generalmouse click in tab control Pin
treetops21-May-04 0:24
treetops21-May-04 0:24 
GeneralImporting CSV files Pin
si_6921-May-04 0:01
si_6921-May-04 0:01 
GeneralOutlook Express plug-in Pin
robinfinity20-May-04 23:37
robinfinity20-May-04 23:37 
GeneralRe: Outlook Express plug-in Pin
ravjak21-May-04 2:56
ravjak21-May-04 2:56 
GeneralRe: Outlook Express plug-in Pin
robinfinity21-May-04 3:11
robinfinity21-May-04 3:11 
GeneralRe: Long Wait for a Mutex Pin
sweep12320-May-04 23:22
sweep12320-May-04 23:22 
GeneralRe: Long Wait for a Mutex Pin
valikac21-May-04 7:01
valikac21-May-04 7:01 
GeneralRe: Long Wait for a Mutex Pin
Grahamfff21-May-04 9:10
Grahamfff21-May-04 9:10 
GeneralI need to browse a folder only for rtf files Pin
Filomela20-May-04 23:20
Filomela20-May-04 23:20 
GeneralRe: I need to browse a folder only for rtf files Pin
*Dreamz21-May-04 0:28
*Dreamz21-May-04 0:28 
GeneralRe: I need to browse a folder only for rtf files Pin
Branislav21-May-04 1:53
Branislav21-May-04 1:53 
GeneralRe: I need to browse a folder only for rtf files Pin
Filomela21-May-04 21:24
Filomela21-May-04 21:24 
Generalconstant expression in case Pin
Ernesto D.20-May-04 22:46
Ernesto D.20-May-04 22:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.