📄 mtlprofile.h
字号:
POINT ptOrg = { 0, 0 };
::DPtoLP(hDC1, &ptOrg, 1);
if(hDC == NULL)
::ReleaseDC(NULL, hDC1);
return -abs(pt.y - ptOrg.y);
}
static LONG GetPointSizeFromHeight(LONG nHeight, HDC hDC = NULL)
{
HDC hDC1 = (hDC != NULL) ? hDC : (::GetDC(NULL));
LONG nPointSize = ::MulDiv(-nHeight, 72, ::GetDeviceCaps(hDC1, LOGPIXELSY));
if(hDC == NULL)
::ReleaseDC(NULL, hDC1);
return nPointSize;
}
};
/////////////////////////////////////////////////////////////////////////////
// MtlWrite&GetProfileMainFrameState
template <class _Profile>
void MtlWriteProfileMainFrameState(_Profile& __profile, HWND hWnd,
const CString strPrefix = _T("frame."))
{
ATLASSERT(::IsWindow(hWnd));
CWindowPlacement wndpl;
MTLVERIFY(::GetWindowPlacement(hWnd, &wndpl));
wndpl.WriteProfile(__profile, strPrefix);
}
template <class _Profile>
void MtlGetProfileMainFrameState(_Profile& __profile, HWND hWnd, int nCmdShow, bool bShowNoMinimized = true,
const CString strPrefix = _T("frame."))
{
ATLASSERT(::IsWindow(hWnd));
CWindowPlacement wndpl;
if (wndpl.GetProfile(__profile, strPrefix) && wndpl.IsInsideScreen()) {
if (bShowNoMinimized) {
if (wndpl.showCmd == SW_SHOWMINIMIZED) {
if (wndpl.flags & WPF_RESTORETOMAXIMIZED)
wndpl.showCmd = SW_SHOWMAXIMIZED;
else
wndpl.showCmd = SW_SHOWNORMAL;
}
}
MTLVERIFY(::SetWindowPlacement(hWnd, &wndpl));
}
else
::ShowWindow(hWnd, nCmdShow);
}
template <class _Profile, class _Function>
void MtlGetProfileMainFrameState_Chance(_Profile& __profile, HWND hWnd, int nCmdShow, _Function __f,
bool bShowNoMinimized = true, const CString strPrefix = _T("frame."))
{
ATLASSERT(::IsWindow(hWnd));
CWindowPlacement wndpl;
if (wndpl.GetProfile(__profile, strPrefix) && wndpl.IsInsideScreen()) {
if (bShowNoMinimized) {
if (wndpl.showCmd == SW_SHOWMINIMIZED) {
if (wndpl.flags & WPF_RESTORETOMAXIMIZED)
wndpl.showCmd = SW_SHOWMAXIMIZED;
else
wndpl.showCmd = SW_SHOWNORMAL;
}
}
int showCmd = wndpl.showCmd;
wndpl.showCmd = SW_HIDE;
MTLVERIFY(::SetWindowPlacement(hWnd, &wndpl));
__f();
::ShowWindow(hWnd, showCmd);
}
else {
__f();
::ShowWindow(hWnd, nCmdShow);
}
}
/////////////////////////////////////////////////////////////////////////////
// MtlWrite&GetProfileChildFrameState
template <class _Profile>
void MtlWriteProfileChildFrameState(_Profile& __profile, HWND hWnd,
const CString strPrefix = _T("frame."))
{
ATLASSERT(::IsWindow(hWnd));
CWindowPlacement wndpl;
MTLVERIFY(::GetWindowPlacement(hWnd, &wndpl));
wndpl.WriteProfile(__profile, strPrefix);
}
template <class _Profile>
void MtlGetProfileChildFrameState(_Profile& __profile, HWND hWnd, int nCmdShowDefault, bool bShowNoActivate = false,
const CString strPrefix = _T("frame."))
{
ATLASSERT(::IsWindow(hWnd));
CWindowPlacement wndpl;
if (wndpl.GetProfile(__profile, strPrefix) && wndpl.IsInsideScreen()) {
if (bShowNoActivate) {
if (wndpl.showCmd == SW_SHOWMINIMIZED)
wndpl.showCmd = SW_SHOWMINNOACTIVE;
else
wndpl.showCmd = SW_SHOWNOACTIVATE;
}
MTLVERIFY(::SetWindowPlacement(hWnd, &wndpl));
}
else {
if (bShowNoActivate)
nCmdShowDefault = SW_SHOWNOACTIVATE;
::ShowWindow(hWnd, nCmdShowDefault);
}
}
/////////////////////////////////////////////////////////////////////////////
// MtlWrite&GetProfileRebarBandsState
template <class _Profile>
void MtlWriteProfileReBarBandsState(_Profile& __profile, CReBarCtrl rebar,
const CString& strPrefix = _T("band"))
{
for (int nIndex = 0; ; ++nIndex) {
CVersional<REBARBANDINFO> rbBand;
rbBand.fMask = RBBIM_SIZE | RBBIM_STYLE | RBBIM_CHILD;
if (!rebar.GetBandInfo(nIndex, &rbBand))
break;
CString strBuff = strPrefix + _T("#");
strBuff.Append(CWindow(rbBand.hwndChild).GetDlgCtrlID());
MTLVERIFY(__profile.SetValue(nIndex, strBuff + _T(".index")) == ERROR_SUCCESS);
MTLVERIFY(__profile.SetValue(rbBand.cx, strBuff + _T(".cx")) == ERROR_SUCCESS);
MTLVERIFY(__profile.SetValue(rbBand.fStyle, strBuff + _T(".fStyle")) == ERROR_SUCCESS);
}
}
struct CReBarBandInfo
{
UINT nIndex; // must be filled, cause stable_sort requires CRT
HWND hWnd;
UINT nID;
UINT fStyle;
LPTSTR lpText;
UINT cx; // can be 0
};
template <class _MainFrame>
class __AddBand : public std::unary_function<const CReBarBandInfo&, void>
{
public:
explicit __AddBand(_MainFrame& __m) : mainFrame(&__m) { }
void operator()(const CReBarBandInfo& arg)
{
// _mainFrame.AddSimpleReBarBand(arg.hWnd, arg.lpText, (arg.fStyle & RBBS_BREAK) ? TRUE : FALSE, arg._cx); ATL_IDW_BAND_FIRST is bullshit!
_MainFrame::AddSimpleReBarBandCtrl(mainFrame->m_hWndToolBar,
arg.hWnd, arg.nID, arg.lpText, (arg.fStyle & RBBS_BREAK) ? TRUE : FALSE, arg.cx);
}
protected:
_MainFrame* mainFrame;
};
class __ShowBand : public std::unary_function<const CReBarBandInfo&, void>
{
public:
explicit __ShowBand(HWND hWndReBar) : rebar(hWndReBar), nIndex(0) { }
void operator()(const CReBarBandInfo& arg)
{
rebar.ShowBand(nIndex++, (arg.fStyle & RBBS_HIDDEN) ? FALSE : TRUE);
}
protected:
CReBarCtrl rebar;
int nIndex;
};
struct __ReBarBandInfoIndexOrdering : public std::binary_function<const CReBarBandInfo&, const CReBarBandInfo&, bool>
{
bool operator()(const CReBarBandInfo& arg1, const CReBarBandInfo& arg2) const
{
return arg1.nIndex < arg2.nIndex;
}
};
/*
inline bool operator<(const CReBarBandInfo& arg1, const CReBarBandInfo& arg2)
{
return arg1.nIndex < arg2.nIndex;
}
*/
template <class _Profile, class _MainFrame>
bool MtlGetProfileReBarBandsState(CReBarBandInfo* __first, CReBarBandInfo* __last,
_Profile __profile, _MainFrame& __mainFrame,
const CString& strPrefix = _T("band"))
{
if (__first == __last)
return false;
CSimpleArray<CReBarBandInfo> tmp;
int nIndex = 0;
for (; __first != __last; ++__first) {
CString strBuff = strPrefix + _T("#");
strBuff.Append(__first->nID);
DWORD dwIndex, dwCx, dwStyle;
if (__profile.QueryValue(dwIndex, strBuff + _T(".index")) == ERROR_SUCCESS &&
__profile.QueryValue(dwCx, strBuff + _T(".cx")) == ERROR_SUCCESS &&
__profile.QueryValue(dwStyle, strBuff + _T(".fStyle")) == ERROR_SUCCESS)
{
__first->nIndex = dwIndex;
__first->cx = dwCx;
__first->fStyle = dwStyle;
}
tmp.Add(*__first);// even if value not found, user's supplements used as default
}
if (tmp.GetSize() == 0)// need
return false;
// I want use std::stable_sort for users, but it requires CRT.
// I think easy sort algorithms have to be added to ATL/WTL.
std::sort(&tmp[0], &tmp[0] + tmp.GetSize(), __ReBarBandInfoIndexOrdering());
std::for_each(&tmp[0], &tmp[0] + tmp.GetSize(), __AddBand<_MainFrame>(__mainFrame));
std::for_each(&tmp[0], &tmp[0] + tmp.GetSize(), __ShowBand(__mainFrame.m_hWndToolBar));
return true;
}
/////////////////////////////////////////////////////////////////////////////
// MtlWrite&GetProfileStatusBarState
template <class _Profile>
void MtlWriteProfileStatusBarState(_Profile& __profile, HWND hWndStatusBar,
const CString& strPrefix = _T("statusbar."))
{
ATLASSERT(::IsWindow(hWndStatusBar));
LONG lRet = __profile.SetValue(::IsWindowVisible(hWndStatusBar) ? 1 : 0, strPrefix + _T("Visible"));
ATLASSERT(lRet == ERROR_SUCCESS);
}
template <class _Profile>
bool MtlGetProfileStatusBarState(_Profile& __profile, HWND hWndStatusBar, BOOL& bVisible,
const CString& strPrefix = _T("statusbar."))
{
ATLASSERT(::IsWindow(hWndStatusBar));
DWORD dwValue;
LONG lRet = __profile.QueryValue(dwValue, strPrefix + _T("Visible"));
if (lRet != ERROR_SUCCESS) {
bVisible = TRUE;
return false;
}
bVisible = (dwValue == 1) ? TRUE : FALSE;
MTLVERIFY(::ShowWindow(hWndStatusBar, bVisible ? SW_SHOWNOACTIVATE : SW_HIDE));
return true;
}
/*
This makes fatal error C1001, what can I do ?
template <class _Profile>
bool MtlWriteProfileStatusBarState(_Profile& __profile, CStatusBarCtrl statusbar,
const CString& strPrefix = _T("statusbar."))
{
__profile.SetValue(statusbar.IsWindowVisible() ? 1 : 0, strPrefix + _T("Visible"));
return true;
}
template <class _Profile>
bool MtlGetProfileStatusBarState(_Profile& __profile, CStatusBarCtrl statusbar,
const CString& strPrefix = _T("statusbar."))
{
DWORD dwValue;
LONG lRet = __profile.QueryValue(dwValue, strPrefix + _T("Visible"));
if (lRet != ERROR_SUCCESS)
return false;
statusbar.ShowWindow((dwValue == 1) ? SW_SHOWNOACTIVATE : SW_HIDE);
return true;
}
*/
/////////////////////////////////////////////////////////////////////////////
// Global functions for Profile
// app.exe -> APP.INI
// App.exe -> App.ini
inline void MtlIniFileNameInit(LPTSTR lpszIniFileName, DWORD nSize, LPCTSTR lpszExtText = _T(".INI"))
{// cf.ATL::AtlModuleLoadTypeLib (we can't use _tsplitpath that requires CRT)
::GetModuleFileName(_Module.GetModuleInstance(), lpszIniFileName, nSize);
LPTSTR lpszExt = NULL;
LPTSTR lpsz;
for (lpsz = lpszIniFileName; *lpsz != NULL; lpsz = ::CharNext(lpsz)) {
if (*lpsz == _T('.'))
lpszExt = lpsz;
}
if (lpszExt == NULL)
lpszExt = lpsz;
ATLASSERT(::lstrlen(lpszExtText) == 4);
::lstrcpy(lpszExt, lpszExtText);
#ifdef _MTL_PROFILE_EXTRA_TRACE
ATLTRACE2(atlTraceUI, 0, _T("MtlIniFileNameInit : %s\n"), lpszIniFileName);
#endif
}
/////////////////////////////////////////////////////////////////////////////
// MtlWrite&GetProfileString
template <class _InputIterString, class _Profile>
bool MtlWriteProfileString(_InputIterString __first, _InputIterString __last, _Profile& __profile,
const CString& strPrefix = _T("string"), DWORD nStartSuffix = 0)
{
// MTLVERIFY(__profile.SetValue(std::distance(__first, __last), strPrefix + _T(".count")) == ERROR_SUCCESS);
// set new values
for (int n = nStartSuffix; __first != __last; ++__first, ++n) {
CString strBuff = strPrefix;
strBuff.Append(n);
MTLVERIFY(__profile.SetValue(*__first, strBuff) == ERROR_SUCCESS);
}
return true;
}
template <class _Profile, class _OutputIterString>
bool MtlGetProfileString(_Profile& __profile, _OutputIterString __result,
const CString& strPrefix = _T("string"), DWORD nStartSuffix = 0, DWORD nLastSuffix = -1)
{
// DWORD dwValue;
// LONG lRet = __profile.QueryValue(dwValue, strPrefix + _T(".count"));
// if (lRet != ERROR_SUCCESS)
// return false;
_OutputIterString __resultSrc = __result;
DWORD n;
for (n = nStartSuffix; ; ++n) {
CString strBuff = strPrefix;
strBuff.Append(n);
TCHAR szT[4096];// no other way
DWORD dwCount = 4096;
LONG lRet = __profile.QueryValue(szT, strBuff, &dwCount);
if (lRet == ERROR_SUCCESS)
*__result++ = szT;
else
break;
if (n == nLastSuffix)
break;
}
if (n == nStartSuffix)
return false;
else
return true;
}
/////////////////////////////////////////////////////////////////////////////
// MtlWrite&GetProfileTBBtns
// This will save a bitmap index, so you can't change it by toolbar resource editor.
// If you want to add a new button for your new app's version, you have to add it last of all.
template <class _Profile>
void MtlWriteProfileTBBtns(_Profile& __profile, CToolBarCtrl toolbar, const CString& strPrefix = _T("button"))
{
int iBtn = toolbar.GetButtonCount();
MTLVERIFY(__profile.SetValue(iBtn, strPrefix + _T(".count")) == ERROR_SUCCESS);
for (int n = 0; n < iBtn; ++n) {
TBBUTTON tbBtn;
MTLVERIFY(toolbar.GetButton(n, &tbBtn));
CString strBuff = strPrefix;
strBuff.Append(n);
int nIndex;
if (tbBtn.fsStyle & TBSTYLE_SEP)
nIndex = -1;
else
nIndex = tbBtn.iBitmap;
MTLVERIFY(__profile.SetValue(nIndex, strBuff + _T(".iBitmap")) == ERROR_SUCCESS);
}
}
/*
void MtlWriteProfileTBBtns(CRegKey& regkey, CToolBar toolbar, const CString& strPrefix = _T("button"))
{
toolbar.SaveState(regkey.m_hKey, strPrefix, strPrefix);
}
*/
template <class _Profile>
bool MtlGetProfileTBBtns(_Profile& __profile, CSimpleArray<int>& arrBmpIndex,
const CString& strPrefix = _T("button"))
{
// load profile
DWORD dwCount;
LONG lRet = __profile.QueryValue(dwCount, strPrefix + _T(".count"));
if (lRet != ERROR_SUCCESS)
return false;//not found
// insert buttons
for (DWORD n = 0; n < dwCount; ++n) {
CString strBuff = strPrefix;
strBuff.Append(n);
DWORD dwBitmap;
if (__profile.QueryValue(dwBitmap, strBuff + _T(".iBitmap")) == ERROR_SUCCESS)
{
int iBmpIndex = dwBitmap;
arrBmpIndex.Add(iBmpIndex);
}
}
return true;
}
template <class _Profile>
bool MtlGetProfileTBBtns(_Profile& __profile, CToolBarCtrl toolbar, const CString& strPrefix = _T("button"))
{
ATLASSERT(toolbar.IsWindow());
// load profile
DWORD dwCount;
LONG lRet = __profile.QueryValue(dwCount, strPrefix + _T(".count"));
if (lRet != ERROR_SUCCESS)
return false;//not found
int iBtn = toolbar.GetButtonCount();
TBBUTTON* pTBBtn = (TBBUTTON*)_alloca(iBtn * sizeof(TBBUTTON));
// save original tbbuttons
for (int i = 0; i < iBtn; ++i) {
TBBUTTON tbBtn;
MTLVERIFY(toolbar.GetButton(i, &tbBtn));
pTBBtn[i] = tbBtn;
}
// clean up previous toolbar buttons
while (toolbar.DeleteButton(0))
;
// insert buttons
for (DWORD n = 0; n < dwCount; ++n) {
CString strBuff = strPrefix;
strBuff.Append(n);
DWORD dwBitmap;
if (__profile.QueryValue(dwBitmap, strBuff + _T(".iBitmap")) == ERROR_SUCCESS)
{
if (dwBitmap == -1) {// separator
TBBUTTON tbBtn;
tbBtn.iBitmap = 8;
tbBtn.idCommand = 0;
tbBtn.fsState = 0;
tbBtn.fsStyle = TBSTYLE_SEP;
tbBtn.dwData = 0;
tbBtn.iString = 0;
MTLVERIFY(toolbar.InsertButton(toolbar.GetButtonCount(), &tbBtn));
}
else {
int i;
for (i = 0; i < iBtn; ++i) {
if (!(pTBBtn[i].fsStyle & TBSTYLE_SEP) && pTBBtn[i].iBitmap == dwBitmap) {
MTLVERIFY(toolbar.InsertButton(toolbar.GetButtonCount(), &pTBBtn[i]));
break;
}
}
ATLASSERT(i != iBtn);
}
}
}
return true;
}
/*
void MtlGetProfileTBBtns(CRegKey& regkey, CToolBar toolbar, const CString& strPrefix = _T("button"))
{
toolbar.RestoreState(regkey.m_hKey, strPrefix, strPrefix);
}
*/
} //namespace MTL
#endif // __MTLPROFILE_H__
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -