// PersistPosWindow.h: interface for the CPersistPosWindow class. // ////////////////////////////////////////////////////////////////////// #if !defined(AFX_PERSISTPOSWINDOW_H__7D209B81_ED68_11D3_8378_0020182B97FC__INCLUDED_) #define AFX_PERSISTPOSWINDOW_H__7D209B81_ED68_11D3_8378_0020182B97FC__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #include template class CPersistPosWindow { public: CPersistPosWindow(LPCTSTR szPrefix) { m_szPrefix = szPrefix; }; bool LoadPosition(CRegSettings& settings); bool SavePosition(CRegSettings& settings); protected: LPCTSTR m_szPrefix; // Prefix for Registry Entries }; template bool CPersistPosWindow::LoadPosition(CRegSettings& settings) { // Pointer to the Window Class T* pThis = (T*)this; // Init WINDOWPLACEMENT Structure WINDOWPLACEMENT wp; wp.length = sizeof(WINDOWPLACEMENT); wp.flags = 0; wp.showCmd = SW_SHOWNA; // Not worrying about these for now // Probably okay wp.ptMaxPosition.x = wp.ptMaxPosition.y = 0; wp.ptMinPosition.x = wp.ptMinPosition.y = 0; // This prefix is used before each Registry Entry string sPrefix(m_szPrefix); // Load the Window Position from the registry wp.rcNormalPosition.top = settings.GetInt(sPrefix + _T("_top"), -1); wp.rcNormalPosition.left = settings.GetInt(sPrefix + _T("_left"), -1); wp.rcNormalPosition.bottom = settings.GetInt(sPrefix + _T("_bottom"), -1); wp.rcNormalPosition.right = settings.GetInt(sPrefix + _T("_right"), -1); // Make sure each of them got a value from the Registry // If not don't proceed if(wp.rcNormalPosition.top != -1 && wp.rcNormalPosition.left != -1 && wp.rcNormalPosition.bottom != -1 && wp.rcNormalPosition.right != -1) { // Check if on screen or not WINDOWPLACEMENT wpScreen; wpScreen.length = sizeof(WINDOWPLACEMENT); wpScreen.flags = 0; GetWindowPlacement(GetDesktopWindow(), &wpScreen); // See if Desktop Window and our Window Intersect // If not don't proceed RECT rcTemp; if(IntersectRect(&rcTemp, &wpScreen.rcNormalPosition, &wp.rcNormalPosition)) { // Move and Size the actual Window SetWindowPlacement(*pThis, &wp); return true; } } return false; } template bool CPersistPosWindow::SavePosition(CRegSettings& settings) { // Pointer to our Window Class T* pThis = (T*)this; WINDOWPLACEMENT wp; wp.length = sizeof(WINDOWPLACEMENT); wp.flags = 0; // Get the Current Placement GetWindowPlacement(*pThis, &wp); // This goes in front of each Registry Entry string sPrefix(m_szPrefix); // Write to the registry settings.WriteInt(sPrefix + _T("_top"), wp.rcNormalPosition.top); settings.WriteInt(sPrefix + _T("_left"), wp.rcNormalPosition.left); settings.WriteInt(sPrefix + _T("_bottom"), wp.rcNormalPosition.bottom); settings.WriteInt(sPrefix + _T("_right"), wp.rcNormalPosition.right); return true; } #endif // !defined(AFX_PERSISTPOSWINDOW_H__7D209B81_ED68_11D3_8378_0020182B97FC__INCLUDED_)