按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!
UINT nRadio=GetCheckedRadioButton(IDC_REMOVE0;IDC_REMOVE1);
switch(nRadio)
{
case IDC_REMOVE0:
m_radio=0;
break;
case IDC_REMOVE1:
m_radio=1;
break;
default:
break;
}
CDialog::OnOK();
}
8。 在ListView。h中CListView类的声明之前添加如下代码,用来定义
一个结构体CStudent,包含两个变量m_name和m_score,分别用于存
放学生的姓名和成绩。
…………………………………………………………Page 607……………………………………………………………
struct CStudent
{
CString m_name;
int m_score;
};
9。 为ClistView添加一个类型为CptrList的成员变量m_list。
10。 在ListView。cpp中添加下列语句:
#include 〃AddStudentDlg。h〃
#include 〃RemoveStudentDlg。h〃
11。 当用户单击左键后,弹出如图10。4所示的对话框,可以添加一个
节点。对应的OnLButtonDown 函数代码如下:
void CMyListView::OnLButtonDown(UINT nFlags; CPoint point)
{
// TODO: Add your message handler code here and/or call default
CAddStudentDlg dialog;
dialog。m_name = 〃〃;
dialog。m_score = 0 ;
// Display the dialog box。
int result = dialog。DoModal();
if (result == IDOK)
{
// Create and initialize the new node。
CStudent* m_pStudent = new CStudent;
m_pStudent…》m_name = dialog。m_name;
m_pStudent…》m_score = dialog。m_score;
// Add the node to the list。
…………………………………………………………Page 608……………………………………………………………
m_list。AddTail(m_pStudent);
// Repaint the window。
Invalidate();
}
CView::OnLButtonDown(nFlags; point);
}
12。 当用户单击右键后,弹出如图10。5所示的对话框,可以删除一个
节点。对应的OnRButtonDown 函数代码如下:
void CMyListView::OnRButtonDown(UINT nFlags; CPoint point)
{
// TODO: Add your message handler code here and/or call default
CRemoveStudentDlg dialog;
dialog。m_radio = 0;
// Display the dialog box。
int result = dialog。DoModal();
// If the user clicked the OK button。。。
if (result == IDOK)
{
CStudent* m_pStudent=new CStudent;
// Make sure the list isn't empty。
if (m_list。IsEmpty())
MessageBox(〃节点已经全部删除!〃);
else
{
// Remove the specified node。
if (dialog。m_radio == 0)
…………………………………………………………Page 609……………………………………………………………
m_pStudent = (CStudent*)m_list。RemoveHead();
else
m_pStudent = (CStudent*)m_list。RemoveTail();
// Delete the node object and repaint the window。
delete m_pStudent;
Invalidate();
}
}
CView::OnRButtonDown(nFlags; point);
}
13。 最后设置OnDraw 函数用来响应Invalidate()。
void CMyListView::OnDraw(CDC* pDC)
{
CListDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
TEXTMETRIC textMetric;
pDC…》GetTextMetrics(&textMetric);
int fontHeight = textMetric。tmHeight;
// Initialize values used in the loop。
POSITION pos = m_list。GetHeadPosition();
int displayPosition = 10;
// Iterate over the list; displaying each node's values。
while (pos != NULL)
{
CStudent* m_pStudent = (CStudent*)m_list。GetNext(pos);
…………………………………………………………Page 610……………………………………………………………
char s'81';
wsprintf(s; 〃 的成绩是 %d。〃;m_pStudent…》m_score);
CString m_string=m_pStudent…》m_name+s;
pDC…》TextOut(10; displayPosition; m_string);
displayPosition += fontHeight;
}
}
图10。 6 程序运行的初始窗口
14。 最后在CListView的析构函数中删除数组中所有的节点。
CMyListView::~CMyListView()
{
while (!m_list。IsEmpty())
{
CStudent* m_pStudent = (CStudent*)m_list。RemoveHead();
delete m_pStudent;
}
}
现在编译并运行这个程序,当程序运行后,弹出如图10。6所示的窗
口。单击左键,弹出如图10。4所示的对话框来添加节点。单击右键,
弹出如图10。5所示的对话框来删除节点。如果在没有节点删除节点,
将弹出如图10。7所示的对话框提示用户节点已经全部删除。
…………………………………………………………Page 611……………………………………………………………
图10。 7 没有节点时删除节点弹出的消息框
第三节 映射类
这个类用于创建关键对象和数值对象联系的集合。你可以使用MFC的
映射类创建查询表格。MFC的映射类包含CMapPtrToPtr;
CMapPtrToWord; CMapStringToOb; CMapStringToPtr;
CMapStringToString; CMapWordToOb; and CmapWordToPtr。在类的名
称中第一个数据类型是关键字的数据类型,第二个数据类型是对应的
数值的数据类型。
映射类有下列成员函数:
Lookup()
查询映射到指定关键字的值。
SetAt()
向映射中插入一个元素,如果指定的关键字存在,替换掉原来的元
素。
operator ' '
向映射中插入一个元素,其作用和SetAt()相同。
RemoveKey
查询符合关键字的映射。如果发现,则删除这个元素。
RemoveAll( )
删除映射中所有的元素。
GetStartPosition( )
获得映射中第一个元素的位置。映射中第一个元素是不预知的,所以
映射的第一个元素实际上没有特定的意义。一般将这个值传递给
GetNextAssoc 函数。
…………………………………………………………Page 612……………………………………………………………
GetNextAssoc
获得映射中指定位置处下一个元素。
GetCount( )
获得映射中元素的个数。
IsEmpty( )
测试这个数组元素是否为空。
现在用一个查询程序来使你对映射类有一个更深入的了解。按照下面
的步骤创建这个程序。
1。 创建一个单文档的应用程序Map。
2。 添加如图10。8所示的对话框,并生成基于这个对话框的类
CLookUpMapDlg;并为文本框添加一个变量m_key。
图10。 8 查询表格
3。 为CMapView添加一个CMapStringToString类型的成员变量m_map。
4。 在