一个小小的精神胜利:TreeView控件实现资源列表

本想熟悉一下TreeView控件的使用,于是找了个资源列表作为为对象!
让一个程序的资源列表显示到TreeView控制是做到了,接着又想实用些,也来显示一下资源内容!
花了一天的时间翻阅MSDN和调试,终于实现了菜单的查看,哈!

高兴啊!如图:

这里先把TreeView控制的使用记录一下吧!
一、要使用TreeView 控件,需要先初始化一下,因为TreeView控件是包含在commctrl.dll里:
首先是包含:


#include
#pragma comment(lib,"comctl32.lib")

然后是初始控件:


  INITCOMMONCONTROLSEX Icc={sizeof(INITCOMMONCONTROLSEX)};
  Icc.dwICC=ICC_BAR_CLASSES;
  if(!InitCommonControlsEx(&Icc)){
    MessageBox(NULL,TEXT("Setup Status Falied!"),TEXT("Error"),0);
    return 0;
  }

二、接下来,在初始化消息里,设置一下TreeView的样式:


hTree=GetDlgItem(hDlg,IDC_TREELIST);
      DWORD dwStyle=GetWindowLong(hTree,GWL_STYLE);
      dwStyle |= (TVS_HASBUTTONS | TVS_LINESATROOT | TVS_HASLINES);
      SetWindowLong(hTree,GWL_STYLE,dwStyle);

三、为了枚举程序的资源,这里需要定义三个回调函数


BOOL EnumTypesFunc(HANDLE hModule,LPTSTR lpType,LONG lParam);    
BOOL EnumNamesFunc(HANDLE hModule, LPCTSTR lpType,LPTSTR lpName,LONG lParam);
BOOL EnumLangsFunc(HANDLE hModule,LPCTSTR lpType,LPCTSTR lpName,WORD wLang,LONG lParam);

四、下面就是调用并设置到TreeView控件上了:


void GenerateRTree(HWND hDlg){
  TCHAR szFilePath[MAX_PATH] = {0};
  memset(szFilePath,0,MAX_PATH);
  GetDlgItemText(hDlg,IDC_INFO,szFilePath,MAX_PATH-2);
  
  hExe = LoadLibrary(szFilePath);
  if (hExe == NULL){
    MessageBox(hDlg,TEXT("载入程序出错,拖入的不是一个有效的PE文件!"),TEXT("提示"),0);
    return;
  }
  
  EnumResourceTypes(hExe,(ENUMRESTYPEPROC)EnumTypesFunc,0);
  FreeLibrary(hExe);
  SendMessage(g_hStatus,SB_SETTEXT,1,(LPARAM)TEXT("  程序资源加载完毕!"));
}

BOOL EnumTypesFunc(HANDLE hModule,LPTSTR lpType,LONG lParam)
{
  DWORD dwType=0;
  char szBuffer[32];
  memset(szBuffer,0,32);
    if ((ULONG)lpType & 0xFFFF0000){
    dwType=atoi(lpType);
    }
    else{
    dwType=(USHORT)lpType;
    }
  switch(dwType){
    case RT_ACCELERATOR:
      wsprintf(szBuffer,"%s", "Accelerator");
      break;
    case RT_CURSOR:
      wsprintf(szBuffer,"%s", "光标");
      break;
    case RT_ANICURSOR:
    case RT_GROUP_CURSOR:
      wsprintf(szBuffer,"%s", "光标组");
      break;
    case RT_ICON:
      wsprintf(szBuffer,"%s", "图标");
      break;
    case RT_ANIICON:
    case RT_GROUP_ICON:
      wsprintf(szBuffer,"%s", "图标组");
      break;
    case RT_BITMAP:
      wsprintf(szBuffer,"%s", "位图");
      break;

    case RT_DLGINCLUDE:
    case RT_DIALOG:
      wsprintf(szBuffer,"%s", "对话框");
      break;
    
    case RT_FONTDIR:
      wsprintf(szBuffer,"%s", "字体目录");
      break;
    case RT_FONT:
      wsprintf(szBuffer,"%s", "字体");
      break;

    case RT_HTML:
      wsprintf(szBuffer,"%s", "HTML");
      break;
    case RT_MANIFEST:
      wsprintf(szBuffer,"%s", "Manifest");
      break;
    case RT_MENU:
      wsprintf(szBuffer,"%s", "菜单");
      break;
    case RT_MESSAGETABLE:
      wsprintf(szBuffer,"%s", "消息表");
      break;
    case RT_PLUGPLAY:
      wsprintf(szBuffer,"%s", "Plug&Play");
      break;
    case RT_RCDATA:
      wsprintf(szBuffer,"%s", "自定义");
      break;

    case RT_STRING:
      wsprintf(szBuffer,"%s", "字符串");
      break;
    case RT_VERSION:
      wsprintf(szBuffer,"%s", "Version");
      break;
    case RT_VXD:
      wsprintf(szBuffer,"%s", "VXD");
      break;
    default:
      wsprintf(szBuffer,"%d", dwType);
      break;
  }
  
  TVITEM item;
  item.mask=TVIF_TEXT|TVIF_PARAM;
  item.pszText=szBuffer;
  item.lParam=(LPARAM)dwType;

  TVINSERTSTRUCT tv;
  tv.hParent=NULL;
  tv.item=item;
  HTREEITEM hItem=TreeView_InsertItem(hTree,&tv);

    EnumResourceNames((HMODULE)hModule,lpType,(ENUMRESNAMEPROC)EnumNamesFunc,(LONG_PTR)hItem);
    return TRUE;
}

BOOL EnumNamesFunc(HANDLE hModule, LPCTSTR lpType,LPTSTR lpName,LONG lParam)
{
  char szBuffer[32];
  memset(szBuffer,0,32);
    if ((ULONG)lpName & 0xFFFF0000){
    wsprintf(szBuffer,"%s", lpName);
    }
    else{
    wsprintf(szBuffer,"%u", (USHORT)lpName);
    }
  HTREEITEM hItem=(HTREEITEM)lParam;
  TVITEM item;
  item.mask=TVIF_TEXT;
  item.pszText=szBuffer;

  TVINSERTSTRUCT tv;
  tv.hParent=hItem;
  tv.item=item;
  HTREEITEM hThree=TreeView_InsertItem(hTree,&tv);
    EnumResourceLanguages((HMODULE)hModule,lpType,lpName,(ENUMRESLANGPROC)EnumLangsFunc,(LONG_PTR)hThree);

    return TRUE;
}

//    FUNCTION: EnumLangsFunc(HANDLE, LPSTR, LPSTR, WORD, LONG)
BOOL EnumLangsFunc(HANDLE hModule,LPCTSTR lpType,LPCTSTR lpName,WORD wLang,LONG lParam)
{
    char szBuffer[32];
  memset(szBuffer,0,32);

  wsprintf(szBuffer,"%u", (USHORT)wLang);

  HTREEITEM hThree=(HTREEITEM)lParam;
  TVITEM item;
  item.mask=TVIF_TEXT;
  item.pszText=szBuffer;

  TVINSERTSTRUCT tv;
  tv.hParent=hThree;
  tv.item=item;
  TreeView_InsertItem(hTree,&tv);
    return TRUE;
}

程序还没有完善,先不放工程了,呵呵!

发表评论