📄 sstate.h
字号:
wp.showCmd=SW_SHOWNORMAL;
bRes=(::RegSetValueEx(key,ctxtPlacement,NULL,REG_BINARY,
reinterpret_cast<CONST BYTE *>(&wp),
sizeof(WINDOWPLACEMENT))==ERROR_SUCCESS);
}
return baseClass::Store(pMState,key);
}
virtual bool Restore(IMainState* pMState,CRegKey& key)
{
assert(IsWindow(m_hWnd));
WINDOWPLACEMENT wp;
DWORD dwType;
DWORD cbData=sizeof(WINDOWPLACEMENT);
bool bRes=(::RegQueryValueEx(key,ctxtPlacement,NULL,&dwType,
reinterpret_cast<LPBYTE>(&wp),&cbData)==ERROR_SUCCESS)
&&(dwType==REG_BINARY);
if(bRes)
{
UINT nCmdShow=wp.showCmd;
// LockWindowUpdate(m_hWnd);
if(wp.showCmd==SW_MAXIMIZE)
::ShowWindow(m_hWnd,nCmdShow);
wp.showCmd=SW_HIDE;
::SetWindowPlacement(m_hWnd,&wp);
bRes=baseClass::Restore(pMState,key);
::ShowWindow(m_hWnd,nCmdShow);
// LockWindowUpdate(NULL);
}
else
bRes=baseClass::RestoreDefault();
return bRes;
}
virtual bool RestoreDefault()
{
assert(IsWindow(m_hWnd));
bool bRes=baseClass::RestoreDefault();
ShowWindow(m_hWnd,m_nDefCmdShow);
return bRes;
}
protected:
HWND m_hWnd;
int m_nDefCmdShow;
};
public:
CWindowStateMgr(HWND hWnd=NULL,int nDefCmdShow=SW_SHOWNOACTIVATE)
{
m_pImpl=new CImpl(hWnd,nDefCmdShow);
}
~CWindowStateMgr()
{
assert(m_pImpl);
m_pImpl->Release();
}
operator IState* ()
{
return m_pImpl;
}
ID Add(IState* pState)
{
return m_pImpl->Add(pState);
}
void Add(ID id,IState* pState)
{
m_pImpl->Add(id,pState);
}
void Remove(ID id)
{
m_pImpl->Remove(id);
}
void Initialize(const tstring& strMainKey,HWND hWnd,int nDefCmdShow=SW_SHOWNOACTIVATE)
{
m_pImpl->SetWindow(hWnd,nDefCmdShow);
m_strMainKey=strMainKey;
}
void Store()
{
CMainState mstate(m_strMainKey);
if(mstate.Store())
{
CRegKey key;
DWORD dwDisposition;
if(key.Create(mstate.MainKey(),ctxtMainWindow,REG_NONE,
REG_OPTION_NON_VOLATILE, KEY_WRITE|KEY_READ,
NULL,&dwDisposition)==ERROR_SUCCESS)
m_pImpl->Store(&mstate,key);
}
}
void Restore()
{
CMainState mstate(m_strMainKey);
CRegKey key;
if(mstate.Restore()&&
(key.Open(mstate.MainKey(),ctxtMainWindow,KEY_READ)==ERROR_SUCCESS))
m_pImpl->Restore(&mstate,key);
else
m_pImpl->RestoreDefault();
}
protected:
tstring m_strMainKey;
CImpl* m_pImpl;
};
class CWindowStateAdapter
{
protected:
class CImpl : public CStateBase<IState>
{
public:
CImpl(HWND hWnd,int nDefCmdShow=SW_SHOWNA)
:m_hWnd(hWnd),m_nDefCmdShow(nDefCmdShow)
{
assert(::IsWindow(hWnd));
}
virtual bool Store(IMainState* /*pMState*/,CRegKey& key)
{
WINDOWPLACEMENT wp;
wp.length = sizeof(WINDOWPLACEMENT);
assert(::IsWindow(m_hWnd));
bool bRes=false;
if (::GetWindowPlacement(m_hWnd,&wp))
{
wp.flags = 0;
if (::IsZoomed(m_hWnd))
wp.flags |= WPF_RESTORETOMAXIMIZED;
bRes=(::RegSetValueEx(key,ctxtPlacement,NULL,REG_BINARY,
reinterpret_cast<CONST BYTE *>(&wp),
sizeof(WINDOWPLACEMENT))==ERROR_SUCCESS);
}
return bRes;
}
virtual bool Restore(IMainState* /*pMState*/,CRegKey& key)
{
assert(::IsWindow(m_hWnd));
WINDOWPLACEMENT wp;
DWORD dwType;
DWORD cbData=sizeof(WINDOWPLACEMENT);
bool bRes=(::RegQueryValueEx(key,ctxtPlacement,NULL,&dwType,
reinterpret_cast<LPBYTE>(&wp),&cbData)==ERROR_SUCCESS)
&&(dwType==REG_BINARY);
if(bRes)
bRes=(::SetWindowPlacement(m_hWnd,&wp)!=FALSE);
return bRes;
}
virtual bool RestoreDefault()
{
::ShowWindow(m_hWnd,m_nDefCmdShow);
return true;
}
protected:
HWND m_hWnd;
int m_nDefCmdShow;
};
public:
CWindowStateAdapter(HWND hWnd,int nDefCmdShow=SW_SHOWNOACTIVATE)
{
m_pImpl = new CImpl(hWnd,nDefCmdShow);
}
~CWindowStateAdapter()
{
assert(m_pImpl);
m_pImpl->Release();
}
operator IState* ()
{
return m_pImpl;
}
protected:
CImpl* m_pImpl;
};
class CToggleWindowAdapter
{
protected:
class CImpl : public CStateBase<IState>
{
public:
CImpl(HWND hWnd,int nDefCmdShow=SW_SHOWNA)
:m_hWnd(hWnd),m_nDefCmdShow(nDefCmdShow)
{
assert(::IsWindow(hWnd));
}
virtual bool Store(IMainState* /*pMState*/,CRegKey& key)
{
DWORD visible=::IsWindowVisible(m_hWnd);
return (::RegSetValueEx(key, ctxtVisible, NULL, REG_DWORD,
reinterpret_cast<BYTE*>(&visible), sizeof(DWORD))==ERROR_SUCCESS);
// return (key.SetValue(visible,ctxtVisible)==ERROR_SUCCESS);
}
virtual bool Restore(IMainState* /*pMState*/,CRegKey& key)
{
DWORD visible;
// bool bRes=(key.QueryValue(visible,ctxtVisible)==ERROR_SUCCESS);
DWORD dwCount = sizeof(DWORD);
bool bRes=(::RegQueryValueEx(key,ctxtVisible,NULL,NULL,
reinterpret_cast<LPBYTE>(&visible),&dwCount)==ERROR_SUCCESS
&& (dwCount == sizeof(DWORD)));
if(bRes)
::ShowWindow(m_hWnd, (visible!=0) ? SW_SHOWNA : SW_HIDE);
else
RestoreDefault();
return bRes;
}
virtual bool RestoreDefault()
{
::ShowWindow(m_hWnd,m_nDefCmdShow);
return true;
}
protected:
HWND m_hWnd;
int m_nDefCmdShow;
};
public:
CToggleWindowAdapter(HWND hWnd,int nDefCmdShow=SW_SHOWNOACTIVATE)
{
m_pImpl = new CImpl(hWnd,nDefCmdShow);
}
~CToggleWindowAdapter()
{
assert(m_pImpl);
m_pImpl->Release();
}
operator IState* ()
{
return m_pImpl;
}
protected:
CImpl* m_pImpl;
};
class CRebarStateAdapter
{
protected:
class CImpl : public CStateBase<IState>
{
public:
CImpl(HWND hWnd, int storeVersion)
:m_rebar(hWnd), m_storageVersion(storeVersion)
{
assert(::IsWindow(hWnd));
}
virtual bool Store(IMainState* /*pMState*/,CRegKey& key)
{
assert(m_rebar.IsWindow());
::RegSetValueEx(key,ctxtStoreVer, NULL, REG_DWORD, (LPBYTE)&m_storageVersion, sizeof(DWORD));
unsigned int bandCount=m_rebar.GetBandCount();
for(unsigned int i=0;i<bandCount;i++)
{
std::basic_stringstream<TCHAR> sstrKey;
sstrKey<<ctxtBand<<i;
REBARBANDINFO rbi;
ZeroMemory(&rbi,sizeof(REBARBANDINFO));
rbi.cbSize = sizeof(REBARBANDINFO);
rbi.fMask = RBBIM_ID |
RBBIM_SIZE | RBBIM_STYLE
// The following causes the app to remember rebar colors,
// breaking windows theme changes.
//RBBIM_COLORS |
// The following causes the rebars to shift left on restore.
#if (_WIN32_IE >= 0x0400)
| /*RBBIM_HEADERSIZE |*/ RBBIM_IDEALSIZE
#endif
;
m_rebar.GetBandInfo(i, &rbi);
::RegSetValueEx(key,sstrKey.str().c_str(),NULL,REG_BINARY,
reinterpret_cast<CONST BYTE *>(&rbi),
rbi.cbSize);
}
return true;
}
virtual bool Restore(IMainState* /*pMState*/,CRegKey& key)
{
DWORD dwType;
DWORD dwVal;
DWORD cbData = sizeof(DWORD);
if( ::RegQueryValueEx(key, ctxtStoreVer, NULL, &dwType,
(LPBYTE)&dwVal, &cbData) == ERROR_SUCCESS && (dwType == REG_DWORD) )
{
// If the versions aren't the same, then we don't bother to
// restore - we'll probably brake the ReBars by doing so.
if( dwVal != m_storageVersion )
return false;
}
else
{
// If we couldn't query that key, then we never wrote a version
// number before, so it must be an old version.
return false;
}
unsigned int bandCount=m_rebar.GetBandCount();
for(unsigned int i=0;i<bandCount;i++)
{
std::basic_stringstream<TCHAR> sstrKey;
sstrKey<<ctxtBand<<i;
CRegKey keyBand;
REBARBANDINFO rbi;
//ZeroMemory(&rbi,sizeof(REBARBANDINFO));
cbData=sizeof(REBARBANDINFO);
if((::RegQueryValueEx(key,sstrKey.str().c_str(),NULL,&dwType,
reinterpret_cast<LPBYTE>(&rbi),&cbData)==ERROR_SUCCESS)
&&(dwType==REG_BINARY))
{
m_rebar.MoveBand(m_rebar.IdToIndex(rbi.wID), i);
m_rebar.SetBandInfo(i, &rbi);
}
}
return true;
}
virtual bool RestoreDefault()
{
return true;
}
protected:
CReBarCtrl m_rebar;
int m_storageVersion;
};
public:
CRebarStateAdapter(HWND hWnd, int storeVersion = 0)
{
m_pImpl = new CImpl(hWnd, storeVersion);
}
~CRebarStateAdapter()
{
assert(m_pImpl);
m_pImpl->Release();
}
operator IState* ()
{
return m_pImpl;
}
protected:
CImpl* m_pImpl;
};
}//namespace sstate
#endif // __WTL_DW__SSTATE_H__
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -