運行環境: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消息映射,並增加下列代碼:
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設爲空,如下面的代碼:
class CScoreDlg : public CDialog
{
// Construction
public:
CString m_strOKText;
};
③ 用MFC ClassWizard爲CScoreDlg類映射WM_INITDIALOG消息,並添加下列代碼:
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函數中添加設置列表控件標題頭代碼:
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消息,並添加下列代碼:
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消息,並添加下列代碼:
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消息,並添加下列代碼:
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類的頭文件包含:
#include "stdafx.h"
#include "Ex_List.h"
#include "Ex_ListDlg.h"
#include "ScoreDlg.h"
編譯運行並測試結果。