📄 nthelper.cpp
字号:
// NTHelper.cpp: implementation of the CNTHelper class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "NTHelper.h"
#ifdef _UNICODE
#include "afxpriv.h"
#endif
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CNTHelper::CNTHelper()
{
}
CNTHelper::~CNTHelper()
{
}
/////////////////////////////////////////////////////////////////////////////
// Get Windows NT Language Name in standard short name such as
// ENU, JPN, etc
//
// return: Language Name.
//
// Note: if API call failed, it will return default value ENU.
//
CString CNTHelper::GetNTLangName()
{
TCHAR strLangName[10];
memset( strLangName, 0, 10 );
if ( GetLocaleInfo( GetSystemDefaultLangID(),
LOCALE_SABBREVLANGNAME,
strLangName,
sizeof( strLangName ) ) ) // ENU, JPN etc.
{
return strLangName;
}
return _T("ENU");
}
BOOL CNTHelper::QueryKeyValue(HKEY hKey, CString strKey, CString& strValue)
{
DWORD dwType;
LPBYTE pBuffer = 0;
DWORD bufSize = 0;
strValue = _T("");
LONG nResult = RegQueryValueEx( hKey,
strKey,
0,
&dwType,
pBuffer,
&bufSize);
if (ERROR_SUCCESS != nResult)
{
return FALSE;
}
if (bufSize == 0 )
{
strValue = _T("");
return TRUE;
}
// bufSize should now tell us how much space we need...
pBuffer = (LPBYTE)malloc(bufSize);
nResult = RegQueryValueEx( hKey,
strKey,
0,
&dwType,
pBuffer,
&bufSize);
if (ERROR_SUCCESS == nResult)
{
strValue = (LPCTSTR)pBuffer;
free(pBuffer);
return TRUE;
}
else
{
free(pBuffer);
return FALSE;
}
}
/////////////////////////////////////////////////////////////////////////////
// Popup a window to ask user for file name
//
// strFileFilter: File Filter
// bSaveOrOpen: FALSE for Save File, TRUE or Open file caption
// strFileExtension: File Name extension
// strFileName: The place where the result will be placed
//
BOOL CNTHelper::PopWindowForFileName( CString strFileFilter,
BOOL bSaveOrOpen,
CString strFileExtension,
CString& strFileName
)
{
CFileDialogEx theFileDialog( bSaveOrOpen,
strFileExtension,
NULL,
OFN_HIDEREADONLY,
strFileFilter);
if (theFileDialog.DoModal() == IDOK)
{
strFileName = theFileDialog.GetPathName();
return TRUE;
}
else
{
return FALSE;
}
}
/////////////////////////////////////////////////////////////////////////////
// Return the directory of the current module file
//
// Suppose current module file is: E:\Products\DataAnalyst\bin\DataAnalyst.exe
// It will return E:\Products\DataAnalyst\bin
//
CString CNTHelper::GetModuleFileDir()
{
DWORD dwLength, dwSize;
TCHAR szFileName [MAX_PATH];
CString strFileName;
int nPos;
dwSize = sizeof (szFileName) / sizeof (szFileName [0]);
dwLength = ::GetModuleFileName (AfxGetInstanceHandle(), szFileName, dwSize);
if (dwLength <= 0)
{
return _T("");
}
strFileName = szFileName;
nPos = strFileName.ReverseFind( '\\' );
return strFileName.Left( nPos );
}
/////////////////////////////////////////////////////////////////////////////
// Function to get the system temporary path name
//
CString CNTHelper::GetTempFolder()
{
TCHAR buf[255];
GetTempPath( 255, buf );
CString strTmp( buf );
return strTmp;
}
/////////////////////////////////////////////////////////////////////////////
// Get system time in the following format
// YYYYMMDDHH24MISS
//
CString CNTHelper::GetSysTimeStamp()
{
CTime t = CTime::GetCurrentTime();
CString strTime;
strTime = t.Format("%Y%m%d%H%M%S");
return strTime;
}
/////////////////////////////////////////////////////////////////////////////
// Print a string to the default printer, p points to the string buffer,
// pSize holds the size information
//
void CNTHelper::PrintString(TCHAR *p, DWORD pSize, CString strTitle)
{
CDC dc;
CPrintDialog printDlg(FALSE);
CRect r;
int nHeight;
// ask the user to select a printer
if (printDlg.DoModal() == IDCANCEL)
return;
// For one line long string, it wraps the last word. If we add this,
// it will not wrap the last line.
p[pSize] = '\n';
pSize++;
// Attach a printer DC
dc.Attach(printDlg.GetPrinterDC());
dc.m_bPrinting = TRUE;
// use Textmappingmode, that's easiest to map the fontsize
dc.SetMapMode(MM_TEXT);
// setup font specifics
LOGFONT LogFont;
CFont aFont, *oldFont;
LogFont.lfHeight = -MulDiv(10, GetDeviceCaps(dc, LOGPIXELSY), 72);
LogFont.lfWidth = 0;
LogFont.lfEscapement = 0;
LogFont.lfOrientation = 0;
LogFont.lfWeight = 0;
LogFont.lfItalic = false;
LogFont.lfUnderline = 0;
LogFont.lfStrikeOut = 0;
LogFont.lfCharSet = DEFAULT_CHARSET; // So that it can deal with Chinese or Japanese
LogFont.lfOutPrecision = OUT_TT_PRECIS;
LogFont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
LogFont.lfQuality = DEFAULT_QUALITY;
LogFont.lfPitchAndFamily = DEFAULT_PITCH | FF_SWISS;
lstrcpy (LogFont.lfFaceName, _T("Courier New"));
dc.SetBkMode(OPAQUE);
aFont.CreateFontIndirect ( &LogFont );
// ok, we've build the font, now use it
oldFont = dc.SelectObject( &aFont );
// Initialise print document details
DOCINFO di;
::ZeroMemory ((void*)&di, sizeof (DOCINFO));
di.cbSize = sizeof (DOCINFO);
// application title appears in the spooler view
di.lpszDocName = strTitle;
// Begin a new print job
BOOL bPrintingOK = dc.StartDoc( &di );
// Get the printing extents and store in the m_rectDraw field of a
// CPrintInfo object
CPrintInfo Info;
int w = dc.GetDeviceCaps(HORZRES);
int h = dc.GetDeviceCaps(VERTRES);
Info.m_rectDraw.SetRect(100,100, w, h-200);
TCHAR *pStartAt = p;
long nTotalDone = 0;
long nLengthToGo = pSize;
dc.SelectObject( &aFont );
for (UINT page = Info.GetMinPage(); bPrintingOK && nTotalDone < nLengthToGo; page++)
{
// begin new page
dc.StartPage();
Info.m_nCurPage = page;
// calc how much text fits on one page
r = Info.m_rectDraw;
r.bottom = r.top;
long nLow = 0;
long nHigh = nLengthToGo - nTotalDone;
long nCount = nLengthToGo - nTotalDone;
r = Info.m_rectDraw;
// Use binary search algorithm to quickly find out the size of each page
while( nLow < nHigh )
{
r.right = Info.m_rectDraw.right;
nHeight = dc.DrawText(pStartAt, nCount, r, DT_CALCRECT|DT_WORDBREAK|DT_NOCLIP|DT_EXPANDTABS);
if ( nHeight < h )
{
nLow = nCount;
}
if ( nHeight > h )
{
nHigh = nCount;
}
if ( nLow == nHigh-1 )
{
nLow = nHigh;
}
if ( nLow < nHigh )
{
nCount = nLow + ( nHigh - nLow ) / 2;
}
}
// go one back to assure correct height
if (r.bottom >= Info.m_rectDraw.bottom)
{
nCount--;
}
// print that text
dc.DrawText(pStartAt, nCount, r, DT_WORDBREAK|DT_NOCLIP|DT_EXPANDTABS);
// go to next page
pStartAt += nCount;
nTotalDone += nCount;
// end page
bPrintingOK = (dc.EndPage() > 0);
}
// end a print job
if (bPrintingOK)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -