/////////////////////////////////////////////////////////

- CWinApp참조

CWinApp* AfxGetApp();

 

-메인 프레임 윈도우 참조

CWnd* AfxGetMainWnd();

/////////////////////////////////////////////////////////

 

/////////////////////////////////////////////////////////

-뷰에서 토큐먼트 참조

CDocument* CView::GetDocument();

 

-뷰에서 그 뷰를 둘러싸고 있는 프레임 윈도우 참조

CFrameWnd* CWnd::GetParentFrame();

/////////////////////////////////////////////////////////

 

/////////////////////////////////////////////////////////

-도큐먼트에서 뷰 참조

POSITION pos = GetFirstViewPosition();

while(pos != NULL)

{

    CView* pView = (CView *)GetNextView(pos);

    if(pView->IsKindOf(RUNTIME_CLASS(CTestView)))
    {

        pView->UpdateWindow();

    }

}

 

-도큐먼트에서 프레임 윈도우 참조

단일 도큐먼트 : AfxGetMainWnd();

다중 도큐먼트 : 도큐먼트에서 뷰를 얻고, 그 뷰에서 자신을 포함하는 프레임 윈도우를 얻음

/////////////////////////////////////////////////////////

 

/////////////////////////////////////////////////////////

-프레임 윈도우에서 뷰 참조

CView* CFrameWnd::GetActiveView();

 

-프레임 윈도우에서 도큐먼트 참조

virtual CDocument* CFrameWnd::GetActiveDocument();

/////////////////////////////////////////////////////////

 

/////////////////////////////////////////////////////////////////////////////////////////////////

//예제 : 폼뷰에 올라와 있는 리스트박스에 등록되어있는 Text를 얻어와 다른 뷰에 출력하는 방법들

void CTestFormView::OnSelchangeList()
{
    CTestDoc* pDoc = GetDocument();
    int index;

 

    index = m_ctrList.GetCurSel();

 

    m_ctrList.GetText( index, pDoc->str );

 

    /////////////////////////////////////////////////////////////////////////
    //방법 1(MDI, SDI 모두가능)
    CMainFrame* pFrame = (CMainFrame *)AfxGetMainWnd();
    CMFC0604Doc* pDoc2 = (CMFC0604Doc *)pFrame->GetActiveDocument();
 
    POSITION pos = pDoc2->GetFirstViewPosition();

    CMFC0604View *pView;
    pView = (CMFC0604View *)pDoc2->GetNextView(pos); 
    pView = (CMFC0604View *)pDoc2->GetNextView(pos); 

    pView->Invalidate();
 
    /////////////////////////////////////////////////////////////////////////
    //방법 2(MDI, SDI 모두가능)
    CMFC0604Doc* pDoc2 = (CMFC0604Doc *)(((CMainFrame *)AfxGetMainWnd())->GetActiveDocument() );
 
    POSITION pos = pDoc2->GetFirstViewPosition();

    CMFC0604View *pView;
    pView = (CMFC0604View *)pDoc2->GetNextView(pos); 
    pView = (CMFC0604View *)pDoc2->GetNextView(pos); 

    pView->Invalidate();

 

    /////////////////////////////////////////////////////////////////////////
    //방법 3(MDI, SDI 모두가능)
    CMFC0604Doc* pDoc2 = (CMFC0604Doc *)((CMainFrame *)AfxGetApp()->m_pMainWnd)->GetActiveDocument();
 
    POSITION pos = pDoc2->GetFirstViewPosition();

    CMFC0604View *pView;
    pView = (CMFC0604View *)pDoc2->GetNextView(pos); 
    pView = (CMFC0604View *)pDoc2->GetNextView(pos); 

    pView->Invalidate();

 

    /////////////////////////////////////////////////////////////////////////
    //방법 4(MDI, SDI 모두가능)

    CMFC0604Doc* pDoc2 = (CMFC0604Doc *)((CMainFrame *)AfxGetApp()->m_pMainWnd)->GetActiveDocument();
 
    POSITION pos = pDoc2->GetFirstViewPosition();

    CMFC0604View *pView;
    pView = (CMFC0604View *)pDoc2->GetNextView(pos); 
    pView = (CMFC0604View *)pDoc2->GetNextView(pos); 

    pView->Invalidate();

 

    /////////////////////////////////////////////////////////////////////////
    //방법 5(MDI, SDI 모두가능) <-범용적인 방법
    CDocument* pDoc2;
    CView *pView, *pView2;

    POSITION pos;

    pDoc2 = (CDocument *)((CMainFrame *)AfxGetMainWnd())->GetActiveDocument();

    pos = pDoc2->GetFirstViewPosition();

    pView = pDoc2->GetNextView(pos);
    pView2 = pDoc2->GetNextView(pos);

    pView->Invalidate();
    pView2->Invalidate();
 
    /////////////////////////////////////////////////////////////////////////
    //방법6
    CView* pView;

    POSITION pos;

    pos = pDoc->GetFirstViewPosition();

    while( pos != NULL )
    {
        pView = (CView *)pDoc->GetNextView(pos);

        if( pView->IsKindOf(RUNTIME_CLASS(CMFC0604View)) )
        {
            pView->Invalidate();
        }
    }

}

 

 

 

/***********************************************************************************/

/*메인프레임에서 각 뷰에 접근하는 예제

/*기능 : 타이머를 설정하여 매 타임마다 각 뷰에 텍스트를 찍는 예제

/***********************************************************************************/

BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT /*lpcs*/, CCreateContext* pContext)
{
    /*return m_wndSplitter.Create(this,
     2, 2,               // TODO: adjust the number of rows, columns
     CSize(10, 10),      // TODO: adjust the minimum pane size
     pContext);*/

 

    if (!m_wndSplitter.CreateStatic(this, 2, 2))
    {
        TRACE0("Fail to create splitter window.\n");
        return FALSE;    // failed to create
    }
 
    m_wndSplitter.CreateView(0, 0, RUNTIME_CLASS(CMFC0606View), CSize(100, 100), pContext);
    m_wndSplitter.CreateView(0, 1, RUNTIME_CLASS(CTestView2), CSize(100, 100), pContext);
    m_wndSplitter.CreateView(1, 0, RUNTIME_CLASS(CTestView3), CSize(100, 100), pContext);
    m_wndSplitter.CreateView(1, 1, RUNTIME_CLASS(CTestView4), CSize(100, 100), pContext);
  
    SetActiveView( (CView*)m_wndSplitter.GetPane(0, 0) ); //처음 포커스를 줄 뷰를 선택한다.

    m_wndSplitter.SetRowInfo(0, 300, 1);
    m_wndSplitter.SetColumnInfo(0, 300, 1);  
   
    return TRUE;
}

 

//////////////////////////////////////////////////////////////////////////////////////////////////

 

void CMainFrame::OnDestroy()
{
    CFrameWnd::OnDestroy();
 
    KillTimer( 0 );
}

 

//////////////////////////////////////////////////////////////////////////////////////////////////

 

void CMainFrame::OnTimer(UINT nIDEvent)
{
    static CView* pOldView = NULL;
    static int num = 1;

 

    if( pOldView ) pOldView->Invalidate();

 

    CDocument* pDoc;
    CView* pView;

    CString str;

    POSITION pos;
    CDC *pDC;

 

    pDoc = GetActiveDocument();
    pos = pDoc->GetFirstViewPosition();

 

    for( int i = 0; i < num; i++ )
    {
        pView = pDoc->GetNextView(pos);
    }

 

    pDC = pView->GetDC();  
    str.Format( "Num : %d", num );
    pDC->TextOut( 50, 50, str );
 
    pOldView = pView;

    num++;
    if( num > 4 ) num = 1;

    CFrameWnd::OnTimer(nIDEvent);
}

'Programming > MFC' 카테고리의 다른 글

현재 시간 얻어오기  (0) 2010.11.16
Editbox 컨트롤 사용방법  (0) 2010.11.16
뷰 및 객체간의 상호참조  (0) 2010.11.16
모달리스 다이얼로그  (0) 2010.11.16
컨트롤에서 데이터값 가져오기, 삽입하기  (0) 2010.11.16

+ Recent posts