|
#ifndef WINDOWPLACEMENT_H
#define WINDOWPLACEMENT_H
class CWindowPlacement : public WINDOWPLACEMENT
{
public:
CWindowPlacement()
{
memset( (WINDOWPLACEMENT*) this, 0, sizeof(WINDOWPLACEMENT) );
length = sizeof(WINDOWPLACEMENT);
showCmd = SW_NORMAL;
}
CWindowPlacement(HWND hWnd)
{
length = sizeof(WINDOWPLACEMENT);
GetPosData(hWnd);
}
BOOL GetPosData(HWND hWnd)
{
return ::GetWindowPlacement(hWnd, this);
}
BOOL GetPosData(LPCTSTR pstr)
{
if( lstrlen(pstr)==0 ) return FALSE;
flags = _GetInt(pstr);
showCmd = _GetInt(pstr);
ptMinPosition.x = _GetInt(pstr);
ptMinPosition.y = _GetInt(pstr);
ptMaxPosition.x = _GetInt(pstr);
ptMaxPosition.y = _GetInt(pstr);
rcNormalPosition.left = _GetInt(pstr);
rcNormalPosition.top = _GetInt(pstr);
rcNormalPosition.right = _GetInt(pstr);
rcNormalPosition.bottom = _GetInt(pstr);
return TRUE;
}
BOOL GetPosData(HKEY hReg, LPCTSTR pstrKeyName)
{
TCHAR szData[256];
DWORD dwCount = sizeof(szData)/sizeof(TCHAR);
DWORD dwType = NULL;
LONG lRes = ::RegQueryValueEx(hReg, pstrKeyName, NULL, &dwType, (LPBYTE)szData, &dwCount);
if( lRes!=ERROR_SUCCESS ) return FALSE;
return GetPosData(szData);
}
BOOL SetPosData(HWND hWnd)
{
// NOTE: Do not place this call in the window's own WM_CREATE handler.
// Remember to call ShowWindow() if this method fails!
// Make sure it is not out-of-bounds
RECT rcTemp;
RECT rcScreen = { 0, 0, ::GetSystemMetrics(SM_CXSCREEN), ::GetSystemMetrics(SM_CYSCREEN) };
if( ::IsRectEmpty(&rcNormalPosition) ) return FALSE;
if( !::IntersectRect(&rcTemp, &rcNormalPosition, &rcScreen) ) return FALSE;
// Show it...
return ::SetWindowPlacement(hWnd, this);
}
BOOL SetPosData(LPTSTR pstr, UINT cchMax) const
{
TCHAR szData[256] = { 0 };
::wsprintf(szData, TEXT("%ld %ld (%ld,%ld) (%ld,%ld) (%ld,%ld,%ld,%ld)"),
flags,
showCmd,
ptMinPosition.x,
ptMinPosition.y,
ptMaxPosition.x,
ptMaxPosition.y,
rcNormalPosition.left,
rcNormalPosition.top,
rcNormalPosition.right,
rcNormalPosition.bottom);
if (lstrlen(szData) > (int)cchMax)
return FALSE;
lstrcpy(pstr, szData);
return TRUE;
}
BOOL SetPosData(HKEY hReg, LPCTSTR pstrValueName) const
{
TCHAR szData[256] = { 0 };
if( !SetPosData(szData, (UINT) sizeof(szData)/sizeof(TCHAR)) ) return FALSE;
return ::RegSetValueEx(hReg, pstrValueName, NULL, REG_SZ, (CONST BYTE*) szData, (::lstrlen(szData)+1)*sizeof(TCHAR))==ERROR_SUCCESS;
}
long _GetInt(LPCTSTR& pstr) const
{
// NOTE: 'pstr' argument is "byref"
bool fNeg = false;
if( *pstr==TCHAR('-') ) {
fNeg = true;
pstr = ::CharNext(pstr);
}
long n = 0;
while( *pstr>=TCHAR('0') && *pstr<=TCHAR('9') ) {
n = (n*10) + (*pstr - TCHAR('0'));
pstr = ::CharNext(pstr);
}
while( *pstr>=TCHAR(' ') && *pstr<TCHAR('0') && *pstr!=TCHAR('-') ) pstr = ::CharNext(pstr);
return fNeg ? -n : n;
}
};
#endif //WINDOWPLACEMENT_H
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Ernest is a multi-discipline software engineer.
Skilled at software design and development for all Windows platforms.
-
MCSD (C#, .NET)
Interests: User Interface, GDI/GDI+, Scripting, Android, iOS, Windows Mobile.
Programming Skills: C/C++, C#, Java (Android), VB and ASP.NET.
I hope you will enjoy my contributions.