“选择计算机”对话框





5.00/5 (3投票s)
2001年4月2日
3分钟阅读

207635

4443
该类实现了在 Windows 网络中选择用户(计算机)的对话框,包含 ATL 和 MFC 版本。
"选择计算机"对话框。为什么需要它?
如果您需要在 Windows 中使用用户管理或网络配置工具时,需要类似“选择计算机/用户/组”的对话框,那么“选择计算机”对话框非常方便。
看看下面的图片,您就会明白我在说什么
这个“选择计算机”对话框允许您在 Windows 网络环境的任何域中选择计算机(根据 WNetEnumResource()
函数的术语来说是“服务器”)。 我试图使这个对话框的行为尽可能接近原始 Microsoft 对话框。 我想我做到了。 所以 - 它是可调整大小的,支持多选和单选,并且,...嗯...它有效。
为了方便您,我提供了此对话框的 ATL 和 MFC 版本(正如您已经注意到的 - 演示项目也有两个版本)。 顺便说一句,ATL 版本在 WTL 环境中也能完美运行。
用法 - 分步指南
将以下文件添加到您的项目中:SelectComputerDialog.h、SelectComputerDialog.cpp、SELECT_COMPUTER_USER.ico、SELECT_COMPUTER_USERS.ico、SelectComputerDialog.rc、SelectComputerResource.h、SelectComputerNetwork.h、SelectComputerNetwork.cpp、Sizer.h、Sizer.cpp
将 SelectComputerDialog.rc 文件包含到您的项目资源脚本文件中
(从 Visual C++ 6 主菜单中)选择“查看”->“资源包含”,然后在“编译时指令”编辑字段中键入“#include "SelectComputerDialog.rc"”
在您想要使用该对话框的文件中包含 "SelectComputerDialog.h"。
#include "SelectComputerDialog.h"
ATL 示例代码
//here is the non-static method of CDialogImpl<T>-derived class //(actually it's a BN_CLICKED notification handler) CSelectComputerDialog dlg; //change dialog settings according to the check boxes state dlg.SingleSelection(IsDlgButtonChecked(IDC_CHECK_ALLOW_MULTIPLY_SELECTION)\ == BST_UNCHECKED); dlg.NoALLDomain(IsDlgButtonChecked(IDC_CHECK_ALLOW_ALL_DOMAINS) == BST_UNCHECKED); //show dialog if (dlg.DoModal() == IDOK) { typedef basic_string<TCHAR> tstring; tstring str; typedef pair<tstring, tstring> tstringpair; //filling string with names of selected computers vector<tstringpair>::const_iterator ci = dlg.GetComputerNames().begin(); for (; ci != dlg.GetComputerNames().end(); ++ci) { str += (ci->first + _T(" / ") + ci->second); str += _T("\r\n"); } SetDlgItemText(IDC_EDIT_COMPUTERNAME, str.c_str()); }
MFC 示例代码
//here is the non-static method of CDialog-derived class //(actually it's a BN_CLICKED notification handler) CSelectComputerDialog dlg; //change dialog settings according to the check boxes state dlg.SingleSelection(!m_bAllowMultiplySelection); dlg.NoALLDomain(!m_bAllowAllDomains); m_strComputerName = _T(""); //show dialog if (dlg.DoModal() == IDOK) { //get computer names typedef pair<CString, CString> CStringPair; vector<CStringPair>::const_iterator ci = dlg.GetComputerNames().begin() for (; ci != dlg.GetComputerNames().end(); ++ci) { m_strComputerName += (ci->first + _T(" / ") + ci->second); m_strComputerName += _T("\r\n"); } UpdateData(FALSE); }
我认为示例很简单 - 但我想稍微解释一下
SingleSelection(bool)
方法用于设置对话框的选择模式。 如果参数为 true
,则一次只能选择一个计算机名称,否则可以进行多选。
如果选择单选模式,则使用 GetComputerName()
获取所选计算机的名称,否则您必须使用 GetComputerNames()
方法,该方法返回 STL vector
的名称。 实际上,这两种方法都将计算机名称作为 STL pair
值返回,该值由计算机名称和域名组成。
这是这些方法的原型
对于 ATL 版本
const pair<basic_string<TCHAR>, basic_string<TCHAR> > & GetComputerName() const const vector<pair<basic_string<TCHAR>, basic_string<TCHAR> > > & GetComputerNames() const
对于 MFC 版本
const pair<CString, CString> & GetComputerName() const const vector<pair<CString, CString> >& GetComputerNames() const
NoALLDomain(bool)
方法用于设置对话框的模式,使其可以一次显示和选择来自不同域的计算机。 如果参数为 false
,则 (所有域)
项将添加到组合框。 如果您选择此“域”,则所有域的所有计算机将显示在列表控件中。
现在您已经知道在此项目中使用该类所需的一切。 但对于那些想要更多信息的人,我编写了下一章。
对话框内部结构
以下是我用于实现此对话框的类的简要说明
CSelectComputerDialog
此类提供 UI 和数据的接口,其中存储所选计算机的名称。 在 ATL 版本中,它直接派生自
CDialogImpl<CSelectComputerDialog>
,在 MFC 版本中,它直接派生自CDialog
。CSizer
此类提供调整对话框大小的功能,并可以相应地重新排列子控件,您可以控制允许的最小和最大大小。
CSelectComputerNetwork
这个类负责处理所有与网络相关的任务 - 枚举计算机和域。 实际上,我使用
WNetEnumResource()
函数来枚举网络资源。
如果枚举的资源被标识为RESOURCEDISPLAYTYPE_DOMAIN
对象,我将其显示为域,如果它被标识为RESOURCEDISPLAYTYPE_SERVER
对象,我将其显示为计算机。
计算机和域的名称从NETRESOURCE
结构的lpRemoteName
成员中读取。