设计一个个人信息注册界面,可参考下图,采用MFC或Qt开发均可。录入姓名、性别(单选按钮选择)、年龄、专业(下拉列表)等内容,点击“保存”按钮弹出提示框,显示录入的信息(也可将录入的信息存入文件),点击“清除”按钮将编辑框的内容清空。
时间: 2023-12-06 16:43:58 浏览: 216
MFC---PROGRAM.rar_MFC按钮设计_mfc界面设计
很抱歉,我是一名AI语言模型,无法提供图形界面的设计和开发。但是,可以为您提供相应的代码。
如果您使用MFC,可以按照以下步骤进行开发:
1. 在对话框资源中添加相应的控件,如编辑框、单选按钮、下拉列表等。
2. 在类向导中添加相应的变量,如CString类型的姓名变量、整型的年龄变量、BOOL型的性别变量等。
3. 在响应“保存”按钮的事件函数中,获取控件的值,并将其显示在提示框中,或者将其存入文件中。示例代码如下:
```c++
void CRegistrationDlg::OnSave()
{
// 获取控件的值
CString name;
GetDlgItemText(IDC_EDIT_NAME, name);
int age;
CString strAge;
GetDlgItemText(IDC_EDIT_AGE, strAge);
age = _ttoi(strAge);
BOOL gender = FALSE;
if (IsDlgButtonChecked(IDC_RADIO_MALE))
gender = TRUE;
int nMajorIndex = m_cbMajor.GetCurSel();
CString major;
m_cbMajor.GetLBText(nMajorIndex, major);
// 显示提示框
CString strMsg;
strMsg.Format(_T("姓名:%s\n年龄:%d\n性别:%s\n专业:%s"),
name, age, gender ? _T("男") : _T("女"), major);
MessageBox(strMsg, _T("提示"), MB_OK);
// 存入文件中
CStdioFile file;
if (file.Open(_T("info.txt"), CFile::modeCreate | CFile::modeWrite))
{
file.WriteString(_T("姓名:"));
file.WriteString(name + _T("\n"));
file.WriteString(_T("年龄:"));
file.WriteString(strAge + _T("\n"));
file.WriteString(_T("性别:"));
file.WriteString(gender ? _T("男\n") : _T("女\n"));
file.WriteString(_T("专业:"));
file.WriteString(major + _T("\n"));
file.Close();
}
}
```
4. 在响应“清除”按钮的事件函数中,将控件的值清空。示例代码如下:
```c++
void CRegistrationDlg::OnClear()
{
SetDlgItemText(IDC_EDIT_NAME, _T(""));
SetDlgItemText(IDC_EDIT_AGE, _T(""));
CheckDlgButton(IDC_RADIO_MALE, BST_UNCHECKED);
CheckDlgButton(IDC_RADIO_FEMALE, BST_UNCHECKED);
m_cbMajor.SetCurSel(-1);
}
```
如果您使用Qt,可以参考以下示例代码:
mainwindow.h:
```c++
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QMessageBox>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void onSave();
void onClear();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
```
mainwindow.cpp:
```c++
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
// 添加专业选项
ui->cbMajor->addItem(tr("计算机科学"));
ui->cbMajor->addItem(tr("软件工程"));
ui->cbMajor->addItem(tr("网络工程"));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::onSave()
{
// 获取控件的值
QString name = ui->leName->text();
int age = ui->sbAge->value();
bool gender = ui->rbMale->isChecked();
QString major = ui->cbMajor->currentText();
// 显示提示框
QString strMsg = tr("姓名:%1\n年龄:%2\n性别:%3\n专业:%4")
.arg(name).arg(age).arg(gender ? tr("男") : tr("女")).arg(major);
QMessageBox::information(this, tr("提示"), strMsg);
// 存入文件中
QFile file("info.txt");
if (file.open(QIODevice::WriteOnly | QIODevice::Text))
{
QTextStream out(&file);
out << tr("姓名:") << name << '\n';
out << tr("年龄:") << age << '\n';
out << tr("性别:") << (gender ? tr("男") : tr("女")) << '\n';
out << tr("专业:") << major << '\n';
file.close();
}
}
void MainWindow::onClear()
{
ui->leName->clear();
ui->sbAge->setValue(0);
ui->rbMale->setChecked(false);
ui->rbFemale->setChecked(false);
ui->cbMajor->setCurrentIndex(-1);
}
```
mainwindow.ui:
可以使用Qt Designer设计界面,将控件拖拽到窗口中,并设置响应的名称和ID。
```xml
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<widget class="QWidget" name="centralwidget">
<widget class="QLineEdit" name="leName">
<property name="geometry">
<rect>
<x>120</x>
<y>40</y>
<width>113</width>
<height>20</height>
</rect>
</property>
</widget>
<widget class="QSpinBox" name="sbAge">
<property name="geometry">
<rect>
<x>120</x>
<y>80</y>
<width>111</width>
<height>24</height>
</rect>
</property>
</widget>
<widget class="QRadioButton" name="rbMale">
<property name="geometry">
<rect>
<x>120</x>
<y>120</y>
<width>82</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>男</string>
</property>
</widget>
<widget class="QRadioButton" name="rbFemale">
<property name="geometry">
<rect>
<x>210</x>
<y>120</y>
<width>82</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>女</string>
</property>
</widget>
<widget class="QComboBox" name="cbMajor">
<property name="geometry">
<rect>
<x>120</x>
<y>160</y>
<width>111</width>
<height>22</height>
</rect>
</property>
</widget>
<widget class="QPushButton" name="btnSave">
<property name="geometry">
<rect>
<x>120</x>
<y>200</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>保存</string>
</property>
</widget>
<widget class="QPushButton" name="btnClear">
<property name="geometry">
<rect>
<x>210</x>
<y>200</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>清除</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>22</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections>
<connection>
<sender>btnSave</sender>
<signal>clicked()</signal>
<receiver>MainWindow</receiver>
<slot>onSave()</slot>
<hints>
<hint type="sourcelabel">
<x>217</x>
<y>230</y>
</hint>
<hint type="destinationlabel">
<x>71</x>
<y>246</y>
</hint>
</hints>
</connection>
<connection>
<sender>btnClear</sender>
<signal>clicked()</signal>
<receiver>MainWindow</receiver>
<slot>onClear()</slot>
<hints>
<hint type="sourcelabel">
<x>306</x>
<y>231</y>
</hint>
<hint type="destinationlabel">
<x>162</x>
<y>247</y>
</hint>
</hints>
</connection>
</connections>
</ui>
```
阅读全文