📄 ngutils.cpp
字号:
}
// Convert float to CString
NGLIB_EXT_API CString FloatToStr(float f, int nDigits /*= 7 */)
{
return (FloatToStr((double) f, nDigits));
}
NGLIB_EXT_API BOOL IsNumeric(LPCTSTR pszValue, double* pdValue /*= NULL*/)
{
double dValue = 0;
if (pszValue != NULL)
{
while ( (*pszValue == _T(' ')) || (*pszValue == _T('\t') ) )
{
pszValue++; // Eat leading white space
}
TCHAR chFirst = pszValue[0];
dValue = _tcstod(pszValue, (TCHAR**)&pszValue);
if ( (dValue == 0.0) && (chFirst != _T('0') ) )
{
return FALSE; // could not convert
}
while ( (*pszValue == _T(' ')) || (*pszValue == _T('\t') ) )
{
pszValue++;
}
if (*pszValue != _T('\0') )
{
return FALSE; // not terminated properly
}
if (pdValue != NULL)
{
*pdValue = dValue;
}
return TRUE;
}
return FALSE;
}
NGLIB_EXT_API BOOL IsEven(int n)
{
return ( 2*(n/2) == n);
}
NGLIB_EXT_API BOOL IsOdd(int n)
{
return !IsEven(n);
}
/*****************************************************************************
* Rounding functions
*****************************************************************************/
// This function will round a number upwards if it's modulus is greater
// than or equal to 0.5 or round it down if it is less than 0.5
NGLIB_EXT_API double Round(double dVal)
{
double dRemainder = fabs(fmod(dVal, 1) * (int)10);
if (dVal > 0)
{
if (dRemainder >= (int)5)
return ceil(dVal);
return floor(dVal);
}
if (dRemainder >= (int)5) // Negative numbers require special treatment
return floor(dVal);
return ceil(dVal);
}
// Round to a specified number of decimal places
NGLIB_EXT_API double Round(double dVal, UINT nDecPlaces)
{
double dFactor = pow(10.0, (double)nDecPlaces);
return ( Round( dVal * dFactor) / dFactor);
}
// Retrieves the name of this PC
NGLIB_EXT_API CString GetComputerName(void)
{
DWORD dwSize = _MAX_PATH;
CString sName;
LPTSTR pszBuffer = sName.GetBuffer(dwSize);
if (::GetComputerName(pszBuffer, &dwSize))
{
sName.ReleaseBuffer();
return sName;
}
return _T("");
}
// Retrieves the name of the currently logged on user
NGLIB_EXT_API CString GetUserName(void)
{
DWORD dwSize = UNLEN + 1;
CString sUserName;
LPTSTR pszBuffer = sUserName.GetBuffer(dwSize);
if (::GetUserName(pszBuffer, &dwSize))
{
sUserName.ReleaseBuffer();
return sUserName;
}
return _T("");
}
// Retrieves the resolution of the screen, in pixels
NGLIB_EXT_API CSize GetDisplayResolution(void)
{
ASSERT(NULL != AfxGetMainWnd());
CClientDC dc(AfxGetMainWnd());
return CSize(dc.GetDeviceCaps(HORZRES), dc.GetDeviceCaps(VERTRES) );
}
// Retrieves the SIZE of the screen, in mm
NGLIB_EXT_API CSize GetDisplaySize(void)
{
ASSERT(NULL != AfxGetMainWnd());
CClientDC dc(AfxGetMainWnd());
return CSize(dc.GetDeviceCaps(HORZSIZE), dc.GetDeviceCaps(VERTSIZE) );
}
// Return colour depth in bits
NGLIB_EXT_API int GetColourDepth(void)
{
int nDepth = -1;
HDC hdc = ::GetDC(NULL);
if (hdc != NULL)
{
nDepth = ::GetDeviceCaps(hdc, BITSPIXEL);
::ReleaseDC(NULL, hdc);
}
return nDepth;
}
// Return TRUE if screen deivce supports 256 colors or better
NGLIB_EXT_API BOOL Is256ColorSupported(void)
{
BOOL bRetval = FALSE;
HDC hdc = ::GetDC(NULL);
if (hdc != NULL)
{
if (::GetDeviceCaps(hdc, BITSPIXEL) >= 8)
{
bRetval = TRUE;
}
::ReleaseDC(NULL, hdc);
}
return bRetval;
}
NGLIB_EXT_API BOOL FileExists(const CString& sFileName)
{
CFile f;
CFileException e;
BOOL bExists = f.Open( sFileName,
CFile::modeRead |
CFile::shareDenyNone,
&e);
if (bExists)
{
f.Close();
}
return bExists;
}
NGLIB_EXT_API BOOL FolderExists(const CString& sDirName)
{
return (::GetFileAttributes(sDirName) == FILE_ATTRIBUTE_DIRECTORY);
}
// This function does the same thing as PathCombine() in Shlwapi.dll but using CStrings
NGLIB_EXT_API CString CombinePath(const CString& sFolder,
const CString& sRelativePath)
{
CString sResult = sFolder;
sResult.TrimRight( _T("\\") );
int nFolderEnd = -1;
int nPathStart = 0;
while (TRUE)
{
if (sRelativePath.Mid(nPathStart, 3) == _T("..\\") )
{
// For each leading "..\", strip a folder from sFolder
nPathStart +=3;
nFolderEnd = sResult.ReverseFind( _T('\\') );
sResult = sResult.Left(nFolderEnd);
}
else if (sRelativePath.Mid(nPathStart, 2) == _T(".\\") )
{
// Just ignore .\ as a starting folder
nPathStart+=2;
}
else
{
break;
}
}
if (sRelativePath.GetLength() > nPathStart)
{
sResult += _T("\\") + sRelativePath.Mid(nPathStart);
}
#ifdef _VERIFY_PATHCOMBINE
// Test against the value returned by shlwapi's PathCombine()
CString sPathCombineResult;
LPTSTR pszBuffer = sPathCombineResult.GetBuffer(_MAX_PATH);
::PathCombine( pszBuffer,
sFolder,
sRelativePath);
sPathCombineResult.ReleaseBuffer();
ASSERT(sPathCombineResult == sResult);
#endif
return sResult;
}
NGLIB_EXT_API int LoadRtfString(HMODULE hInst,
UINT nID,
LPTSTR lpszBuf,
UINT nMaxBuf)
{
DWORD dwSize = 0;
// Load text into display
HRSRC hInfo = ::FindResource(hInst, MAKEINTRESOURCE(nID), _T("RTF"));
if (hInfo != NULL)
{
HGLOBAL hInfoMem = ::LoadResource(hInst, hInfo);
dwSize = ::SizeofResource(hInst, hInfo);
if (dwSize <= (DWORD)nMaxBuf * sizeof(TCHAR) )
{
LPBYTE pData = (LPBYTE)::LockResource(hInfoMem);
memcpy( (LPVOID)lpszBuf, (LPVOID)pData, dwSize);
}
else
{
dwSize = 0;
}
}
return (int)dwSize;
}
// Taken from AfxLoadString()
NGLIB_EXT_API int LoadRtfString(UINT nID,
LPTSTR lpszBuf,
UINT nMaxBuf)
{
ASSERT(AfxIsValidAddress(lpszBuf, nMaxBuf*sizeof(TCHAR)));
LPCTSTR lpszName = MAKEINTRESOURCE(nID);
HINSTANCE hInst;
int nLen;
// first check the main module state
AFX_MODULE_STATE* pModuleState = AfxGetModuleState();
if (!pModuleState->m_bSystem)
{
hInst = AfxGetResourceHandle();
if (::FindResource(hInst, lpszName, _T("RTF")) != NULL &&
(nLen = ::LoadRtfString(hInst, nID, lpszBuf, nMaxBuf)) != 0)
{
// found a non-zero string in app
return nLen;
}
}
// check non-system DLLs in proper order
AfxLockGlobals(CRIT_DYNLINKLIST);
for (CDynLinkLibrary* pDLL = pModuleState->m_libraryList; pDLL != NULL;
pDLL = pDLL->m_pNextDLL)
{
if (!pDLL->m_bSystem && (hInst = pDLL->m_hResource) != NULL &&
::FindResource(hInst, lpszName, _T("RTF")) != NULL &&
(nLen = ::LoadRtfString(hInst, nID, lpszBuf, nMaxBuf)) != 0)
{
AfxUnlockGlobals(CRIT_DYNLINKLIST);
return nLen;
}
}
AfxUnlockGlobals(CRIT_DYNLINKLIST);
// check language specific DLL next
hInst = pModuleState->m_appLangDLL;
if (hInst != NULL && ::FindResource(hInst, lpszName, _T("RTF")) != NULL &&
(nLen = ::LoadRtfString(hInst, nID, lpszBuf, nMaxBuf)) != 0)
{
// found a non-zero string in language DLL
return nLen;
}
// check the system module state
if (pModuleState->m_bSystem)
{
hInst = AfxGetResourceHandle();
if (::FindResource(hInst, lpszName, _T("RTF")) != NULL &&
(nLen = ::LoadRtfString(hInst, nID, lpszBuf, nMaxBuf)) != 0)
{
// found a non-zero string in app
return nLen;
}
}
// check system DLLs in proper order
AfxLockGlobals(CRIT_DYNLINKLIST);
for (pDLL = pModuleState->m_libraryList; pDLL != NULL; pDLL = pDLL->m_pNextDLL)
{
if (pDLL->m_bSystem && (hInst = pDLL->m_hResource) != NULL &&
::FindResource(hInst, lpszName, _T("RTF")) != NULL &&
(nLen = ::LoadRtfString(hInst, nID, lpszBuf, nMaxBuf)) != 0)
{
AfxUnlockGlobals(CRIT_DYNLINKLIST);
return nLen;
}
}
AfxUnlockGlobals(CRIT_DYNLINKLIST);
// did not find it
lpszBuf[0] = '\0';
return 0;
}
NGLIB_EXT_API CString LoadRtfString(UINT uID)
{
CString sText;
int nMaxBuf = 16384; // Pick a number, any number
LPTSTR pszText = sText.GetBuffer(nMaxBuf);
int nCount = ::LoadRtfString(uID, pszText, nMaxBuf);
sText.ReleaseBuffer(nCount);
return sText;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -