애플리케이션의 이전 위치를 보존하려면
애플리케이션의 셋팅을 저장하려면 윈도우 레지스트리를 이용하면 됩니다.
이를 지원하기 위해서 CWinApp 클래스는 SetRegistryKey()와 WriteProfileInt(),
WriteProfileString(), GetProfileInt(), GetProfileString()을 제공합니다.
SetRegistryKey()의 원형은 다음과 같습니다.
void SetRegistryKey(LPCTSTR lpszRegistryKey);
일반적으로 lpszRegistryKey는 애플리케이션을 제작한 회사명입니다.
애플리케이션 위저드를 이용해 코드를 생성했다면 기본적으로 다음과 같이
코딩됩니다.
BOOL CAboutApp::InitInstance() {
// 생략...
// Change the registry key under which our settings are stored.
// You should modify this string to be something appropriate
// such as the name of your company or organization.
SetRegistryKey(_T("Local AppWizard-Generated Applications"));
// 생략...
}
이 함수를 호출하면 자동으로 MRU(Most Recently Used) List가 저장됩니다.
레지스트리에 저장되는 형식은 다음과 같습니다.
HKEY_CURRENT_USERSoftware
애플리케이션이 위치와 크기를 보관하고 애플리케이션 시작 시 보관된
애플리케이션의 크기와 위치를 읽어오는 작업을 하기에 가장 좋은 부분은
CMainFrame의 OnClose()와 OnShowWindow()입니다.
다음 예는 CFameWnd의 OnClose()와 OnShowWindow()를 오버라이드한
것으로 애플리케이션의 크기와 위치를 기억해 둡니다.
void CMainFrame::OnClose() {
// 현재 메인 윈도우의 위치 정보를 얻어온다.
WINDOWPLACEMENT wp;
GetWindowPlacement(&wp);
// 현재 메인 윈도우의 위치와 크기를 레지스트리에 쓴다.
AfxGetApp()->WriteProfileInt("Initialization",
"VertPos",
wp.rcNormalPosition.top);
AfxGetApp()->WriteProfileInt("Initialization",
"HorizPos",
wp.rcNormalPosition.left);
AfxGetApp()->WriteProfileInt("Initialization",
"Height",
wp.rcNormalPosition.bottom - wp.rcNormalPosition.top);
AfxGetApp()->WriteProfileInt("Initialization",
"Width",
wp.rcNormalPosition.right - wp.rcNormalPosition.left);
CFrameWnd::OnClose();
}
void CMainFrame::OnShowWindow(BOOL bShow, UINT nStatus) {
CFrameWnd::OnShowWindow(bShow, nStatus);
// 레지스트리에서 이전에 저장된 프레임 윈도우의 크기와 위치를
// 얻어 와서 프레임 윈도우의 크기를 조정한다.
int iVert =
AfxGetApp()->GetProfileInt("Initialization","VertPos",0);
int iHoriz =
AfxGetApp()->GetProfileInt("Initialization","HorizPos",0);
int Width =
AfxGetApp()->GetProfileInt("Initialization","Width",400);;
int Height =
AfxGetApp()->GetProfileInt("Initialization","Height",280);
// adjust the application's frame size and position
SetWindowPos(NULL,iHoriz,iVert,Width,Height, NULL);
}