MFC로 간단하게 이미지 뷰어

Posted at 2008/08/23 14:42 // in Programming // by Daniel

http://drcarter.tistory.com/entry/%ea%b ··· 596%25b4 에서 퍼왔습니다.

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
   
LPCTSTR szFilter = _T("Image Files(*.BMP, *.GIF, *.JPG, *.PNG) | *.BMP;*.GIF;*.JPG;*.PNG | All Files(*.*)|*.*||");
    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.
실행

크리에이티브 커먼즈 라이센스
Creative Commons License