2007년 4월 3일 화요일

CImage를 이용한 Simple Image Viewer

일단은 닥치고 따라 해 보자.

1. MFC Application 생성

    Application Type
        Single document, Document/View architecture support는 uncheck
    User Interface Features
        Thick frame, Initial status bar, System menu(저는 귀찮아서 이것도 삭제) uncheck
    
2. Stdafx.h 파일을 열고 마지막 줄에 다음 추가

    #include "atlimage.h"

3. ChildView.h 파일을 열고 CChildView class 속성에 다음 추가

    CImage image;
    CString sFilename;
    
4. ChildView.cpp 파일을 열고 생성자에 다음 추가

    sFilename = L"";
    
5. Resource view 창에서 Menu 항목의 IDR_MAINFRAME 을 더블 클릭하고 File 메뉴 아래 Open... 메뉴 추가

6. Open... 메뉴 오른 클릭해서 Event handler 추가 선택, CChildView에 추가한다.

7. CChildView::OnFileOpen() (방금 추가한 Event handler) 에 다음 추가

    // Get image file name
    char szFilter[] = "Image Files(*.BMP, *.GIF, *.JPG, *.PNG) | *.BMP;*.GIF;*.JPG;*.PNG | All Files(*.*)|*.*||";
    CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY, szFilter);
    if(IDOK != dlg.DoModal()) return;
    CString sFilename = dlg.GetPathName();

    // Detaches the bitmap from the CImage object and destroys the bitmap if image already loaded
    if ( !image.IsNull() )
        image.Destroy();
    image.Load(sFilename);
    Invalidate();
    UpdateWindow();
    
8. CChildView::OnPaint() 의 //TODO 아래에 에 다음 추가

    // Draw image if a source bitmap is currently loaded
    if ( !image.IsNull() ) {
        image.Draw(dc.m_hDC, 0, 0);
    }
    
9. 여기까지 하면 약간 버그는 있지만 어쨌든 동작하는 ImageViewer가 완성되었다.
    일단 여기까지...
    
    

댓글 없음: