服务器之家

服务器之家 > 正文

Vc++ 控件List Control用法总结

时间:2021-02-27 15:14     来源/作者:C++教程网

1.新建对话框MFC,在对话框上放一个ListCtrl

ID:IDC_PATH

View:Report

Vc++ 控件List Control用法总结

2.为ListCtrl添加变量

右击->添加变量m_wndPath

Vc++ 控件List Control用法总结

3.找到OnInitDialog()函数添加如下代码:

// TODO: 在此添加额外的初始化代码

m_wndPath.DeleteAllItems();//清空
m_wndPath.InsertColumn(0,_T("项目"));//添加列
m_wndPath.InsertColumn(1,_T("所在路径"));
m_wndPath.SetColumnWidth(0, 150);//设置列宽
m_wndPath.SetColumnWidth(1, 350);
m_wndPath.SetRedraw(FALSE);//防止重绘

int nIndex;
//char|TCHAR项目属性->字符集:使用多字节字符集
TCHAR Path[MAX_PATH+1];//TCHAR取代char MAX_PATH最长路径
nIndex=m_wndPath.InsertItem( 0,_T("Windows目录") );
if( nIndex < 0 ) return TRUE;
GetWindowsDirectory(Path,MAX_PATH);//取得windows目录
m_wndPath.SetItemText( nIndex, 1, Path );

LPITEMIDLIST pidl;//桌面CSIDL_DESKTOPDIRECTORY
//用来得到系统的某些特定文件夹的位置信息
if(SUCCEEDED(SHGetSpecialFolderLocation(NULL,CSIDL_DESKTOPDIRECTORY,&pidl)))
{
  if(SHGetPathFromIDList(pidl, Path))//功能是把项目标志符列表转换为文档系统路径
  {
    nIndex=m_wndPath.InsertItem( 0,_T("桌面"));//成功则返回0
    if( nIndex < 0 )
    {
      return TRUE;
    }
    m_wndPath.SetItemText( nIndex, 1, Path );
  }
}

m_wndPath.SetRedraw(TRUE);//显示

return TRUE; // 除非将焦点设置到控件,否则返回 TRUE

Vc++ 控件List Control用法总结

4.char被TCHAR取代,若要用则:

项目属性->字符集:使用多字节字符集

Vc++ 控件List Control用法总结

参考代码:

m_wndPath.DeleteAllItems();
m_wndPath.InsertColumn(0,"项目");
m_wndPath.InsertColumn(1,"所在路径");
m_wndPath.SetColumnWidth(0, 150);
m_wndPath.SetColumnWidth(1, 350);
m_wndPath.SetRedraw(FALSE);

int nIndex;
const int nFolder[]={ CSIDL_ALTSTARTUP,CSIDL_APPDATA,CSIDL_BITBUCKET,CSIDL_COMMON_ALTSTARTUP,
  CSIDL_COMMON_DESKTOPDIRECTORY,CSIDL_COMMON_FAVORITES,CSIDL_COMMON_PROGRAMS,
  CSIDL_COMMON_STARTMENU,CSIDL_COMMON_STARTUP,CSIDL_CONTROLS,CSIDL_COOKIES,
  CSIDL_DESKTOP,CSIDL_DESKTOPDIRECTORY,CSIDL_DRIVES,CSIDL_FAVORITES,CSIDL_FONTS,
  CSIDL_HISTORY,CSIDL_INTERNET,CSIDL_INTERNET_CACHE,CSIDL_NETHOOD,CSIDL_NETWORK,
  CSIDL_PERSONAL,CSIDL_PRINTERS,CSIDL_PRINTHOOD,CSIDL_PROGRAMS,CSIDL_RECENT,
  CSIDL_SENDTO,CSIDL_STARTMENU,CSIDL_STARTUP,CSIDL_TEMPLATES };
const CString strFolderName[]={ "CSIDL_ALTSTARTUP","CSIDL_APPDATA","回收站","CSIDL_COMMON_ALTSTARTUP",
  "CSIDL_COMMON_DESKTOPDIRECTORY","CSIDL_COMMON_FAVORITES","CSIDL_COMMON_PROGRAMS",
  "CSIDL_COMMON_STARTMENU","CSIDL_COMMON_STARTUP","控制面板","CSIDL_COOKIES",
  "CSIDL_DESKTOP","桌面","我的电脑","收藏夹","字体",
  "历史纪录","CSIDL_INTERNET","CSIDL_INTERNET_CACHE","网上邻居","CSIDL_NETWORK",
  "我的文档","打印机","CSIDL_PRINTHOOD","程序组","最近打开的文档",
  "发送","任务条启动菜单目录","启动目录","临时文档" };

char Path[MAX_PATH+1];
nIndex=m_wndPath.InsertItem( 0,"Windows目录" );
if( nIndex < 0 ) return TRUE;
GetWindowsDirectory(Path,MAX_PATH);
m_wndPath.SetItemText( nIndex, 1, Path );

nIndex=m_wndPath.InsertItem( 0,"System目录" );
if( nIndex < 0 ) return TRUE;
GetSystemDirectory(Path,MAX_PATH);
m_wndPath.SetItemText( nIndex, 1, Path );

int i,count=sizeof(nFolder)/sizeof(int);
for(i=0;i<count;i++)
{
  LPITEMIDLIST pidl;
  LPMALLOC pShellMalloc;

  if(SUCCEEDED(SHGetMalloc(&pShellMalloc)))
  {
    if(SUCCEEDED(SHGetSpecialFolderLocation(NULL,
      nFolder[i],&pidl)))
    {
      if(SHGetPathFromIDList(pidl, Path))
      {
        nIndex=m_wndPath.InsertItem( 0,strFolderName[i] );
        if( nIndex < 0 )
        {
          pShellMalloc->Free(pidl);
          pShellMalloc->Release();
          return TRUE;
        }
        m_wndPath.SetItemText( nIndex, 1, Path );
      }
      pShellMalloc->Free(pidl);
    }
    pShellMalloc->Release();
  }
}
m_wndPath.SetRedraw(TRUE);

以上所述就是本文的全部内容了,希望大家能够喜欢。

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部