// // AUTHOR // N. Nielsen // // LICENSE // This software is in the public domain. // // The software is provided "as is", without warranty of any kind, // express or implied, including but not limited to the warranties // of merchantability, fitness for a particular purpose, and // noninfringement. In no event shall the author(s) be liable for any // claim, damages, or other liability, whether in an action of // contract, tort, or otherwise, arising from, out of, or in connection // with the software or the use or other dealings in the software. // // SUPPORT // Send bug reports to: // #ifndef __PERSISTPOSWINDOW_H__ #define __PERSISTPOSWINDOW_H__ #include "regsettings.h" 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 TCHAR sPrefix[MAX_PATH]; _tcsncpy(sPrefix, m_szPrefix, MAX_PATH); LPTSTR sEnd = sPrefix + _tcslen(sPrefix); // Load the Window Position from the registry _tcsncat(sPrefix, _T("_top"), MAX_PATH); wp.rcNormalPosition.top = settings.GetInt(sPrefix, -1); *sEnd = 0; _tcsncat(sPrefix, _T("_left"), MAX_PATH); wp.rcNormalPosition.left = settings.GetInt(sPrefix, -1); *sEnd = 0; _tcsncat(sPrefix, _T("_bottom"), MAX_PATH); wp.rcNormalPosition.bottom = settings.GetInt(sPrefix, -1); *sEnd = 0; _tcsncat(sPrefix, _T("_right"), MAX_PATH); wp.rcNormalPosition.right = settings.GetInt(sPrefix, -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 prefix is used before each Registry Entry TCHAR sPrefix[MAX_PATH]; _tcsncpy(sPrefix, m_szPrefix, MAX_PATH); LPTSTR sEnd = sPrefix + _tcslen(sPrefix); _tcsncat(sPrefix, _T("_top"), MAX_PATH); settings.WriteInt(sPrefix, wp.rcNormalPosition.top); *sEnd = 0; _tcsncat(sPrefix, _T("_left"), MAX_PATH); settings.WriteInt(sPrefix, wp.rcNormalPosition.left); *sEnd = 0; _tcsncat(sPrefix, _T("_bottom"), MAX_PATH); settings.WriteInt(sPrefix, wp.rcNormalPosition.bottom); *sEnd = 0; _tcsncat(sPrefix, _T("_right"), MAX_PATH); settings.WriteInt(sPrefix, wp.rcNormalPosition.right); return true; } #endif