우선 Visual Studio(VS2010 기준으로 작성)를 실행한다.
다음으로 Application Setting 창에서 Console application과 MFC를 선택한다. (그림 참고)
Finish를 선택하고, 자동으로 생성되는 파일에 다음과 같은 코드를 추가한다.
//stdafx.h에 추가 #include <stdio.h> #include <tchar.h> #include <iostream> #include <fstream> #include <cctype> #include <conio.h> #include <iomanip> #include <afxdlgs.h> #include <atlstr.h>
다음으로 cpp 파일에 다음과 같이 추가하면 끝!!
//_tmain 함수에 추가 CString szFileName = ""; CString szFilter = "Text Files(*.txt)|*.txt|All Files(*.*)|*.*"; char ch; cout << "Open File 'y' or 'Y' ? "; cin >> ch; if((ch == 'y') || (ch == 'Y')) { CWnd* pWnd = CWnd::FromHandle(GetForegroundWindow()); CFileDialog dlg(TRUE, _T("*.txt"), _T("*.txt"), NULL, szFilter, pWnd); dlg.m_ofn.Flags |= OFN_FILEMUSTEXIST; dlg.m_ofn.lpstrTitle = _T("Read a text file"); if(dlg.DoModal() == IDOK) { szFileName = dlg.GetPathName(); } } cin.get(); if(szFileName.IsEmpty()) { szFileName = "ReadMe.txt"; } ifstream in(szFileName, ios::in | ios::binary); if(!in) { cout << "Cannot open input file.\n"; cout << "Press any key to Exit!"; while(!_kbhit()) Sleep(2); return 1; }
<실행 화면>
Console application으로 프로젝트를 설정한 이후에 Filedialog가 웬말이냐 라는 사람도 있겠지만, 가끔은 필요할 때도 있다.
※ 추가사항
반드시, 메인함수의 초기에 다음의 초기화 과정을 거쳐야 한다.
HMODULE hModule = ::GetModuleHandle(NULL);
if (hModule == NULL) return 1;
if (!AfxWinInit(hModule, NULL, ::GetCommandLine(), 0)) return 1;
댓글 없음:
댓글 쓰기