'윈도우위치저장'에 해당되는 글 1건
- 2013.05.10 윈도우 위치,크기 저장/복구
윈도우 위치,크기 저장/복구
Posted on 2013. 5. 10. 16:05
Filed Under Visual C++
설명
CMainFrame에서 창크기를 제어한다.
저장은 CMainFrame::OnClose() 에서 하고
로드는 BOOL C******App::InitInstance() 에서..
소스
//아래는 MSDN에서 빼온 함수들. MainFrm.cpp에 추가
//////////////////////////////////////////////////////////////////
// Helpers for saving/restoring window state
static TCHAR BASED_CODE szSection[] = _T("Settings");
static TCHAR BASED_CODE szWindowPos[] = _T("WindowPos");
static TCHAR szFormat[] = _T("%u,%u,%d,%d,%d,%d,%d,%d,%d,%d");
static BOOL PASCAL NEAR ReadWindowPlacement(LPWINDOWPLACEMENT pwp)
{
CString strBuffer = AfxGetApp()->GetProfileString(szSection, szWindowPos);
if (strBuffer.IsEmpty())
return FALSE;
WINDOWPLACEMENT wp;
int nRead = _stscanf(strBuffer, szFormat,
&wp.flags, &wp.showCmd,
&wp.ptMinPosition.x, &wp.ptMinPosition.y,
&wp.ptMaxPosition.x, &wp.ptMaxPosition.y,
&wp.rcNormalPosition.left, &wp.rcNormalPosition.top,
&wp.rcNormalPosition.right, &wp.rcNormalPosition.bottom);
if (nRead != 10)
return FALSE;
wp.length = sizeof wp;
*pwp = wp;
return TRUE;
}
static void PASCAL NEAR WriteWindowPlacement(LPWINDOWPLACEMENT pwp)
// write a window placement to settings section of app"s ini file
{
TCHAR szBuffer[sizeof("-32767")*8 + sizeof("65535")*2];
wsprintf(szBuffer, szFormat,
pwp->flags, pwp->showCmd,
pwp->ptMinPosition.x, pwp->ptMinPosition.y,
pwp->ptMaxPosition.x, pwp->ptMaxPosition.y,
pwp->rcNormalPosition.left, pwp->rcNormalPosition.top,
pwp->rcNormalPosition.right, pwp->rcNormalPosition.bottom);
AfxGetApp()->WriteProfileString(szSection, szWindowPos, szBuffer);
}
////////////////////////////////////////////////
//아래 두개의 함수를 만들어 주세요.
//아래는 복원, 저장 함수...둘다 public
void CMainFrame::SaveWindowPlacement()
{
WINDOWPLACEMENT wp;
wp.length = sizeof wp;
if (GetWindowPlacement(&wp))
{
wp.flags = 0;
if (IsZoomed())
wp.flags |= WPF_RESTORETOMAXIMIZED;
// and write it to the .INI file
WriteWindowPlacement(&wp);
}
}
BOOL CMainFrame::RestoreWindowPlaceMent()
{
WINDOWPLACEMENT wp;
if (ReadWindowPlacement(&wp)){
SetWindowPlacement(&wp);
return TRUE;
}
return FALSE;
}
//////////////////////////////////////////////////////////////
//사용예
//저장
void CMainFrame::OnClose()
{
// TODO: Add your message handler code here and/or call default
//현재위치 저장하기
SaveWindowPlacement();
CFrameWnd::OnClose();
}
//복원.. 한글 주석이 달린곳만 보세요.
//CMainFrame에서 복원 작업을 하면 최대화 상태의 윈도우에서 문제가 있답니다.
BOOL C******App::InitInstance()
{
AfxEnableControlContainer();
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif
// Change the registry key under which our settings are stored.
// TODO: You should modify this string to be something appropriate
// such as the name of your company or organization.
SetRegistryKey(_T("Local AppWizard-Generated Applications"));
LoadStdProfileSettings(); // Load standard INI file options (including MRU)
//아래줄을 안해놓으면 깜빡일수 있다.
this->m_nCmdShow=SW_HIDE;
// Register the application"s document templates. Document templates
// serve as the connection between documents, frame windows and views.
CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CDockingWndDoc),
RUNTIME_CLASS(CMainFrame), // main SDI frame window
RUNTIME_CLASS(CDockingWndView));
AddDocTemplate(pDocTemplate);
// Parse command line for standard shell commands, DDE, file open
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
// Dispatch commands specified on the command line
if (!ProcessShellCommand(cmdInfo))
return FALSE;
// The one and only window has been initialized, so show and update it.
//이전위치로 만들기.
if (!((CMainFrame*)m_pMainWnd)->RestoreWindowPlaceMent())
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
return TRUE;
}
////////////////////////////////////////////////////////////////////////////////////////////
출처 - http://www.devpia.com/Forum/boardview.aspx?forumname=VC_LEC&no=2041