CInputBox
一个 CDialog 派生类,
屏幕截图

在我的项目中,我通常需要在 C++ 中实现类似 Visual Basic 中的 InputBox
函数的功能。Nish [BusterBoy] 有一个从 CFrameWnd
派生的 CInputBox
类。虽然这个类提供了基本的功能,但我创建了自己的从 CDialog
派生的 CInputBox
类。CInputBox
类提供了 Visual Basic 中 InputBox
函数的所有功能。此外,它还提供了一些额外的不错的功能。
- 可以获取整数值,而不是字符串
- 可以显示浏览按钮来获取文件名
- 可以使用拖放来获取文件名
- 最重要的是,它提供了一个不错的界面 :)
如何使用该类?
#include "DialogHeaderCtrl.h" #include "FocusEditCtrl.h" enum InputBox_DataType { DataType_String, DataType_Integer }; enum InputBox_BrowseOption { Browse_None, Browse_Open, Browse_Save, Browse_Folder }; ///////////////////////////////////// // CInputBox dialog class CInputBox : public CDialog { // Construction public: void SetLabel (const CString &sLabel); void SetDefaultText (const CString &sDefault); void SetDesc (const CString &sDesc); void SetTitle (const CString &sTitle); void SetAllowEmpty (bool bAllowEmpty); void SetDataType (InputBox_DataType nDataType); void SetBrowseOption (InputBox_BrowseOption nBrowse); void SetIcon (UINT nIcon); CInputBox (CWnd* pParent = NULL); // nBrowse = Browse_None => Hide browse button // nBrowse = Browse_Open => Browse button opens Open File dialog // nBrowse = Browse_Save => Browse button opens Save File dialog // nBrowse = Browse_Folder => Browse button opens Folder dialog // nDataType = DataType_String => String // nDataType = DataType_Integer => Integer CInputBox ( const CString &sTitle, const CString &sDesc, const CString &sLabel, const CString &sDefault, UINT nIcon = NULL, InputBox_BrowseOption nBrowse = Browse_None, InputBox_DataType nDataType = DataType_String, bool bAllowEmpty = false, CWnd* pParent = NULL); // standard constructor // Dialog Data //{{AFX_DATA(CInputBox) enum { IDD = IDD_INPUTBOX_DIALOG }; CFocusEditCtrl m_Edit1; CDialogHeaderCtrl m_HeaderCtrl; CButton m_BrowseBtn; CString m_sLabel1; CString m_sValue; //}}AFX_DATA // Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CInputBox) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: CString m_sTitle, m_sDesc; InputBox_BrowseOption m_nBrowse; InputBox_DataType m_nDataType; bool m_bAllowEmpty; UINT m_nIcon; bool m_bInitialized; // Generated message map functions //{{AFX_MSG(CInputBox) virtual BOOL OnInitDialog(); afx_msg void OnBrowse(); virtual void OnOK(); //}}AFX_MSG DECLARE_MESSAGE_MAP() };以下是一些展示如何使用
CInputBox
类的示例CInputBox dlg;
dlg.SetTitle ("Enter yout title here");
dlg.SetDesc ("Enter your description here");
dlg.SetLabel ("Enter your label here");
dlg.SetDefaultText ("enter the default text to be shown here");
if (dlg.DoModal () == IDOK)
MessageBox (dlg.m_sValue, "InputBoxDemo", MB_OK | MB_ICONINFORMATION);
else
MessageBox ("Cancel selected", "InputBoxDemo", MB_OK | MB_ICONINFORMATION);
您可以使用 void SetTitle (const CString &sTitle)
方法设置标题。
您可以使用 void SetDesc (const CString &sDesc)
方法设置描述。
您可以使用 void SetLabel (const CString &sLabel)
方法设置标签。
您可以使用 void SetDefaultText (const CString &sDefault)
方法设置要在编辑控件中显示的初始文本。
您可以使用 void SetIcon (UINT nIcon)
方法设置要在对话框中显示的图标。这是一个例子
dlg.SetIcon (IDI_DOCUMENT);
您可以使用 void SetDataType (int nDataType)
方法设置数据类型。
- 如果
nDataType
为DataType_String
,则输入为字符串 - 如果
nDataType
为DataType_Integer
,则输入为数字。 仅接受 0123456789 作为输入
如果您希望能够获取空输入,请使用 SetAllowEmpty (bool bAllowEmpty)
方法。
- 如果
bAllowEmpty
为 false,则强制用户输入值。 - 如果
bAllowEmpty
为 true,则用户可以只将输入字段留空。
要显示浏览按钮,请使用 SetBrowseOption (int nBrowseOption)
方法。 参数 nBrowseOption
可以取 4 个值
- 如果
nBrowseOption
为Browse_None
,则隐藏浏览按钮 - 如果
nBrowseOption
为Browse_Open
,则浏览按钮可见,单击时显示“打开文件”对话框 - 如果
nBrowseOption
为Browse_Save
,则浏览按钮可见,单击时显示“保存文件”对话框 - 如果
nBrowseOption
为Browse_Folder
,则浏览按钮可见,单击时显示“浏览文件夹”对话框
这里有更多的例子
CInputBox dlg;
dlg.SetTitle ("Find");
dlg.SetDesc ("Please enter the user name to be found");
dlg.SetLabel ("User name:");
dlg.SetIcon (IDI_DOCUMENT);
if (dlg.DoModal () == IDOK)
MessageBox (dlg.m_sValue, "InputBoxDemo", MB_OK | MB_ICONINFORMATION);
else
MessageBox ("Cancel selected", "InputBoxDemo", MB_OK | MB_ICONINFORMATION);
CInputBox dlg;
dlg.SetTitle ("Age");
dlg.SetDesc ("Please enter your age below.");
dlg.SetLabel ("Age:");
dlg.SetIcon (IDI_DOCUMENT);
dlg.SetDataType (DataType_Integer);
if (dlg.DoModal () == IDOK)
MessageBox (dlg.m_sValue, "InputBoxDemo", MB_OK | MB_ICONINFORMATION);
else
MessageBox ("Cancel selected", "InputBoxDemo", MB_OK | MB_ICONINFORMATION);
CInputBox dlg;
dlg.SetTitle ("Create Directory");
dlg.SetDesc ("Please enter the directory to be created.");
dlg.SetLabel ("Directory:");
dlg.SetDefaultText ("c:\\temp");
dlg.SetIcon (IDI_DIRECTORY);
dlg.SetBrowseOption (Browse_Folder);
if (dlg.DoModal () == IDOK)
MessageBox (dlg.m_sValue, "InputBoxDemo", MB_OK | MB_ICONINFORMATION);
else
MessageBox ("Cancel selected", "InputBoxDemo", MB_OK | MB_ICONINFORMATION);
或者您可以使用构造函数初始化所有成员变量
CInputBox dlg ("Create Directory", "Please enter the directory to be created",
"Directory:", "c:\\temp", IDI_DIRECTORY, Browse_Folder);
if (dlg.DoModal () == IDOK)
MessageBox (dlg.m_sValue, "InputBoxDemo", MB_OK | MB_ICONINFORMATION);
else
MessageBox ("Cancel selected", "InputBoxDemo", MB_OK | MB_ICONINFORMATION);
您应该记住的一些要点
- 要在您的项目中使用 CInputBox 类,您必须将文件
InputBox.h, InputBox.cpp, DialogHeaderCtrl.h, DialogHeaderCtrl.cpp, FocusEditCtrl.h 和 FocusEditCtrl.cpp
添加到您的项目中。 您还必须将对话框资源IDD_INPUTBOX_DIALOG
复制到您的项目中。 - 请记住,我的实现可能不是执行此类操作的最有效方法。 我既不是 GUI 大师,也不是天才 :)