📄 atlwinmisc.h
字号:
ReleaseBuffer();
return nLen > 0;
}
BOOL __cdecl Format(WORD wLanguage, UINT nFormatID, ...)
{
CLangString strFormat;
BOOL bRet = strFormat.LoadString(wLanguage, nFormatID);
ATLASSERT(bRet!=0);
va_list argList;
va_start(argList, nFormatID);
bRet = FormatV(strFormat, argList);
va_end(argList);
return bRet;
}
// Load String helper
static int __stdcall _LoadString(WORD wLanguage, UINT nID, LPTSTR lpszBuf, UINT nMaxBuf)
{
#if (_ATL_VER >= 0x0700)
HRSRC hRsrc = ::FindResourceEx(ATL::_AtlBaseModule.GetResourceInstance(), (LPCTSTR) RT_STRING, (LPCTSTR) MAKEINTRESOURCE((nID>>4)+1), wLanguage);
#else
HRSRC hRsrc = ::FindResourceEx(_Module.GetResourceInstance(), (LPCTSTR) RT_STRING, (LPCTSTR) MAKEINTRESOURCE((nID>>4)+1), wLanguage);
#endif // _ATL_VER
if( hRsrc == NULL ) {
#ifdef _DEBUG
LPVOID lpMsgBuf = NULL;
::FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
::GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
// Display the error string.
::MessageBox(NULL, static_cast<LPTSTR>(lpMsgBuf), NULL, MB_OK | MB_ICONERROR );
::LocalFree(lpMsgBuf);
#endif // _DEBUG
lpszBuf[0] = _T('\0');
return 0;
}
#if (_ATL_VER >= 0x0700)
HGLOBAL hGlb = ::LoadResource(ATL::_AtlBaseModule.GetResourceInstance(), hRsrc);
#else
HGLOBAL hGlb = ::LoadResource(_Module.GetResourceInstance(), hRsrc);
#endif // _ATL_VER
LPWSTR lpwsz = static_cast<LPWSTR>(::LockResource(hGlb));
// Increment the pointer in the resource block up to the required string
for( UINT i = 0; i < (nID & 15); i++ ) {
lpwsz += lpwsz[0] + 1; // Jump over preceding resources in the block
}
size_t nStrLen = lpwsz[0];
#ifndef _UNICODE
// BUG: Doesn't really handle Chinese/Japanese DBCS!
// OPTI: Should use CString::_wcstombsz instead.
int nRet = wcstombs(lpszBuf, lpwsz + 1, min(nStrLen, (size_t) nMaxBuf));
if( nRet < (int) nMaxBuf ) lpszBuf[nRet] = _T('\0');
#else
wcsncpy(lpszBuf, lpwsz + 1, nMaxBuf);
int nRet = min( (int) nStrLen, (int) nMaxBuf );
#endif // _UNICODE
return nRet;
}
};
#endif // __ATLSTR_H__
/////////////////////////////////////////////////////////////////////////////
// CModulePath
// Wraps the ::GetModuleFileName() path
class CModulePath
{
public:
LPTSTR m_pstr; // Reference to string; allows use in wsprintf("%s", xxx) style format
CModulePath(HINSTANCE hInst = NULL) : m_pstr(NULL)
{
Set(hInst);
}
CModulePath(const CModulePath& src)
{
m_pstr = new TCHAR[MAX_PATH];
::lstrcpy(m_pstr, src.m_pstr);
}
~CModulePath()
{
if( m_pstr ) delete [] m_pstr;
}
void Set(HINSTANCE hInst)
{
// Allocate string memory
if( m_pstr ) delete [] m_pstr;
m_pstr = new TCHAR[MAX_PATH];
// Get entire module filepath
#if (_ATL_VER >= 0x0700)
if( hInst == NULL ) hInst = ATL::_AtlBaseModule.GetResourceInstance();
#else
if( hInst == NULL ) hInst = _Module.GetModuleInstance();
#endif // _ATL_VER
::GetModuleFileName(hInst, m_pstr, MAX_PATH);
// Remove filename portion
LPTSTR p = m_pstr, pSep = NULL;
while( *p ) {
if( *p == _T(':') || *p == _T('\\') ) pSep = p;
p = ::CharNext(p);
}
ATLASSERT(pSep);
*(pSep + 1) = _T('\0');
}
operator LPCTSTR() const
{
return m_pstr;
}
};
/////////////////////////////////////////////////////////////////////////////
// CWindowText
class CWindowText
{
public:
LPTSTR m_pstr;
CWindowText() : m_pstr(NULL)
{
}
CWindowText(const CWindowText& src)
{
int len = ::lstrlen(src.m_pstr) + 1;
ATLTRY(m_pstr = new TCHAR[len]);
ATLASSERT(m_pstr);
::lstrcpy(m_pstr, src.m_pstr);
}
CWindowText(HWND hWnd) : m_pstr(NULL)
{
Assign(hWnd);
}
~CWindowText()
{
delete [] m_pstr;
}
int GetLength() const
{
return ::lstrlen(m_pstr);
}
CWindowText& operator=(HWND hWnd)
{
Assign(hWnd);
return *this;
}
CWindowText& operator=(const CWindowText& src)
{
Assign(src.m_pstr);
return *this;
}
operator LPCTSTR() const
{
return m_pstr;
}
int Assign(HWND hWnd)
{
ATLASSERT(::IsWindow(hWnd));
if( m_pstr ) delete [] m_pstr;
int len = ::GetWindowTextLength(hWnd) + 1;
ATLTRY(m_pstr = new TCHAR[len]);
ATLASSERT(m_pstr);
return ::GetWindowText(hWnd, m_pstr, len);
}
int Assign(LPCTSTR pstrSrc)
{
ATLASSERT(!::IsBadStringPtr(pstrSrc, -1));
if( m_pstr ) delete [] m_pstr;
int len = ::lstrlen(pstrSrc);
ATLTRY(m_pstr = new TCHAR[len+1]);
ATLASSERT(m_pstr);
::lstrcpy(m_pstr, pstrSrc);
return len;
}
};
/////////////////////////////////////////////////////////////////////////////
// CLockWindowUpdate
class CLockWindowUpdate
{
public:
CLockWindowUpdate(HWND hWnd)
{
// NOTE: A locked window cannot be moved.
// See also Q270624 for problems with layered windows.
ATLASSERT(::IsWindow(hWnd));
::LockWindowUpdate(hWnd);
}
~CLockWindowUpdate()
{
::LockWindowUpdate(NULL);
}
};
/////////////////////////////////////////////////////////////////////////////
// CWindowRedraw
class CWindowRedraw
{
public:
HWND m_hWnd;
CWindowRedraw(HWND hWnd) : m_hWnd(hWnd)
{
ATLASSERT(::IsWindow(hWnd));
// NOTE: See Q130611 for using this with a TreeView control.
::SendMessage(m_hWnd, WM_SETREDRAW, (WPARAM) FALSE, 0);
}
~CWindowRedraw()
{
ATLASSERT(::IsWindow(m_hWnd));
::SendMessage(m_hWnd, WM_SETREDRAW, (WPARAM) TRUE, 0);
::InvalidateRect(m_hWnd, NULL, TRUE);
}
};
/////////////////////////////////////////////////////////////////////////////
// CLoadLibrary
class CLoadLibrary
{
public:
HINSTANCE m_hInst;
CLoadLibrary() : m_hInst(NULL)
{
}
CLoadLibrary(LPCTSTR pstrFileName)
{
m_hInst = NULL;
Load(pstrFileName);
}
~CLoadLibrary()
{
Free();
}
BOOL Load(LPCTSTR pstrFileName, DWORD dwFlags = 0)
{
ATLASSERT(!::IsBadStringPtr(pstrFileName, MAX_PATH));
Free();
m_hInst = ::LoadLibraryEx(pstrFileName, NULL, dwFlags);
return m_hInst != NULL;
}
void Free()
{
if( IsLoaded() ) {
::FreeLibrary(m_hInst);
m_hInst = NULL;
}
}
HINSTANCE Detach()
{
HINSTANCE hInst = m_hInst;
m_hInst = NULL;
return hInst;
}
BOOL IsLoaded() const
{
return m_hInst != NULL;
}
FARPROC GetProcAddress(LPCSTR pszFuncName) const
{
ATLASSERT(!::IsBadStringPtrA(pszFuncName,-1));
ATLASSERT(IsLoaded());
return ::GetProcAddress(m_hInst, pszFuncName);
}
BOOL GetFileName(LPTSTR pstrFilename, DWORD cchMax = MAX_PATH) const
{
ATLASSERT(IsLoaded());
return ::GetModuleFileName(m_hInst, pstrFilename, cchMax);
}
operator HINSTANCE() const
{
return m_hInst;
}
};
/////////////////////////////////////////////////////////////////////////////
// CResModule
// A class suggested by Tim Tabor to work around the problem of having
// to add a CComModule member just to be able to use CString and other
// WTL classes.
// It's a minimalistic CComModule implementation. Works best
// for EXE files though. Cannot be used as a CAppModule.
struct CResModule
{
HINSTANCE GetModuleInstance() { return ::GetModuleHandle(NULL); };
HINSTANCE GetResourceInstance() { return ::GetModuleHandle(NULL); };
};
struct CLockStaticDataInit
{
#if (_ATL_VER >= 0x0700)
CComCritSecLock<CComCriticalSection> m_cslock;
CLockStaticDataInit() : m_cslock(_pAtlModule->m_csStaticDataInitAndTypeInfo, true) { };
#else
CLockStaticDataInit() { ::EnterCriticalSection(&_Module.m_csStaticDataInit); };
~CLockStaticDataInit() { ::LeaveCriticalSection(&_Module.m_csStaticDataInit); };
#endif // _ATL_VER
};
/////////////////////////////////////////////////////////////////////////////
// CDeferWindowPos
// Wraps the DeferWindowPos() Windows API
class CDeferWindowPos
{
public:
HDWP m_hdwp;
CDeferWindowPos(int nWindows = 2)
{
m_hdwp = ::BeginDeferWindowPos(nWindows);
ATLASSERT(m_hdwp);
}
~CDeferWindowPos()
{
::EndDeferWindowPos(m_hdwp);
}
void DeferWindowPos(HWND hWnd, RECT &rc, DWORD nFlags = 0)
{
ATLASSERT(m_hdwp);
ATLASSERT(::IsWindow(hWnd));
bool bIsParent = ::GetWindow(hWnd, GW_CHILD) != NULL;
::DeferWindowPos(m_hdwp,
hWnd,
bIsParent ? HWND_TOP : 0,
rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top,
nFlags | (bIsParent ? SWP_NOZORDER : 0) );
}
void DeferWindowPos(HWND hWnd, int rcLeft, int rcTop, int rcWidth, int rcHeight, DWORD nFlags = 0)
{
ATLASSERT(m_hdwp);
ATLASSERT(::IsWindow(hWnd));
bool bIsParent = ::GetWindow(hWnd, GW_CHILD) != NULL;
::DeferWindowPos(m_hdwp,
hWnd,
bIsParent ? HWND_TOP : 0,
rcLeft, rcTop, rcWidth, rcHeight,
nFlags | (bIsParent ? SWP_NOZORDER : 0) );
}
};
/////////////////////////////////////////////////////////////////////////////
// RECT derives
#if (defined(__ATLMISC_H__) && !defined(_WTL_NO_WTYPES)) || ( defined (__ATLTYPES_H__) )
class CClientRect : public CRect
{
public:
CClientRect(HWND hWnd)
{
ATLASSERT(::IsWindow(hWnd));
::GetClientRect(hWnd, this);
}
};
class CWindowRect : public CRect
{
public:
CWindowRect(HWND hWnd)
{
ATLASSERT(::IsWindow(hWnd));
::GetWindowRect(hWnd, this);
}
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -