mfc提供了CFileDialog标准对话框类,用来打开或保存文件,我们常用的软件也是使用这个类进行文件操作。
同时CFileDialog也提供了一系列函数,用来在标准对话框上添加额外的控件,例如添加一个按钮CButton或下拉框CComboBox等。
主要添加函数如下,摘录自CFileDialog头文件。
/// <summary>
/// Adds a button to the dialog.</summary>
/// <returns>
/// Returns S_OK if successful, or an error value otherwise.</returns>
/// <param name="dwIDCtl">The ID of the button to add</param>
/// <param name="strLabel">The button name</param>
HRESULT AddPushButton(DWORD dwIDCtl, const CString& strLabel);
/// <summary>
/// Adds a combo box to the dialog.</summary>
/// <returns>
/// Returns S_OK if successful, or an error value otherwise.</returns>
/// <param name="dwIDCtl">The ID of the combo box to add</param>
HRESULT AddComboBox(DWORD dwIDCtl);
/// <summary>
/// Adds an option button (also known as radio button) group to the dialog.</summary>
/// <returns>
/// Returns S_OK if successful, or an error value otherwise.</returns>
/// <param name="dwIDCtl">The ID of the option button group to add</param>
HRESULT AddRadioButtonList(DWORD dwIDCtl);
/// <summary>
/// Adds a check button to the dialog.</summary>
/// <returns>
/// Returns S_OK if successful, or an error value otherwise.</returns>
/// <param name="dwIDCtl">The ID of the check button to add</param>
/// <param name="strLabel">The check button name</param>
/// <param name="bChecked">A BOOL indicating the current state of the check button. TRUE if checked; FALSE otherwise</param>
HRESULT AddCheckButton(DWORD dwIDCtl, const CString& strLabel, BOOL bChecked);
/// <summary>
/// Adds an edit box to the dialog.</summary>
/// <returns>
/// Returns S_OK if successful, or an error value otherwise.</returns>
/// <param name="dwIDCtl">The ID of the edit box to add</param>
/// <param name="strText">The edit box name</param>
HRESULT AddEditBox(DWORD dwIDCtl, const CString& strText);
/// <summary>
/// Adds a separator to the dialog.</summary>
/// <returns>
/// Returns S_OK if successful, or an error value otherwise.</returns>
/// <param name="dwIDCtl">The ID of the separator add</param>
HRESULT AddSeparator(DWORD dwIDCtl);
/// <summary>
/// Adds text content to the dialog.</summary>
/// <returns>
/// Returns S_OK if successful, or an error value otherwise.</returns>
/// <param name="dwIDCtl">The ID of the text to add</param>
/// <param name="strText">The text name</param>
HRESULT AddText(DWORD dwIDCtl, const CString& strText);
/// <summary>
/// Sets the text associated with a control, such as button text or an edit box label.</summary>
/// <returns>
/// Returns S_OK if successful, or an error value otherwise.</returns>
/// <param name="dwIDCtl">The ID of the control</param>
/// <param name="strLabel">The control name</param>
HRESULT SetControlLabel(DWORD dwIDCtl, const CString& strLabel);
/// <summary>
/// Gets the current visibility and enabled states of a given control.</summary>
/// <returns>
/// Returns S_OK if successful, or an error value otherwise.</returns>
/// <param name="dwIDCtl">The ID of the control</param>
/// <param name="dwState">A reference to a variable that receives one or more values from the CDCONTROLSTATE enumeration that indicate the current state of the control.</param>
HRESULT GetControlState(DWORD dwIDCtl, CDCONTROLSTATEF& dwState);
/// <summary>
/// Sets the current visibility and enabled states of a given control.</summary>
/// <returns>
/// Returns S_OK if successful, or an error value otherwise.</returns>
/// <param name="dwIDCtl">The ID of the control</param>
/// <param name="dwState"> One or more values from the CDCONTROLSTATE enumeration that indicate the current state of the control.</param>
HRESULT SetControlState(DWORD dwIDCtl, CDCONTROLSTATEF dwState);
/// <summary>
/// Gets the current text in an edit box control.</summary>
/// <returns>
/// Returns S_OK if successful, or an error value otherwise.</returns>
/// <param name="dwIDCtl">The ID of the edit box</param>
/// <param name="strText">The text value</param>
HRESULT GetEditBoxText(DWORD dwIDCtl, CString& strText);
/// <summary>
/// Sets the current text in an edit box control.</summary>
/// <returns>
/// Returns S_OK if successful, or an error value otherwise.</returns>
/// <param name="dwIDCtl">The ID of the edit box</param>
/// <param name="strText">The text value</param>
HRESULT SetEditBoxText(DWORD dwIDCtl, const CString& strText);
/// <summary>
/// Gets the current state of a check button (check box) in the dialog.</summary>
/// <returns>
/// Returns S_OK if successful, or an error value otherwise.</returns>
/// <param name="dwIDCtl">The ID of the check box</param>
/// <param name="bChecked">check box state: TRUE means checked; FALSE, unchecked</param>
HRESULT GetCheckButtonState(DWORD dwIDCtl, BOOL& bChecked);
/// <summary>
/// Sets the current state of a check button (check box) in the dialog.</summary>
/// <returns>
/// Returns S_OK if successful, or an error value otherwise.</returns>
/// <param name="dwIDCtl">The ID of the check box</param>
/// <param name="bChecked">check box state: TRUE means checked; FALSE, unchecked</param>
HRESULT SetCheckButtonState(DWORD dwIDCtl, BOOL bChecked);
/// <summary>
/// Adds an item to a container control in the dialog.</summary>
/// <returns>
/// Returns S_OK if successful, or an error value otherwise.</returns>
/// <param name="dwIDCtl">The ID of the container control to which the item is to be added</param>
/// <param name="dwIDItem">The ID of the item</param>
/// <param name="strLabel">Item's text</param>
HRESULT AddControlItem(DWORD dwIDCtl, DWORD dwIDItem, const CString& strLabel);
/// <summary>
/// Removes an item from a container control in the dialog.</summary>
/// <returns>
/// Returns S_OK if successful, or an error value otherwise.</returns>
/// <param name="dwIDCtl">The ID of the container control from which the item is to be removed</param>
/// <param name="dwIDItem">The ID of the item</param>
HRESULT RemoveControlItem(DWORD dwIDCtl, DWORD dwIDItem);
/// <summary>
/// Gets the current state of an item in a container control found in the dialog.</summary>
/// <returns>
/// Returns S_OK if successful, or an error value otherwise.</returns>
/// <param name="dwIDCtl">The ID of the container control</param>
/// <param name="dwIDItem">The ID of the item</param>
/// <param name="dwState">A reference to a variable that receives one of more values from the CDCONTROLSTATE enumeration that indicate the current state of the control</param>
HRESULT GetControlItemState(DWORD dwIDCtl, DWORD dwIDItem, CDCONTROLSTATEF& dwState);
/// <summary>
/// Sets the current state of an item in a container control found in the dialog.</summary>
/// <returns>
/// Returns S_OK if successful, or an error value otherwise.</returns>
/// <param name="dwIDCtl">The ID of the container control</param>
/// <param name="dwIDItem">The ID of the item</param>
/// <param name="dwState">One or more values from the CDCONTROLSTATE enumeration that indicate the new state of the control</param>
HRESULT SetControlItemState(DWORD dwIDCtl, DWORD dwIDItem, CDCONTROLSTATEF dwState);
/// <summary>
/// Gets a particular item from specified container controls in the dialog.</summary>
/// <returns>
/// Returns S_OK if successful, or an error value otherwise.</returns>
/// <param name="dwIDCtl">The ID of the container control</param>
/// <param name="dwIDItem">The ID of the item that the user selected in the control</param>
HRESULT GetSelectedControlItem(DWORD dwIDCtl, DWORD& dwIDItem);
/// <summary>
/// Sets the selected state of a particular item in an option button group or a combo box found in the dialog.</summary>
/// <returns>
/// Returns S_OK if successful, or an error value otherwise.</returns>
/// <param name="dwIDCtl">The ID of the container control</param>
/// <param name="dwIDItem">The ID of the item that the user selected in the control</param>
HRESULT SetSelectedControlItem(DWORD dwIDCtl, DWORD dwIDItem);
/// <summary>
/// Declares a visual group in the dialog. Subsequent calls to any "add" method add those elements to this group.</summary>
/// <returns>
/// Returns S_OK if successful, or an error value otherwise.</returns>
/// <param name="dwIDCtl">The ID of the visual group</param>
/// <param name="strLabel">The group name</param>
HRESULT StartVisualGroup(DWORD dwIDCtl, const CString& strLabel);
/// <summary>
/// Stops the addition of elements to a visual group in the dialog.</summary>
/// <returns>
/// Returns S_OK if successful, or an error value otherwise.</returns>
HRESULT EndVisualGroup();
/// <summary>
/// Places a control in the dialog so that it stands out compared to other added controls.</summary>
/// <returns>
/// Returns S_OK if successful, or an error value otherwise.</returns>
/// <param name="dwIDCtl">The ID of the control</param>
HRESULT MakeProminent(DWORD dwIDCtl);
/// <summary>
/// Sets the text of a control item. For example, the text that accompanies a radio button or an item in a menu.</summary>
/// <returns>
/// Returns S_OK if successful, or an error value otherwise.</returns>
/// <param name="dwIDCtl">The ID of the container control</param>
/// <param name="dwIDItem">The ID of the item</param>
/// <param name="strLabel">Item's text</param>
HRESULT SetControlItemText(DWORD dwIDCtl, DWORD dwIDItem, const CString& strLabel);
下面是几个简单示例
1、添加按钮
CFileDialog dlg(TRUE);
dlg.AddPushButton(1, _T("一个按钮"));
dlg.DoModal();
2、添加复选框
CFileDialog dlg(TRUE);
dlg.AddCheckButton(2, _T("选中我"), TRUE);
dlg.DoModal();
3、一个组多个控件
CFileDialog dlg(TRUE);
dlg.StartVisualGroup(3, _T(""));
dlg.AddText(4, _T("一个组"));
dlg.AddCheckButton(5, _T("选项1"), FALSE);
dlg.AddCheckButton(6, _T("选项2"), FALSE);
dlg.EndVisualGroup();
dlg.DoModal();