📄 nthelper.cpp
字号:
{
dc.EndDoc();
}
else
{
// abort job.
dc.AbortDoc();
}
// restore font
dc.SelectObject(oldFont);
// free font memory
aFont.DeleteObject();
// detach the printer DC
dc.Detach();
}
/////////////////////////////////////////////////////////////////////////////
// Execute DOS Command
//
// strCmd: Command to be executed
// bSync: Whether sync or not
//
void CNTHelper::ExecuteCmd(CString strCmd, BOOL bSync /*=FALSE*/)
{
BOOL bRetCode;
STARTUPINFO StartupInfo;
GetStartupInfo( &StartupInfo );
PROCESS_INFORMATION Process_Information;
bRetCode = CreateProcess( NULL,
(LPTSTR)(LPCTSTR)strCmd,
NULL,
NULL,
FALSE,
CREATE_NO_WINDOW,
NULL,
NULL,
&StartupInfo,
&Process_Information );
if ( bSync )
{
WaitForSingleObject( Process_Information.hProcess, INFINITE );
}
}
/////////////////////////////////////////////////////////////////////////////
// Start File
//
// strFile: File to be opened
//
void CNTHelper::StartFile(CString &strFile)
{
CString strCmd;
strCmd.Format( _T("cmd /c \"%s\""), strFile );
ExecuteCmd( strCmd );
}
CNTHelper::OSType CNTHelper::GetOSType()
{
OSVERSIONINFO vinfo;
vinfo.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );
::GetVersionEx( &vinfo );
if( vinfo.dwPlatformId != VER_PLATFORM_WIN32_NT &&
vinfo.dwMajorVersion == 4 &&
vinfo.dwMinorVersion == 0 )
{
return WIN_95;
}
if( vinfo.dwPlatformId != VER_PLATFORM_WIN32_NT &&
vinfo.dwMajorVersion >= 4 &&
vinfo.dwMinorVersion > 0 &&
!( vinfo.dwMajorVersion == 4 && vinfo.dwMinorVersion == 90 ) )
{
return WIN_98;
}
if( vinfo.dwPlatformId != VER_PLATFORM_WIN32_NT &&
vinfo.dwMajorVersion == 4 &&
vinfo.dwMinorVersion == 90 )
{
return WIN_ME;
}
if( vinfo.dwPlatformId == VER_PLATFORM_WIN32_NT &&
vinfo.dwMajorVersion == 4 &&
vinfo.dwMinorVersion == 0 )
{
return WIN_NT;
}
if( vinfo.dwPlatformId == VER_PLATFORM_WIN32_NT &&
vinfo.dwMajorVersion == 5 &&
vinfo.dwMinorVersion == 0 )
{
return WIN_2000;
}
if( vinfo.dwPlatformId == VER_PLATFORM_WIN32_NT &&
vinfo.dwMajorVersion == 5 &&
vinfo.dwMinorVersion == 1 )
{
return WIN_XP;
}
// Default always return Windows NT
return WIN_NT;
}
/////////////////////////////////////////////////////////////////////////////
// Convert string to double
//
// strValue: String to be converted
// return: Double data type
//
double CNTHelper::_ttof( CString strValue )
{
#ifdef _UNICODE
USES_CONVERSION;
return atof( W2A( strValue ) );
#else
return atof( strValue );
#endif
}
/////////////////////////////////////////////////////////////////////////////
BOOL AFXAPI AfxUnhookWindowCreateEx()
{
_AFX_THREAD_STATE* pThreadState = AfxGetThreadState();
#ifndef _AFXDLL
if (afxContextIsDLL && pThreadState->m_hHookOldCbtFilter != NULL)
{
::UnhookWindowsHookEx(pThreadState->m_hHookOldCbtFilter);
pThreadState->m_hHookOldCbtFilter = NULL;
}
#endif
if (pThreadState->m_pWndInit != NULL)
{
pThreadState->m_pWndInit = NULL;
return FALSE; // was not successfully hooked
}
return TRUE;
}
void AFXAPI AfxHookWindowCreateEx(CWnd* pWnd)
{
_AFX_THREAD_STATE* pThreadState = AfxGetThreadState();
if (pThreadState->m_pWndInit == pWnd)
return;
if (pThreadState->m_hHookOldCbtFilter == NULL)
{
/*
pThreadState->m_hHookOldCbtFilter = ::SetWindowsHookEx(WH_CBT,
_AfxCbtFilterHook, NULL, ::GetCurrentThreadId());
if (pThreadState->m_hHookOldCbtFilter == NULL)
AfxThrowMemoryException();
*/
}
ASSERT(pThreadState->m_hHookOldCbtFilter != NULL);
ASSERT(pWnd != NULL);
ASSERT(pWnd->m_hWnd == NULL); // only do once
ASSERT(pThreadState->m_pWndInit == NULL); // hook not already in progress
pThreadState->m_pWndInit = pWnd;
}
/////////////////////////////////////////////////////////////////////////////
// CFileDialogEx
IMPLEMENT_DYNAMIC(CFileDialogEx, CFileDialog)
CFileDialogEx::CFileDialogEx(BOOL bOpenFileDialog, LPCTSTR lpszDefExt, LPCTSTR lpszFileName,
DWORD dwFlags, LPCTSTR lpszFilter, CWnd* pParentWnd) :
CFileDialog(bOpenFileDialog, lpszDefExt, lpszFileName, dwFlags, lpszFilter, pParentWnd)
{
}
BEGIN_MESSAGE_MAP(CFileDialogEx, CFileDialog)
//{{AFX_MSG_MAP(CFileDialogEx)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
int CFileDialogEx::DoModal()
{
ASSERT_VALID(this);
ASSERT(m_ofn.Flags & OFN_ENABLEHOOK);
ASSERT(m_ofn.lpfnHook != NULL); // can still be a user hook
// zero out the file buffer for consistent parsing later
ASSERT(AfxIsValidAddress(m_ofn.lpstrFile, m_ofn.nMaxFile));
DWORD nOffset = lstrlen(m_ofn.lpstrFile)+1;
ASSERT(nOffset <= m_ofn.nMaxFile);
memset(m_ofn.lpstrFile+nOffset, 0, (m_ofn.nMaxFile-nOffset)*sizeof(TCHAR));
// WINBUG: This is a special case for the file open/save dialog,
// which sometimes pumps while it is coming up but before it has
// disabled the main window.
HWND hWndFocus = ::GetFocus();
BOOL bEnableParent = FALSE;
m_ofn.hwndOwner = PreModal();
AfxUnhookWindowCreateEx();
if (m_ofn.hwndOwner != NULL && ::IsWindowEnabled(m_ofn.hwndOwner))
{
bEnableParent = TRUE;
::EnableWindow(m_ofn.hwndOwner, FALSE);
}
_AFX_THREAD_STATE* pThreadState = AfxGetThreadState();
ASSERT(pThreadState->m_pAlternateWndInit == NULL);
if (m_ofn.Flags & OFN_EXPLORER)
pThreadState->m_pAlternateWndInit = this;
else
AfxHookWindowCreateEx(this);
m_ofn.Flags = 2060;
int nResult;
if (m_bOpenFileDialog)
nResult = ::GetOpenFileName(&m_ofn);
else
nResult = ::GetSaveFileName(&m_ofn);
pThreadState->m_pAlternateWndInit = NULL;
// WINBUG: Second part of special case for file open/save dialog.
if (bEnableParent)
::EnableWindow(m_ofn.hwndOwner, TRUE);
if (::IsWindow(hWndFocus))
::SetFocus(hWndFocus);
PostModal();
return nResult ? nResult : IDCANCEL;
}
/////////////////////////////////////////////////////////////////////////////
// Set Registry Key Value
// hKey: Registry Key group
// strSection: Registry Key Section
// strKey: Registry Key
// strValue: Registry Value
//
BOOL CNTHelper::SetRegKeyValue(HKEY hKey, CString strSection, CString strKey, CString strValue)
{
HKEY hSecKey;
DWORD dwResult;
// Step 1. Open registry key
if ( ::RegOpenKeyEx( hKey, strSection, 0, KEY_ALL_ACCESS, &hSecKey ) != ERROR_SUCCESS )
{
if ( ::RegCreateKeyEx( hKey, strSection, 0, _T(""), REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hSecKey, &dwResult ) != ERROR_SUCCESS )
{
// Can not create registry key
return FALSE;
}
}
// Step 2. Set registry key value
#ifdef _UNICODE
USES_CONVERSION;
RegSetValueEx( hSecKey, strSection, 0, REG_SZ, (unsigned char*)W2A( strValue ), strValue.GetLength() );
#else
TCHAR szValue[1024];
sprintf( szValue, "%s", strValue );
RegSetValueEx( hSecKey, strSection, 0, REG_SZ, (LPBYTE)szValue, strValue.GetLength() );
#endif
// Step 3. Close registry key
RegCloseKey( hSecKey );
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// Set Registry Key Value
// hKey: Registry Key group
// strSection: Registry Key Section
// strKey: Registry Key
// strValue: Registry Value
//
BOOL CNTHelper::GetRegKeyValue(HKEY hKey, CString strSection, CString strKey, CString& strValue)
{
HKEY hSecKey;
DWORD dwResult;
// Step 1. Open registry key
if ( ::RegOpenKeyEx( hKey, strSection, 0, KEY_ALL_ACCESS, &hSecKey ) != ERROR_SUCCESS )
{
if ( ::RegCreateKeyEx( hKey, strSection, 0, _T(""), REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hSecKey, &dwResult ) != ERROR_SUCCESS )
{
// Can not create registry key
return FALSE;
}
}
// Step 2. Set registry key value
#ifdef _UNICODE
USES_CONVERSION;
#endif
QueryKeyValue( hSecKey, strKey, strValue );
// Step 3. Close registry key
RegCloseKey( hSecKey );
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -