⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 afxcore.cpp

📁 股票软件
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*
	Cross Platform Core Code.

	Copyright(R) 2001-2002 Balang Software.
	All rights reserved.

	Using:
		some afx-global-functions;
*/

#include	"stdafx.h"

#include	<ctype.h>

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#ifdef	_DEBUG
#define	new	DEBUG_NEW
#endif


CFont * AfxSelectDCFont( CDC * pDC, int nHeight )
{
	static	CFont	font;
	font.DeleteObject();
	font.CreateFont(nHeight, 0, 0, 0, FW_NORMAL, FALSE, FALSE, 0,
		DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, PROOF_QUALITY, DEFAULT_PITCH,
		"楷体" );
	return	pDC->SelectObject( &font );
}

//////////////////////////////////////////////////////////////////////////////
// 注册表读写函数

BOOL AfxSetRegKey(LPCTSTR lpszKey, LPCTSTR lpszValue, LPCTSTR lpszValueName)
{
	if (lpszValueName == NULL)
	{
		if (::RegSetValue(HKEY_CLASSES_ROOT, lpszKey, REG_SZ,
			  lpszValue, lstrlen(lpszValue) * sizeof(TCHAR)) != ERROR_SUCCESS)
		{
			TRACE1("Warning: registration database update failed for key '%s'.\n",
				lpszKey);
			return FALSE;
		}
		return TRUE;
	}
	else
	{
		HKEY hKey;

		if(::RegCreateKey(HKEY_CLASSES_ROOT, lpszKey, &hKey) == ERROR_SUCCESS)
		{
			LONG lResult = ::RegSetValueEx(hKey, lpszValueName, 0, REG_SZ,
				(CONST BYTE*)lpszValue, (lstrlen(lpszValue) + 1) * sizeof(TCHAR));

			if(::RegCloseKey(hKey) == ERROR_SUCCESS && lResult == ERROR_SUCCESS)
				return TRUE;
		}
		TRACE1("Warning: registration database update failed for key '%s'.\n", lpszKey);
		return FALSE;
	}
}

LONG AfxGetRegKey(HKEY key, LPCTSTR subkey, LPTSTR retdata)
{
	HKEY hkey;
	LONG retval = RegOpenKeyEx(key, subkey, 0, KEY_QUERY_VALUE, &hkey);

	if (retval == ERROR_SUCCESS) {
		long datasize = MAX_PATH;
		TCHAR data[MAX_PATH];
		RegQueryValue(hkey, NULL, data, &datasize);
		lstrcpy(retdata,data);
		RegCloseKey(hkey);
	}

	return retval;
}

// recursively remove a registry key if and only if it has no subkeys
BOOL AfxDeleteRegKey(LPCTSTR lpszKey)
{
	// copy the string
	LPTSTR lpszKeyCopy = _tcsdup(lpszKey);
	LPTSTR lpszLast = lpszKeyCopy + lstrlen(lpszKeyCopy);

	// work until the end of the string
	while (lpszLast != NULL)
	{
		*lpszLast = '\0';
		lpszLast = _tcsdec(lpszKeyCopy, lpszLast);

		// try to open that key
		HKEY hKey;
		if (::RegOpenKey(HKEY_CLASSES_ROOT, lpszKeyCopy, &hKey) != ERROR_SUCCESS)
			break;

		// enumerate the keys underneath
		TCHAR szScrap[_MAX_PATH+1];
		DWORD dwLen = sizeof(szScrap);
		BOOL bItExists = FALSE;

		if (::RegEnumKey(hKey, 0, szScrap, dwLen) == ERROR_SUCCESS)
			bItExists = TRUE;
		::RegCloseKey(hKey);

		// found one?  quit looping
		if (bItExists)
			break;

		// otherwise, delete and find the previous backwhack
		::RegDeleteKey(HKEY_CLASSES_ROOT, lpszKeyCopy);
		lpszLast = _tcsrchr(lpszKeyCopy, '\\');
	}

	// release the string and return
	free(lpszKeyCopy);
	return TRUE;
}

HKEY AfxGetSectionKey( HKEY hKey, LPCTSTR szRegKeyGroup, LPCTSTR szRegKeyCompany, LPCTSTR szRegKeyApp, LPCTSTR lpszSection )
{
	HKEY hGroupKey = NULL;
	HKEY hCompanyKey = NULL;
	HKEY hAppKey = NULL;
	HKEY hSectionKey = NULL;
	if (RegOpenKeyEx( hKey, szRegKeyGroup, 0, KEY_WRITE|KEY_READ,
		&hGroupKey) == ERROR_SUCCESS)
	{
		DWORD dw;
		if (RegCreateKeyEx(hGroupKey, szRegKeyCompany, 0, REG_NONE,
			REG_OPTION_NON_VOLATILE, KEY_WRITE|KEY_READ, NULL,
			&hCompanyKey, &dw) == ERROR_SUCCESS )
		{
			RegCreateKeyEx(hCompanyKey, szRegKeyApp, 0, REG_NONE,
				REG_OPTION_NON_VOLATILE, KEY_WRITE|KEY_READ, NULL,
				&hAppKey, &dw);
		}
	}
	if (hGroupKey != NULL)
		RegCloseKey(hGroupKey);
	if (hCompanyKey != NULL)
		RegCloseKey(hCompanyKey);

	DWORD dw;
	if( NULL == hAppKey )
		return FALSE;
	RegCreateKeyEx(hAppKey, lpszSection, 0, REG_NONE,
		REG_OPTION_NON_VOLATILE, KEY_WRITE|KEY_READ, NULL,
		&hSectionKey, &dw);
	RegCloseKey(hAppKey);
	
	return hSectionKey;
}

BOOL AfxRegSetValue( LPCTSTR szRegKeyCompany, LPCTSTR szRegKeyApp, LPCTSTR lpszSection, LPCTSTR lpszEntry, LPCTSTR lpszValue )
{
	ASSERT( lpszSection && lpszEntry && lpszValue );
	if( !lpszSection || !lpszEntry || !lpszValue )
		return FALSE;
	
	HKEY hSectionKey = AfxGetSectionKey( HKEY_LOCAL_MACHINE, _T("Software"), szRegKeyCompany, szRegKeyApp, lpszSection );

	if (hSectionKey == NULL)
		return FALSE;
	LONG lResult = RegSetValueEx(hSectionKey, lpszEntry, NULL, REG_SZ,
		(LPBYTE)lpszValue, (lstrlen(lpszValue)+1)*sizeof(TCHAR));
	RegCloseKey(hSectionKey);

	return lResult == ERROR_SUCCESS;
}

CString	AfxRegQueryString(HKEY hKey, LPCTSTR szRegKeyGroup, LPCTSTR szRegKeyCompany, LPCTSTR szRegKeyApp, LPCTSTR lpszSection, LPCTSTR lpszEntry, LPCTSTR lpszDefault )
// hKey = HKEY_LOCAL_MACHINE, HKEY_CLASSES_ROOT, or so
{
	HKEY hSecKey = AfxGetSectionKey(hKey, szRegKeyGroup, szRegKeyCompany, szRegKeyApp, lpszSection);
	if (hSecKey == NULL)
		return lpszDefault;
	CString strValue;
	DWORD dwType, dwCount;
	LONG lResult = RegQueryValueEx(hSecKey, (LPTSTR)lpszEntry, NULL, &dwType,
		NULL, &dwCount);
	if (lResult == ERROR_SUCCESS)
	{
		ASSERT(dwType == REG_SZ);
		lResult = RegQueryValueEx(hSecKey, (LPTSTR)lpszEntry, NULL, &dwType,
			(LPBYTE)strValue.GetBuffer(dwCount/sizeof(TCHAR)), &dwCount);
		strValue.ReleaseBuffer();
	}
	RegCloseKey(hSecKey);
	if (lResult == ERROR_SUCCESS)
	{
		ASSERT(dwType == REG_SZ);
		return strValue;
	}
	return lpszDefault;
}

BOOL AfxRegQueryString(HKEY hKey, LPCTSTR szRegKeyGroup, LPCTSTR szRegKeyCompany, LPCTSTR szRegKeyApp,
					   CStringArray &astrName, CStringArray &astrData )
// hKey = HKEY_LOCAL_MACHINE, HKEY_CLASSES_ROOT, or so
{
	HKEY hSecKey = AfxGetSectionKey(hKey, szRegKeyGroup, szRegKeyCompany, szRegKeyApp, "");
	if (hSecKey == NULL)
		return FALSE;
	
	int	nIndex	=	0;
	while( TRUE )
	{
		CString strName, strData;
		CHAR szName[MAX_PATH];
		BYTE szData[MAX_PATH];
		DWORD dwType, dwNameLen=sizeof(szName), dwDataLen=sizeof(szData);
		if( ERROR_SUCCESS != RegEnumValue(hSecKey, nIndex, szName, &dwNameLen,
				NULL, &dwType, szData, &dwDataLen ) )
			break;
		if( dwType == REG_SZ )
		{
			astrName.Add( (LPCTSTR)szName );
			astrData.Add( (LPCTSTR)szData );
		}
		nIndex	++;
	}

	RegCloseKey(hSecKey);
	return astrName.GetSize() > 0;
}

HINSTANCE AfxGotoURL(LPCTSTR url, int showcmd)
{
	TCHAR key[MAX_PATH + MAX_PATH];

	// First try ShellExecute()
	HINSTANCE result = ShellExecute(NULL, _T("open"), url, NULL,NULL, showcmd);

	// If it failed, get the .htm regkey and lookup the program
	if ((UINT)result <= HINSTANCE_ERROR) {

		if (AfxGetRegKey(HKEY_CLASSES_ROOT, _T(".htm"), key) == ERROR_SUCCESS) {
			lstrcat(key, _T("\\shell\\open\\command"));

			if (AfxGetRegKey(HKEY_CLASSES_ROOT,key,key) == ERROR_SUCCESS) {
				TCHAR *pos;
				pos = _tcsstr(key, _T("\"%1\""));
				if (pos == NULL) {                     // No quotes found
					pos = strstr(key, _T("%1"));       // Check for %1, without quotes 
					if (pos == NULL)                   // No parameter at all...
						pos = key+lstrlen(key)-1;
					else
						*pos = '\0';                   // Remove the parameter
				}
				else
					*pos = '\0';                       // Remove the parameter

				lstrcat(pos, _T(" "));
				lstrcat(pos, url);
				result = (HINSTANCE) WinExec(key,showcmd);
			}
		}
	}

	return result;
}

BOOL AfxRegisterActiveXCtrls( LPCTSTR lpszOcxFileName )
{
	HINSTANCE hLib = LoadLibrary( lpszOcxFileName );
	if (hLib < (HINSTANCE)HINSTANCE_ERROR)
		return FALSE; 
  
	FARPROC   lpfnRegister;
	(FARPROC&)lpfnRegister = ::GetProcAddress( hLib, _T("DllRegisterServer") );
  
	if (lpfnRegister!= NULL)
	{
		(*lpfnRegister)();//Register control
		return TRUE;
	}
	return FALSE;
}

CString AfxGetVersionString( )
{
	static	CString	strVersion;
	if( strVersion.IsEmpty() )
	{
		strVersion	=	(LPCTSTR)AfxGetProfile().GetVersion( );
	}
	return strVersion;
}

CString AfxGetFileTitle( LPCTSTR lpszPathName )
{
	CString	strPathName	=	lpszPathName;
	if( strPathName.IsEmpty() )
		return "";
	UINT nTitleLen = AfxGetFileTitle( strPathName, NULL, 0 );
	if( nTitleLen <= 0 || nTitleLen-1 > (UINT)strPathName.GetLength() )
		return "";

	CString strTitle = strPathName.Right( nTitleLen-1 );
	return strTitle;
}

CString AfxGetFileTitleNoExt( LPCTSTR lpszPathName )
{
	CString	strPathName	=	lpszPathName;
	if( strPathName.IsEmpty() )
		return "";
	UINT nTitleLen = AfxGetFileTitle( strPathName, NULL, 0 );
	if( nTitleLen <= 0 || nTitleLen-1 > (UINT)strPathName.GetLength() )
		return "";

	CString strTitle = strPathName.Right( nTitleLen-1 );
	int nIndex = strTitle.ReverseFind( '.' );
	if( -1 != nIndex )
		strTitle = strTitle.Left( nIndex );
	return strTitle;
}

CString AfxGetFileDirectoryExist( LPCTSTR lpszPathName, LPCTSTR lpszDefault )
{
	CString	strPathName	=	lpszPathName;
	CString	strDefault	=	lpszDefault;
	if( strPathName.IsEmpty() )
		return strDefault;

	CString	strResult	=	strPathName;
	while( !strResult.IsEmpty()
		&& FILE_ATTRIBUTE_DIRECTORY != GetFileAttributes( strResult ) )
	{
		int	nIndex	=	strResult.ReverseFind( '\\' );
		if( -1 == nIndex )
			nIndex	=	strResult.ReverseFind( '/' );
		if( -1 == nIndex )
		{
			strResult = strDefault;
			break;
		}
		strResult	=	strResult.Left( nIndex );

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -