summaryrefslogtreecommitdiff
path: root/Shutdown/PersistPosWindow.h
blob: 74aff515fffe18a028ec1b06e914f77726ec4ed6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// 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 <RegSettings.h>

template<class T>
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 <class T>
bool CPersistPosWindow<T>::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 <class T>
bool CPersistPosWindow<T>::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_)