본문 바로가기
C++ 200제/코딩 IT 정보

CFileDialog, 윈도우7과 윈도우XP 호환 소스 (Modal Dialog)

by vicddory 2017. 11. 11.

CFileDialog, 윈도우7과 윈도우XP 호환 소스 (Modal Dialog)


아래는 CFileDialog를 이용한 Modal 소스인데 문제 될 것이 없는 평범한 소스입니다. CFileDialog를 Modal로 여는 것이죠.


1
2
3
4
5
6
7
8
9
10
11
CFileDialog oFileDlg(TRUE, "bmp""*.bmp"
                OFN_FILEMUSTEXIST | OFN_LONGNAMES, "BMP Files"this);
 
oFileDlg.m_ofn.lpstrInitialDir = "C:\\";
 
oFileDlg.DoModal();
 
if(oFileDlg.GetPathName().Compare(""!= 0)
{
...........................
}
cs


그러나, 윈도우7에선 오류를 유발합니다.

그렇다고, 윈도우7이 설치된 모든 PC에서 CFileDialog 오류를 발생시키진 않습니다. (이게 결정적인 문제죠)


MSDN의 OPENFILENAME 구조를 참조해야 합니다.


CFileDialog, 윈도우7과 윈도우XP 호환 소스 (Modal Dialog)[CFileDialog, 윈도우7과 윈도우XP 호환 소스 (Modal Dialog)]


lpstrInitialDir의 주요 특징 중 하나는 초기 디렉토리 설정 부분입니다.


lpstrInitialDir


Type : LPCTSTR

The initial directory. The algorithm for selecting the initial directory varies on different platforms.


Windows 7:

1. If lpstrInitialDir has the same value as was passed the first time the application used an Open or Save As dialog box, the path most recently selected by the user is used as the initial directory.

2. Otherwise, if lpstrFile contains a path, that path is the initial directory.


3. Otherwise, if lpstrInitialDir is not NULL, it specifies the initial directory. (CFileDialog)


4. If lpstrInitialDir is NULL and the current directory contains any files of the specified filter types, the initial directory is the current directory.

5. Otherwise, the initial directory is the personal files directory of the current user.

6. Otherwise, the initial directory is the Desktop folder.


Windows 2000/XP/Vista:

1. If lpstrFile contains a path, that path is the initial directory.

2. Otherwise, lpstrInitialDir specifies the initial directory.


3. Otherwise, if the application has used an Open or Save As dialog box in the past, the path most recently used is selected as the initial directory. However, if an application is not run for a long time, its saved selected path is discarded. (CFileDialog)


4. If lpstrInitialDir is NULL and the current directory contains any files of the specified filter types, the initial directory is the current directory.

5. Otherwise, the initial directory is the personal files directory of the current user.

6. Otherwise, the initial directory is the Desktop folder.


윈도우7로 넘어오면서, XP와는 다르게 가장 최근에 사용했던 경로를 기억한다는 것입니다.


딱 하나 달라졌는데, 이 딱 하나 달라진 부분이 오류를 유발하는 듯 싶습니다.


1
2
3
4
5
6
// FAIL
openDlg.m_ofn.lpstrInitialDir = "C:";
 
// SUCCESS
CString lpstrInitialDir  = _T("C");
openDlg.m_ofn.lpstrInitialDir = lpstrInitialDir.GetBuffer(256);
cs


위의 코드를 보면 둘 다 올바른 코드이나, 윗줄 2번 라인은 에러를 유발합니다.


윈도우7에선 반드시 아래 줄 5, 6번 라인처럼 CFileDialog를 사용해야 합니다.



ps. 국내에선 큰 문제가 없었나... 관련 글을 찾아보기 힘듬.


CFileDialog, 윈도우7과 윈도우XP 호환 소스 (Modal Dialog)

댓글