界面的多语言切换/通过读取xml文件实现

利用窗口遍历函数:

基本信息  函数功能:枚举一个父窗口的所有子窗口。

  函数原型:

  BOOL EnumChildWindows(HWND hWndParent,WNDENUMPROC lpEnumFunc, LPARAM lParam);

  各个参数如下:

  HWND hWndParent 父窗口句柄

  WNDENUMPROC lpEnumFunc 回调函数的地址

  LPARAM lParam 自定义的参数

  注意:回调函数的返回值将会影响到这个API函数的行为。如果回调函数返回true,则枚举继续直到枚举完成;如果返回false,则将会中止枚举。BOOL

 

其中回调函数实现如下:

 

int g_iEngChinese = 1; //为1则切换成中文
CXML pXml; //定义为全局对象,非指针变量,避免内存泄露

BOOL CALLBACK EnumChildProc(HWND hwnd, LPARAM lparam)
{
 pXml.Load("./Debug/Caption.xml");
 int lID = GetDlgCtrlID(hwnd); //根据窗口句柄获取它的id,生成在xml文件中用于查找的节点标石
 int iCount = 0;

//特殊id,先行处理,不做替换,直接return

 if (IDC_BUTTON_SWITCH == lID)
 {
  return TRUE;
 }

 if (lID >= 100000)
 {
  iCount = 6;
 }
 else if (lID >= 10000)
 {
  iCount = 5;
 }
 else if (lID >= 1000)
 {
  iCount = 4;
 }
 else if (lID >= 100)
 {
  iCount = 3;
 }
 else if (lID >= 10)
 {
  iCount = 2;
 }
 else
 {
  iCount = 1;
 }
 
 char strID[10] = {0};
 itoa(lID, strID, 10);


 for (; iCount >= 1; --iCount)
 {
  strID[iCount] = strID[iCount - 1];
 }
 strID[0] = 'A';


 //读取xml文件的公共操作,放在分支之前,代码复用
 pXml.RestPos();
 pXml.FineElem("id");

 if (1 == g_iEngChinese) //两个分支必走一支
 {
  pXml.FineChildElem("Chinese");
  pXml.intoElem();
  pXml.FindChildElem(strID);

  CWND::FromHandle(hwnd)->SetWindowText((LPCSTR)(pXml.GetChildData().c_str())); //根据窗口句柄设置空间的caption


  return TRUE; //返回TRUE,继续遍历其他子窗口,否则遍历将结束
 }
 else
 {
  pXml.FineChildElem("English");
  pXml.intoElem();
  pXml.FindChildElem(strID);

  CWND::FromHandle(hwnd)->SetWindowText((LPCSTR)(pXml.GetChildData().c_str()));


  return TRUE;

 }

}

 

调用窗口遍历函数形式:

 EnumChildWindows(HWND hWndParent,WNDENUMPROC lpEnumFunc, LPARAM lParam);

 EnumChildWindows(AfxGetApp()->m_pMainWnd, EnumChildProc, 0);

 

xml文件的使用和xml文件的创建:

<id> //必须有这个节点,

    <Chinese> //这个已经id的子节点

    <A1003 name = "IDC_BUTTON"> </A1003> //首字母必须为大写,是Chinese的子节点

  </Chinese>

  <English>

    </A1003 name = "IDC_BUTTON">HandeBook</A1003>

  </English>

</id>

posted @ 2011-08-22 14:18  wanglc_work  Views(788)  Comments(0Edit  收藏  举报