使用目录内容建立菜单
2004-11-03 13:46
363 查看
目的:根据目录内容,建立一个菜单。菜单项为目录中的文件和子目录(以弹出方式显示)。
解决方案:遍历子目录,建立一个文件路径数组。菜单项的ID是数组的索引。当用户单击某个菜单项时,从数组中读取文件路径并执行相应的操作。
细节:
首先,我们需要一个菜单。新建立的菜单将作为此菜单的子菜单。
CMenu* pmenuFavorites=new CMenu;
pmenuFavorites->CreatePopupMenu();
然后读取目录,建立菜单
BuildFavoritesMenu(szPath, 0, pmenuFavorites);
最后,将菜单连接到已有菜单上面去
CMenu* pMenu=m_menuPopup.GetSubMenu(0);
pMenu->ModifyMenu(m_iMenuPosition,MF_BYPOSITION|MF_POPUP| MF_STRING,(UINT)(pmenuFavorites->GetSafeHmenu()),_T("动态菜单"));
pmenuFavorites->Detach();
delete pmenuFavorites;
其它的都很简单,但是建立这个菜单很麻烦
申明一个变量来存文件路径
CStringArray m_astrFavoriteURLs;
int CWorkBenchDlg::BuildMenu(LPCTSTR pszPath, int nStartPos, CMenu* pMenu)
{
CString strPath(pszPath);//path to start from
CString strPath2;//path to start from,with trailing backslash
CString str;//menu item text
WIN32_FIND_DATA wfd;
HANDLE h;
int nPos;
int nEndPos;
int nNewEndPos;
int nLastDir;
TCHAR buf[INTERNET_MAX_PATH_LENGTH];
CStringArray astrFavorites;
CStringArray astrDirs;
CMenu* pSubMenu;
// make sure there's a trailing backslash
if(strPath[strPath.GetLength() - 1] != _T('//'))
strPath += _T('//');
strPath2 = strPath;
strPath += _T("*.*");
// now scan the directory, first for files and then for subdirectories
//make a array of full pathnames.
h = FindFirstFile(strPath, &wfd);
if(h != INVALID_HANDLE_VALUE)
{
nEndPos = nStartPos;
do
{
if((wfd.dwFileAttributes & (FILE_ATTRIBUTE_DIRECTORY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM))==0)
{
str = wfd.cFileName;//file name
lstrcpy(buf,strPath2 + str);//file full pathname
if(str.Right(4) .CompareNoCase(_T(".url"))==0)
{
// an .URL file is formatted just like an .INI file, so we can
// use GetPrivateProfileString() to get the information we want
//fill the buf with URL
::GetPrivateProfileString(_T("InternetShortcut"), _T("URL"),
_T(""), buf, INTERNET_MAX_PATH_LENGTH,
strPath2 + str);
str = str.Left(str.GetLength() - 4);//the name of URL
}
if(str.Right(4) .CompareNoCase( _T(".lnk"))==0)
{
//fill the buf with link target
CGlobal::ResolveShortCut(NULL,strPath2 + str,buf);
str = str.Left(str.GetLength() - 4);
}
//TODO:add other format process here
//这里对.url文件和.lnk文件做了处理,去掉了扩展名。lnk文件的处理参见http://support.microsoft.com/support/kb/articles/Q130/6/98.asp 。可以对其他格式的文件进行处理并更改菜单文字。
// scan through the array and perform an insertion sort
// to make sure the menu ends up in alphabetic order
for(nPos = nStartPos ; nPos < nEndPos ; ++nPos)
{
if(str.CompareNoCase(astrFavorites[nPos]) < 0)
break;
}
astrFavorites.InsertAt(nPos, str);
m_astrFavoriteURLs.InsertAt(nPos, buf);
++nEndPos;
}
} while(FindNextFile(h, &wfd));
FindClose(h);
// Now add these items to the menu
for(nPos = nStartPos ; nPos < nEndPos ; ++nPos)
{
pMenu->AppendMenu(MF_STRING | MF_ENABLED, 0xe00 + nPos, astrFavorites[nPos]);
}
// now that we've got all files, check the subdirectories for more
nLastDir = 0;
h = FindFirstFile(strPath, &wfd);
ASSERT(h != INVALID_HANDLE_VALUE);
do
{
if(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
// ignore the current and parent directory entries
if(lstrcmp(wfd.cFileName, _T(".")) == 0 || lstrcmp(wfd.cFileName, _T("..")) == 0)
continue;
for(nPos = 0 ; nPos < nLastDir ; ++nPos)
{
if(astrDirs[nPos].CompareNoCase(wfd.cFileName) > 0)
break;
}
pSubMenu = new CMenu;
pSubMenu->CreatePopupMenu();
// call this function recursively.
nNewEndPos = BuildFavoritesMenu(strPath2 + wfd.cFileName, nEndPos, pSubMenu);
if(nNewEndPos != nEndPos)
{
// only intert a submenu if there are in fact files in the subdirectory
nEndPos = nNewEndPos;
pMenu->InsertMenu(nPos, MF_BYPOSITION | MF_POPUP | MF_STRING, (UINT)pSubMenu->m_hMenu, wfd.cFileName);
pSubMenu->Detach();
astrDirs.InsertAt(nPos, wfd.cFileName);
++nLastDir;
}
delete pSubMenu;
}
} while(FindNextFile(h, &wfd));
FindClose(h);
}
return nEndPos;
}
好了,菜单建立完了。万事大吉?没有。还要写命令处理函数。
afx_msg void OnMenu(UINT nID)
{
ShellExecute(NULL,NULL,m_astrFavoriteURLs[nID-0xe00],NULL,NULL,SW_SHOWDEFAULT);
}
和消息映射
……
//}}AFX_MSG_MAP
ON_COMMAND_RANGE(0xe00, 0xfff, OnMenu)
END_MESSAGE_MAP()
这里我使用了0xe00到0xfff作为命令ID的范围,所以最多有512个文件菜单项(够用吗?不够用自己写一个数好了)。因为通常命令的ID大于327xx,所以不会和其他菜单冲突(倒是可能会和按钮ID冲突,自己注意一下资源ID范围就OK啦)。
好了,编译,运行,通过!
唯一的遗憾是没有文件的图标。由于我对操作系统不是很熟悉,不知道怎么才能得到文件的图标并画到菜单上面去。欢迎各方高人指教!
在Windows ME和Visual C++6 SP5下测试通过。
相关文章推荐
- 使用目录内容建立菜单
- 使用目录内容建立菜单
- VB中使用API创建深层目录(建立多层文件夹)
- 使用递归从数据库读取数据来动态建立菜单
- 机器学习 101 Mahout 简介 建立一个推荐引擎 使用 Mahout 实现集群 使用 Mahout 实现内容分类 结束语 下载资源
- 关于使用DrawerLayout建立侧滑菜单时,下层布局依然可响应点击事件的解决方法
- 源程序List1.java,展示了如何使用list()方法来检查一个目录的内容的实例
- Rman使用前的准备工作,创建恢复目录所需要使用的表空间,在目录数据库创建rman用户,在目录数据库建立恢复目录
- windows的注册列表和使用注册列表建立右键菜单
- 关于如何在Android源码目录中建立自己的C工程,并用mm命令单独进行该C工程模块的编译,以及Android.mk的使用介绍(2)
- windows中建立以自己的名字命名的共享目录,在linux字符终端浏览共享目录中内容。
- 动态建立右键菜单(一级/二级/三级) 响应鼠标单击事件及修改项的内容
- 两种方法使用随机存取文件流RandomAccessFile实现根目录下文档内容任意位置的插入。
- 使用CLion建立多工程目录(简易)
- 百度编辑器使用 嵌入ci (可灵活运用,建立数据库,写入并读出,实现用户自定义网页页面内容)
- vue2.0使用嵌套路由实现页面内容切换/公用一级菜单控制页面内容切换
- symlink readlink link建立、读取,使用readlink获取当前程序目录
- 使用js实现内容可互换式菜单
- 使用纯php建立MVC框架,仿照Thinkphp的目录结构
- Windows 使用右键菜单删除svn 目录