您的位置:首页 > 编程语言 > C语言/C++

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

2017-06-21 18:23 567 查看
运行环境: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

void CScoreDlg::OnOK()   

{  

    // TODO: Add extra validation here  

    UpdateData();  

    m_strStuNo.TrimLeft();  

    if (m_strStuNo.IsEmpty())     

    {  

        MessageBox("学号不能为空!");    

        return;  

    }  

    m_strCourseNo.TrimLeft();  

    if (m_strCourseNo.IsEmpty())      

    {  

        MessageBox("课程号不能为空!");   

        return;  

    }  

    CDialog::OnOK();  

}  

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

[cpp] view
plain copy

class CScoreDlg : public CDialog  

{  

// Construction  

public:  

    CString m_strOKText;  

};  

③ 用MFC ClassWizard为CScoreDlg类映射WM_INITDIALOG消息,并添加下列代码:

[cpp] view
plain copy

BOOL CScoreDlg::OnInitDialog()   

{  

    CDialog::OnInitDialog();  

    if (!m_strOKText.IsEmpty())  

        GetDlgItem( IDOK )->SetWindowText( m_strOKText );  

    // TODO: Add extra initialization here  

      

    return TRUE;  // return TRUE unless you set the focus to a control  

                  // EXCEPTION: OCX Property Pages should return FALSE  

}  


4、完善CEx_ListDlg类代码


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

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

[cpp] view
plain copy

BOOL CEx_ListDlg::OnInitDialog()  

{  

    CDialog::OnInitDialog();  

       // 创建列表控件的标题头  

    CString strHeader[4]={ "学号", "课程", "成绩", "学分"};  

    for (int nCol=0; nCol<4; nCol++)  

        m_ListCtrl.InsertColumn(nCol,strHeader[nCol],LVCFMT_LEFT,80);  

    GetDlgItem( IDC_BUTTON_CHANGE )->EnableWindow(FALSE);  

    ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);  

    ASSERT(IDM_ABOUTBOX < 0xF000);  

  

    CMenu* pSysMenu = GetSystemMenu(FALSE);  

    if (pSysMenu != NULL)  

    {  

        CString strAboutMenu;  

        strAboutMenu.LoadString(IDS_ABOUTBOX);  

        if (!strAboutMenu.IsEmpty())  

        {  

            pSysMenu->AppendMenu(MF_SEPARATOR);  

            pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);  

        }  

    }  

  

    // Set the icon for this dialog.  The framework does this automatically  

    //  when the application's main window is not a dialog  

    SetIcon(m_hIcon, TRUE);         // Set big icon  

    SetIcon(m_hIcon, FALSE);        // Set small icon  

      

    // TODO: Add extra initialization here  

      

    return TRUE;  // return TRUE  unless you set the focus to a control  

}  

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

[cpp] view
plain copy

void CEx_ListDlg::OnButtonScore()   

{  

    // TODO: Add your control notification handler code here  

    CScoreDlg dlg;  

    if (IDOK != dlg.DoModal()) return;  

    int nItem = m_ListCtrl.GetItemCount();  

    m_ListCtrl.InsertItem( nItem, dlg.m_strStuNo );  

    m_ListCtrl.SetItemText( nItem, 1, dlg.m_strCourseNo );  

    CString str;  

    str.Format("%4.1f", dlg.m_fScore );  

    m_ListCtrl.SetItemText( nItem, 2, str );  

    str.Format("%3.1f", dlg.m_fCredit );  

    m_ListCtrl.SetItemText( nItem, 3, str );      

}  

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

[cpp] view
plain copy

void CEx_ListDlg::OnButtonChange()   

{  

    // TODO: Add your control notification handler code here  

    // 获取被选择的列表项索引号  

    POSITION pos;  

    pos = m_ListCtrl.GetFirstSelectedItemPosition();  

    if (pos == NULL)  

        {  

        MessageBox("你还没有选中列表项!");  

                return;  

    }  

    int nItem = m_ListCtrl.GetNextSelectedItem( pos );  

    CScoreDlg dlg;  

    dlg.m_strOKText = "修改";  

    dlg.m_strStuNo = m_ListCtrl.GetItemText( nItem, 0 );  

    dlg.m_strCourseNo = m_ListCtrl.GetItemText( nItem, 1 );  

    CString str = m_ListCtrl.GetItemText( nItem, 2 );  

    dlg.m_fScore = (float)atof( str );  

    str = m_ListCtrl.GetItemText( nItem, 3 );  

    dlg.m_fCredit = (float)atof( str );  

    if (IDOK != dlg.DoModal()) return;  

    m_ListCtrl.SetItemText( nItem, 0, dlg.m_strStuNo );  

    m_ListCtrl.SetItemText( nItem, 1, dlg.m_strCourseNo );  

    str.Format("%4.1f", dlg.m_fScore );  

    m_ListCtrl.SetItemText( nItem, 2, str );  

    str.Format("%3.1f", dlg.m_fCredit );  

    m_ListCtrl.SetItemText( nItem, 3, str );      

}  

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

[cpp] view
plain copy

void CEx_ListDlg::OnItemchangedList1(NMHDR* pNMHDR, LRESULT* pResult)   

{  

    NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;  

    // TODO: Add your control notification handler code here  

    GetDlgItem( IDC_BUTTON_CHANGE )->EnableWindow(TRUE);  

    *pResult = 0;  

}  

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

[cpp] view
plain copy

#include "stdafx.h"  

#include "Ex_List.h"  

#include "Ex_ListDlg.h"  

#include "ScoreDlg.h"  

编译运行并测试结果。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: