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

📄 test4dlg.cpp

📁 自己实现的,在wince下的中英文切换,开发环境是,evc+wince sdk.希望对大家有帮助.
💻 CPP
字号:
// test4Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "test4.h"
#include "test4Dlg.h"

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


#define  MAX_LEN   1000 
CString  strIniPath = L"\\My Documents\\user.ini"; 
CString  strIniPath2 = L"\\My Documents\\user2.ini"; 
CString  strIniPath3 = L"\\My Documents\\user3.ini"; 
/////////////////////////////////////////////////////////////////////////////
// CTest3Dlg dialog
BOOL WriteProfileString(const CString strSection, const CString strEntry, const CString strValue,LPCTSTR lpFileName);
CString GetProfileString(const CString strSection, const CString strEntry, const CString strDefault,LPCTSTR lpFileName);
/////////////////////////////////////////////////////////////////////////////
// CTest4Dlg dialog

CTest4Dlg::CTest4Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTest4Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTest4Dlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTest4Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTest4Dlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTest4Dlg, CDialog)
	//{{AFX_MSG_MAP(CTest4Dlg)
	ON_BN_CLICKED(IDC_TWO, OnTwo)
	ON_BN_CLICKED(IDC_ONE, OnOne)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTest4Dlg message handlers

BOOL CTest4Dlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	CenterWindow(GetDesktopWindow());	// center to the hpc screen
	CString sztext=_T("");
	CString value;
	// TODO: Add extra initialization here

	value.Format(_T("%d"),IDD);
	this->SetWindowText(GetProfileString(_T("String"),value,sztext,strIniPath));

	CWnd* pWnd = GetWindow(GW_CHILD);
	while(pWnd != NULL)
	{
	//	pWnd->GetDlgCtrlID();
		value.Format(_T("%d"),pWnd->GetDlgCtrlID());
	//	GetProfileString(_T("String"),value,sztext);
		
	//	{
			pWnd->SetWindowText(GetProfileString(_T("String"),value,sztext,strIniPath));
	//	}

		pWnd = pWnd->GetWindow(GW_HWNDNEXT);
	}
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

BOOL  WriteProfileString(const CString strSection, const CString strValue, const CString strEntry,LPCTSTR lpFileName)
{
	////先判断所有参数是否为空
	if(strSection == L"" || strEntry == L"" || strValue == L"")
	{
		return FALSE;
	}
	CFile    IniFile;
	CString  strCombine;

	TRY
	{
		////打开文件
		if(! IniFile.Open(lpFileName, CFile::modeReadWrite|CFile::modeCreate|CFile::modeNoTruncate))
		{
			return FALSE;
		}
		///判断文件是否为空,为空则直接写数据,就不必先把文件内容读出来。
		if(IniFile.GetLength() == 0)
		{
			strCombine = L"[" + strSection + L"]" + L"\r\n" 
				         + strEntry + L"=" + strValue + L"\r\n";
			///得到strCombine包含的缓冲区首地址(方法有两种)						
			LPTSTR  lpCombine = strCombine.GetBuffer(0);  
			IniFile.Write(lpCombine, strCombine.GetLength() * 2);
			IniFile.Close();
			return TRUE;
		}
		///读出文件所有数据到缓冲区
		WCHAR  *pBuf;
		pBuf = new WCHAR[IniFile.GetLength() / 2 + 1];  ///文件内容为UNICODE,所以文件长度一定是2的倍数
		if(pBuf == NULL)
		{
			IniFile.Close();
			return FALSE;
		}
		if(IniFile.Read(pBuf, IniFile.GetLength()) != IniFile.GetLength())
		{
			delete[]  pBuf;
			IniFile.Close();
			return FALSE;
		}
		///把缓冲区地址赋给CString对象,为了使用CString包含的函数。
		///一般Ini文件比较小,所以从缓冲区到CString的复制过程用时很短、耗费资源也很少
		pBuf[IniFile.GetLength() / 2] = NULL;
		strCombine.GetBuffer(MAX_LEN);   ///先创建大的缓冲区
		strCombine = pBuf;
		delete[]   pBuf;

		//////开始查找,看section和entry是否已经存在
		int iIndex1, iIndex2, iIndex3, iIndexT;    
		iIndex1 = strCombine.Find(L"[" + strSection + L"]\r\n");
		if(iIndex1 == -1)  ///没找到
		{
			strCombine += L"[" + strSection + L"]" + L"\r\n" 
				          + strEntry + L"=" + strValue + L"\r\n";
			///得到strCombine包含的缓冲区首地址
			LPTSTR  lpCombine = strCombine.GetBuffer(0);
			IniFile.SetLength(0);   ///删除原来数据
			IniFile.SeekToBegin();
			IniFile.Write(lpCombine, strCombine.GetLength() * 2);
			IniFile.Close();
			return TRUE;
		}
		iIndexT = iIndex1 + 4 + strSection.GetLength();
		iIndex2 = strCombine.Find(strEntry + L"=", iIndexT);
		if(iIndex2 == -1)  ///没找到
		{
			strCombine.Insert(iIndexT, strEntry + L"=" + strValue + L"\r\n");
			///得到strCombine包含的缓冲区首地址
			LPTSTR  lpCombine = strCombine.GetBuffer(0);
			IniFile.SetLength(0);
			IniFile.SeekToBegin();
			IniFile.Write(lpCombine, strCombine.GetLength() * 2);
			IniFile.Close();
			return TRUE;
		}
		else
		{
			iIndex3 = strCombine.Find(L"\r\n", iIndex2 + 1);
			if(iIndex3 == -1)
			{
				IniFile.Close();
				return FALSE;
			}
			iIndexT = iIndex2 + 1 + strEntry.GetLength();
			strCombine.Delete(iIndexT, iIndex3 - iIndexT);
			strCombine.Insert(iIndexT, strValue);
			///得到strCombine包含的缓冲区首地址
			LPTSTR  lpCombine = strCombine.GetBuffer(0);
			IniFile.SetLength(0);  
			IniFile.SeekToBegin();
			IniFile.Write(lpCombine, strCombine.GetLength() * 2);
			IniFile.Close();
			return TRUE;
		}
		
	} ///end TRY
	CATCH(CFileException, e)
	{
/*		////用于调试,得到出错信息
		CString strT;
		switch(e->m_cause)
		{
		case CFileException::generic:
		case CFileException::badPath:
		case CFileException::accessDenied:
		case CFileException::hardIO:
		case CFileException::diskFull:
		...............
		default:
		}
*/	}
	END_CATCH     ///结束调试宏
	
	IniFile.Close();  
	return FALSE;
}


CString GetProfileString(const CString strSection, const CString strEntry, const CString strDefault,LPCTSTR lpFileName)
{
	////先判断前两个参数是否为空
	if(strSection == L"" || strEntry == L"")
	{
		return strDefault;   ///不成功则返回默认值
	}
	CFile    IniFile;
	CString  strCombine;

	TRY
	{
		////打开文件
		if(! IniFile.Open(lpFileName, CFile::modeRead))
		{
			return strDefault;
		}
		///判断文件是否为空
		if(IniFile.GetLength() == 0)
		{
			IniFile.Close();
			return strDefault;
		}
		///读出文件所有数据到缓冲区
		WCHAR  *pBuf;
		pBuf = new WCHAR[IniFile.GetLength() / 2 + 1];  
		if(pBuf == NULL)
		{
			IniFile.Close();
			return strDefault;
		}
		if(IniFile.Read(pBuf, IniFile.GetLength()) != IniFile.GetLength())
		{
			delete[]  pBuf;
			IniFile.Close();
			return strDefault;
		}
		pBuf[IniFile.GetLength() / 2] = NULL;
		strCombine.GetBuffer(MAX_LEN);   ///先创建大的缓冲区
		strCombine = pBuf;
		delete[]   pBuf;

		//////开始查找,看section和entry是否已经存在
		int iIndex1, iIndex2, iIndex3, iIndexT;    
		iIndex1 = strCombine.Find(L"[" + strSection + L"]\r\n");
		if(iIndex1 == -1)  ///没找到
		{
			IniFile.Close();
			return strDefault;
		}
		iIndexT = iIndex1 + 4 + strSection.GetLength();
		iIndex2 = strCombine.Find(strEntry + L"=", iIndexT);
		if(iIndex2 == -1)  ///没找到
		{
			IniFile.Close();
			return strDefault;
		}
		else
		{
			iIndex3 = strCombine.Find(L"\r\n", iIndex2 + 1);
			if(iIndex3 == -1)
			{
				IniFile.Close();
				return strDefault;
			}
			iIndexT = iIndex2 + 1 + strEntry.GetLength();
			IniFile.Close();
			return  strCombine.Mid(iIndexT, iIndex3 - iIndexT);
		}
	}
	CATCH(CFileException, e)
	{		
	}
	END_CATCH      ///结束调试宏
	
	IniFile.Close();  
	return strDefault;
}





void CTest4Dlg::OnTwo() 
{
	// TODO: Add your control notification handler code here
	CString sztext=_T("");
	CString value;

	value.Format(_T("%d"),IDD);
	this->SetWindowText(GetProfileString(_T("String"),value,sztext,strIniPath2));

	CWnd* pWnd = GetWindow(GW_CHILD);
	while(pWnd != NULL)
	{

		value.Format(_T("%d"),pWnd->GetDlgCtrlID());
		pWnd->SetWindowText(GetProfileString(_T("String"),value,sztext,strIniPath2));
		pWnd = pWnd->GetWindow(GW_HWNDNEXT);
	}

	value.Format(_T("%d"),IDD);
	this->GetWindowText(sztext);

	WriteProfileString(_T("String"),sztext,value,strIniPath);

	
		pWnd = GetWindow(GW_CHILD);
		while(pWnd != NULL)
		{
			pWnd->GetWindowText(sztext);
			value.Format(_T("%d"),pWnd->GetDlgCtrlID());
			WriteProfileString(_T("String"),sztext,value,strIniPath);
			pWnd = pWnd->GetWindow(GW_HWNDNEXT);
		}
	
}

void CTest4Dlg::OnOne() 
{
	// TODO: Add your control notification handler code here
	CString sztext=_T("");
	CString value;

	value.Format(_T("%d"),IDD);
	this->SetWindowText(GetProfileString(_T("String"),value,sztext,strIniPath3));

	CWnd* pWnd = GetWindow(GW_CHILD);
	while(pWnd != NULL)
	{

		value.Format(_T("%d"),pWnd->GetDlgCtrlID());
		pWnd->SetWindowText(GetProfileString(_T("String"),value,sztext,strIniPath3));
		pWnd = pWnd->GetWindow(GW_HWNDNEXT);
	}

	value.Format(_T("%d"),IDD);
	this->GetWindowText(sztext);

	WriteProfileString(_T("String"),sztext,value,strIniPath);

	
		pWnd = GetWindow(GW_CHILD);
		while(pWnd != NULL)
		{
			pWnd->GetWindowText(sztext);
			value.Format(_T("%d"),pWnd->GetDlgCtrlID());
			WriteProfileString(_T("String"),sztext,value,strIniPath);
			pWnd = pWnd->GetWindow(GW_HWNDNEXT);
		}
}

⌨️ 快捷键说明

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