例程 【C++】MFC 创建对话框,实现对“学生课程成绩”的管理

来源:互联网 发布:台湾误射导弹 知乎 编辑:程序博客网 时间:2024/05/02 05:01

运行环境:VC6.0

具备知识:对MFC的控件有一定的了解,包括图像列表、列表控件等。

实现功能:单击下图中的“学生课程成绩”按钮,弹出“学生课程成绩”对话框,单击“添加”按钮,学生课程成绩添加到列表控件。若选中列表项,“修改”按钮由原来的禁用变成可用,单击“修改”按钮,则弹出的“学生课程成绩”对话框中的“添加”按钮标题变成“修改”,单击“学生课程成绩”对话框中的“修改”按钮,该列表项的内容被修改。


1、创建对话框应用程序Ex_List,并设计其界面

① 选择“文件”→“新建”菜单,在弹出的“新建”对话框中选择“工程”页面,选择MFC AppWizard(exe),在工程框中输入Ex_List。

② 单击“确定”按钮,在出现的对话框中选择“基本对话(框)”应用程序类型,单击“完成”按钮。

③ 在对话框编辑器中,将对话框标题改为“列表控件”。

④ 调整对话框的大小,删除对话框中间的“TODO: 在这里设置对话控制。”静态文本控件和“确定”按钮控件,将“取消”按钮标题改为“退出”,并移至对话框的下方。

⑤ 添加两个按钮,一个是“学生课程成绩]按钮,ID为IDC_BUTTON_SCORE,另一个是“修改”按钮,ID为IDC_BUTTON_CHANGE。

⑥ 添加一个列表控件,取其默认ID号,将“查看”风格设为Report(报告),如图所示, 设置列表控件的“查看”风格。


2添加并设计“学生课程成绩”对话框

① 按Ctrl+R快捷键,弹出“插入资源”对话框,在资源类型列表中选择Dialog,单击“新建”按钮。

② 将该对话框资源的ID设为IDD_SCORE,标题设为“学生课程成绩”,”字体设为“宋体,10号”。

③ 将OK和Cancel按钮的标题改为“添加”和“取消”。

④ 打开对话框网格,参看图4.2的控件布局,为对话框添加如表所示的一些控件。

1  学生课程成绩对话框添加的控件

添加的控件

ID

标    题

其 他 属 性

编辑框(学号)

IDC_EDIT_STUNO

——

默认

编辑框(课程号)

IDC_EDIT_COURSENO

——

默认

编辑框(成绩)

IDC_EDIT_SCORE

——

默认

编辑框(学分)

IDC_EDIT_CREDIT

——

默认

 

 

 

 

⑤ 按Ctrl+W快捷键或双击对话框资源模板的空白处,为IDD_SCORE创建一个对话框类CScoreDlg。

⑥ 打开ClassWizard的Member Variables页面,看Class name是否是CScoreDlg,选中所需的控件ID号,双击鼠标或单击Add Variables按钮。依次为表4.2控件增加成员变量。

2  控件变量

控件ID

变 量 类 别

变 量 类 型

变  量  名

范围和大小

IDC_EDIT_STUNO

Value

CString

m_strStuNo

 

IDC_EDIT_COURSENO

Value

CString

m_strCourseNo

 

IDC_EDIT_SCORE

Value

float

m_fScore

 

IDC_EDIT_CREDIT

Value

float

m_fCredit

 

 

 

 

 

 

3、完善CScoreDlg类代码

① 用MFC ClassWizard为按钮IDOK添加BN_CLICKED消息映射,并增加下列代码:

[cpp] view plain copy
  1. void CScoreDlg::OnOK()   
  2. {  
  3.     // TODO: Add extra validation here  
  4.     UpdateData();  
  5.     m_strStuNo.TrimLeft();  
  6.     if (m_strStuNo.IsEmpty())     
  7.     {  
  8.         MessageBox("学号不能为空!");    
  9.         return;  
  10.     }  
  11.     m_strCourseNo.TrimLeft();  
  12.     if (m_strCourseNo.IsEmpty())      
  13.     {  
  14.         MessageBox("课程号不能为空!");   
  15.         return;  
  16.     }  
  17.     CDialog::OnOK();  
  18. }  

② 为CScoreDlg类添加一个公有型CString类型成员变量m_strOKText,用来设置IDOK按钮的标题,并在CScoreDlg类构造函数中,将m_strOKText设为空,如下面的代码:

[cpp] view plain copy
  1. class CScoreDlg : public CDialog  
  2. {  
  3. // Construction  
  4. public:  
  5.     CString m_strOKText;  
  6. };  

③ MFC ClassWizardCScoreDlg类映射WM_INITDIALOG消息,并添加下列代码:

[cpp] view plain copy
  1. BOOL CScoreDlg::OnInitDialog()   
  2. {  
  3.     CDialog::OnInitDialog();  
  4.     if (!m_strOKText.IsEmpty())  
  5.         GetDlgItem( IDOK )->SetWindowText( m_strOKText );  
  6.     // TODO: Add extra initialization here  
  7.       
  8.     return TRUE;  // return TRUE unless you set the focus to a control  
  9.                   // EXCEPTION: OCX Property Pages should return FALSE  
  10. }  

4、完善CEx_ListDlg类代码

① 用MFC ClassWizardCEx_ListDlg类添加列表控件(IDC_LIST1)变量m_ListCtrl,变量类型为CListCtrl

② 在CEx_ListDlg::OnInitDialog函数中添加设置列表控件标题头代码:

[cpp] view plain copy
  1. BOOL CEx_ListDlg::OnInitDialog()  
  2. {  
  3.     CDialog::OnInitDialog();  
  4.        // 创建列表控件的标题头  
  5.     CString strHeader[4]={ "学号""课程""成绩""学分"};  
  6.     for (int nCol=0; nCol<4; nCol++)  
  7.         m_ListCtrl.InsertColumn(nCol,strHeader[nCol],LVCFMT_LEFT,80);  
  8.     GetDlgItem( IDC_BUTTON_CHANGE )->EnableWindow(FALSE);  
  9.     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);  
  10.     ASSERT(IDM_ABOUTBOX < 0xF000);  
  11.   
  12.     CMenu* pSysMenu = GetSystemMenu(FALSE);  
  13.     if (pSysMenu != NULL)  
  14.     {  
  15.         CString strAboutMenu;  
  16.         strAboutMenu.LoadString(IDS_ABOUTBOX);  
  17.         if (!strAboutMenu.IsEmpty())  
  18.         {  
  19.             pSysMenu->AppendMenu(MF_SEPARATOR);  
  20.             pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);  
  21.         }  
  22.     }  
  23.   
  24.     // Set the icon for this dialog.  The framework does this automatically  
  25.     //  when the application's main window is not a dialog  
  26.     SetIcon(m_hIcon, TRUE);         // Set big icon  
  27.     SetIcon(m_hIcon, FALSE);        // Set small icon  
  28.       
  29.     // TODO: Add extra initialization here  
  30.       
  31.     return TRUE;  // return TRUE  unless you set the focus to a control  
  32. }  

③ 用MFC ClassWizard映射按钮IDC_BUTTON_SCOREBN_CLICKED消息,并添加下列代码:

[cpp] view plain copy
  1. void CEx_ListDlg::OnButtonScore()   
  2. {  
  3.     // TODO: Add your control notification handler code here  
  4.     CScoreDlg dlg;  
  5.     if (IDOK != dlg.DoModal()) return;  
  6.     int nItem = m_ListCtrl.GetItemCount();  
  7.     m_ListCtrl.InsertItem( nItem, dlg.m_strStuNo );  
  8.     m_ListCtrl.SetItemText( nItem, 1, dlg.m_strCourseNo );  
  9.     CString str;  
  10.     str.Format("%4.1f", dlg.m_fScore );  
  11.     m_ListCtrl.SetItemText( nItem, 2, str );  
  12.     str.Format("%3.1f", dlg.m_fCredit );  
  13.     m_ListCtrl.SetItemText( nItem, 3, str );      
  14. }  

④ 用MFC ClassWizard映射按钮IDC_BUTTON_CHANGEBN_CLICKED消息,并添加下列代码:

[cpp] view plain copy
  1. void CEx_ListDlg::OnButtonChange()   
  2. {  
  3.     // TODO: Add your control notification handler code here  
  4.     // 获取被选择的列表项索引号  
  5.     POSITION pos;  
  6.     pos = m_ListCtrl.GetFirstSelectedItemPosition();  
  7.     if (pos == NULL)  
  8.         {  
  9.         MessageBox("你还没有选中列表项!");  
  10.                 return;  
  11.     }  
  12.     int nItem = m_ListCtrl.GetNextSelectedItem( pos );  
  13.     CScoreDlg dlg;  
  14.     dlg.m_strOKText = "修改";  
  15.     dlg.m_strStuNo = m_ListCtrl.GetItemText( nItem, 0 );  
  16.     dlg.m_strCourseNo = m_ListCtrl.GetItemText( nItem, 1 );  
  17.     CString str = m_ListCtrl.GetItemText( nItem, 2 );  
  18.     dlg.m_fScore = (float)atof( str );  
  19.     str = m_ListCtrl.GetItemText( nItem, 3 );  
  20.     dlg.m_fCredit = (float)atof( str );  
  21.     if (IDOK != dlg.DoModal()) return;  
  22.     m_ListCtrl.SetItemText( nItem, 0, dlg.m_strStuNo );  
  23.     m_ListCtrl.SetItemText( nItem, 1, dlg.m_strCourseNo );  
  24.     str.Format("%4.1f", dlg.m_fScore );  
  25.     m_ListCtrl.SetItemText( nItem, 2, str );  
  26.     str.Format("%3.1f", dlg.m_fCredit );  
  27.     m_ListCtrl.SetItemText( nItem, 3, str );      
  28. }  

⑤ 用MFC ClassWizard映射列表控件IDC_LIST1LVN_ITEMCHANGED消息,并添加下列代码:

[cpp] view plain copy
  1. void CEx_ListDlg::OnItemchangedList1(NMHDR* pNMHDR, LRESULT* pResult)   
  2. {  
  3.     NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;  
  4.     // TODO: Add your control notification handler code here  
  5.     GetDlgItem( IDC_BUTTON_CHANGE )->EnableWindow(TRUE);  
  6.     *pResult = 0;  
  7. }  

⑥ 在Ex_ListDlg.cpp文件的前面添加CScoreDlg类的头文件包含:

[cpp] view plain copy
  1. #include "stdafx.h"  
  2. #include "Ex_List.h"  
  3. #include "Ex_ListDlg.h"  
  4. #include "ScoreDlg.h"  

编译运行并测试结果。

阅读全文
0 0
原创粉丝点击