영상 처리 프로그램을 작성하다보면, 영상 처리 결과 이미지를 새 창으로 띄우는 경우가 많다. 여러개의 영상에 대하여 테스트하다보면 프로그램 내에 너무 많은 창이 떠있어서 지저분해보이는 경우가 있다. 이 때, '모든 창 닫기' 같은 메뉴가 있으면 유용하다.

일반적으로 창(Window) 메뉴의 서브 메뉴로 모든 문서 닫기(Close All), ID_WINDOW_CLOSEALL 이라는 ID로 메뉴 아이템을 만들고, 이에 해당하는 이벤트 처리기를 CxxxApp 클래스에 만든다. 그리고, CDocTemplate 클래스의 멤버 함수인 CloseAllDocuments 함수를 호출하면 되는데, 자세한 코드는 아래를 참고하라.

void CxxxApp::OnWindowCloseall()
{
POSITION pos;
CDocTemplate* pTemplate;

pos = GetFirstDocTemplatePosition();
while( pos != NULL )
{
pTemplate = GetNextDocTemplate(pos);
pTemplate->CloseAllDocuments(FALSE);
}
}

위 코드에 대해 간단히 설명하자면, App 클래스에 연결된 모든 도큐먼트 템플릿의 포인터를 받아와서 CloseAllDocuments 함수를 호출하고 있다. while 루프를 사용하는 이유는 여러 개의 도큐먼트 템플릿을 사용하는 프로그램을 염두에 둔 것이다.

참고로, CxxxApp 클래스의 InitInstance() 함수 내 AddDocTemplate 하는 부분에서 도큐먼트 클래스가 지역변수로 설정되어 사용되는데, 이를 멤버 변수로 바꾸면 편리하다. 예를 들어,

[CxxxApp.h 파일]
CDocTemplate* m_pDocTemplate;

[CxxxApp.cpp 파일]
m_pDocTemplate = new CMultiDocTemplate(IDR_XXX_TYPE,
RUNTIME_CLASS(CxxxDoc),
RUNTIME_CLASS(CChildFrame), // 사용자 지정 MDI 자식 프레임입니다.
RUNTIME_CLASS(CxxxView));
if( !m_pDocTemplate)
return FALSE;
AddDocTemplate(m_pDocTemplate);

형태로 사용한다면, 위 OnWindowCloseall 함수를 다음과 같이 간략하게 바꿀 수 있다.

void CxxxApp::OnWindowCloseall()
{
pDocTemplate->CloseAllDocuments(FALSE);
}

만약 도큐먼트 템플릿이 여러 개라면 각각의 도큐먼트 템플릿을 멤버 변수로 만들어서, 각각 CloseAllDocuments 함수를 호출해주어야 한다.

아래는 MSDN에서 설명하고 있는 CloseAllDocuments 함수에 대한 내용이다.

---------------------------------------------------------------------------------------

Call this member function to close all open documents.

virtual void CloseAllDocuments(
BOOL bEndSession
);

Parameters
bEndSession
Specifies whether or not the session is being ended. It is TRUE if the session is being ended; otherwise FALSE.

Remarks
This member function is typically used as part of the File Exit command. The default implementation of this function calls the CDocument::DeleteContents member function to delete the document's data and then closes the frame windows for all the views attached to the document.

Override this function if you want to require the user to perform special cleanup processing before the document is closed. For example, if the document represents a record in a database, you may want to override this function to close the database.

Posted by kkokkal
: