Programming/MFC

DOC와 View 편하게 접근하기

_Erato 2006. 3. 28. 19:53
출처 interigent and interaction | 푸르댕댕
원문 http://blog.naver.com/love7yw/130001504066



단지 NULL 이 리턴 되었을 경우는 생성이 안되어 있거나, 이미 소멸되어 없는 경우

class CPEAnalyzerDoc : public CDocument

{

public:

    static CPEAnalyzerDoc * GetDoc();

    CPEAnalyzerDoc();

    virtual ~CPEAnalyzerDoc();

   

    DECLARE_DYNCREATE(CPEAnalyzerDoc)

private:

//

};


// SDI 인경우.

CPEAnalyzerDoc * CPEAnalyzerDoc::GetDoc()

{

    CFrameWnd * pFrame = (CFrameWnd *)(AfxGetApp()->m_pMainWnd);

    return (CPEAnalyzerDoc *) pFrame->GetActiveDocument();

}


// MDI 인 경우.

/*

CPEAnalyzerDoc * CPEAnalyzerDoc::GetDoc()

{

    CMDIChildWnd * pChild =

        ((CMDIFrameWnd*)(AfxGetApp()->m_pMainWnd))->MDIGetActive();

    if ( !pChild )

        return NULL;

    CDocument * pDoc = pChild->GetActiveDocument();

    if ( !pDoc )

        return NULL;

    // Fail if doc is of wrong kind

    if ( ! pDoc->IsKindOf( RUNTIME_CLASS(CPEAnalyzerDoc) ) )

        return NULL;

    return (CPEAnalyzerDoc *) pDoc;

}*/


그러면 언제 어디서나 DOC 호출은..


CPEAnalyzerDoc* pDoc = CPEAnalyzerDoc::GetDoc();

if(pDoc)

{

    //......

 }

위와 같이 쓰면 됩니다.


뷰에서도 똑 같은데요..


Class CPEAnalyzerView : public CFormView

{

    DECLARE_DYNCREATE(CPEAnalyzerView)

public:

    CPEAnalyzerView();

    virtual ~CPEAnalyzerView();

    static CPEAnalyzerView * GetView();

//

};



// SDI View implementation file

CPEAnalyzerView * CPEAnalyzerView::GetView()

{

    CFrameWnd * pFrame = (CFrameWnd *)(AfxGetApp()->m_pMainWnd);

    CView * pView = pFrame->GetActiveView();

    if ( !pView )

        return NULL;

    // Fail if view is of wrong kind

    // (this could occur with splitter windows, or additional

    // views on a single document

    if ( ! pView->IsKindOf( RUNTIME_CLASS(CPEAnalyzerView) ) )

        return NULL;

    return (CPEAnalyzerView *) pView;

}

// MDI view implementation file

/*

CPEAnalyzerView * CPEAnalyzerView::GetView()

{

    CMDIChildWnd * pChild =

        ((CMDIFrameWnd*)(AfxGetApp()->m_pMainWnd))->MDIGetActive();

    if ( !pChild )

        return NULL;

    CView * pView = pChild->GetActiveView();

    if ( !pView )

        return NULL;

    // Fail if view is of wrong kind

    if ( ! pView->IsKindOf( RUNTIME_CLASS(CPEAnalyzerView) ) )

        return NULL;

    return (CPEAnalyzerView *) pView;

}

*/


가져다 쓰면 ..


    CPEAnalyzerView * pMyview = CPEAnalyzerView::GetView();

    if(pMyview == NULL)

        return FALSE;

이렇게 하시면 됩니다.


아주 사소한 코드지만 , 이렇게 하면 짜증나는 작업이 많이 줄어들것으로 보입니다.

참고로 MDI 는 지금 활성화 되어있는( Actived) View,Doc를 호출하는 코드입니다.


출처 - 데브피아-