第7章MFC通用类内容摘要:1.MFC中数组类的使用方法2.MFC中链表类的使用方法3.MFC中字符串类CString的使用方法4.MFC中日期和时间类的使用方法5.MFC中CPoint类、CSize类和CRect类的使用方法返回目录7.1数组类学习目标1.了解MFC中的数组类及其常用成员函数。2.利用MFC中的数组类处理数据。7.1返回第7章CByteArray:CDWordArray:CPtrArray:CUIntArray:CWordArray:CStringArray:7.1MFC的数组类1.intAdd(ARG_TYPEnewElement);throw(CMemoryException);2.TYPE&ElementAt(intnIndex);3.voidFreeExtra();4.TYPEGetAt(intnIndex)const5.intGetSize()const;6.intGetUpperBound()const;7.(1)voidInsertAt(intnIndex,ARG_TYPEnewElement,intnCount=1);throw(CMemoryException);(2)voidInsertAt(intnStartIndex,CArray*pNewArray);throw(CMemoryException);8.voidRemoveAll();9.voidSetAt(intnIndex,ARG_TYPEnewElement);10.voidSetAtGrow(intnIndex,ARG_TYPEnewElement);throw(CMemoryException);11.voidSetSize(intnNewSize,intnGrowBy=-1);throw(CMemoryException);7.1MFC数组类的常用成员函数例题(1--2)7.11.创建一个基于单文档的应用程序(Array)2.编辑对话框资源(1)IDD_DIALOG_ADDIDC_STATICGroupBoxCaption:设置添加操作IDC_RADIO_SETRadioButtonCaption:设置,Group:选中IDC_RADIO_ADDRadioButtonCaption:添加IDC_RADIO_INSERTRadioButtonCaption:插入IDC_STATICStaticTextCaption:数组元素下标IDC_EDIT_INDEXEditBoxIDC_STATICStaticTextCaption:设置值IDC_EDIT_VALUEEditBoxIDOKButtonCaption:确定IDCANCELButtonCaption:取消关联变量:IDC_RADIO_SETintm_nRidioIDC_EDIT_INDEXintm_nIndexIDC_EDIT_VALUECStringm_strValue例题(2)7.1(2)IDD_DIALOG_ADDIDC_STATICGroupBoxCaption:设置添加操作IDC_RADIO_ALLRadioButtonCaption:全部元素,Group:选中IDC_RADIO_ELEMENTRadioButtonCaption:指定元素IDC_STATICStaticTextCaption:数组下标IDC_EDIT_VALUEEditBoxIDOKButtonCaption:确定IDCANCELButtonCaption:取消关联变量:IDC_RADIO_ALLintm_nRidioIDC_EDIT_VALUEintm_nIndex例题(3)7.13.为对话框IDD_DIALOG_ADD添加响应函数BOOLCDlgAdd::OnInitDialog(){CDialog::OnInitDialog();//TODO:Addextrainitializationhere((CButton*)GetDlgItem(IDC_RADIO_SET))-SetCheck(1);returnTRUE;//returnTRUEunlessyousetthefocustoacontrol//EXCEPTION:OCXPropertyPagesshouldreturnFALSE}voidCDlgAdd::OnRadioSet(){//TODO:AddyourcontrolnotificationhandlercodehereGetDlgItem(IDC_EDIT_INDEX)-EnableWindow(true);}例题(3)7.1voidCDlgAdd::OnRadioAdd(){//TODO:AddyourcontrolnotificationhandlercodehereGetDlgItem(IDC_EDIT_INDEX)-EnableWindow(false);}voidCDlgAdd::OnRadioInsert(){//TODO:AddyourcontrolnotificationhandlercodehereGetDlgItem(IDC_EDIT_INDEX)-EnableWindow(true);}voidCDlgAdd::OnOK(){//TODO:AddextravalidationhereUpdateData(true);CDialog::OnOK();}例题(4)7.1BOOLCDlgDelete::OnInitDialog(){CDialog::OnInitDialog();//TODO:Addextrainitializationhere((CButton*)GetDlgItem(IDC_RADIO_ALL))-SetCheck(1);GetDlgItem(IDC_EDIT_INDEX)-EnableWindow(false);returnTRUE;//returnTRUEunlessyousetthefocustoacontrol//EXCEPTION:OCXPropertyPagesshouldreturnFALSE}voidCDlgDelete::OnRadioElement(){//TODO:AddyourcontrolnotificationhandlercodehereGetDlgItem(IDC_EDIT_INDEX)-EnableWindow(true);}4.为对话框IDD_DIALOG_DELETE添加响应函数例题(4)7.1voidCDlgDelete::OnRadioAll(){//TODO:AddyourcontrolnotificationhandlercodehereGetDlgItem(IDC_EDIT_INDEX)-EnableWindow(false);}voidCDlgDelete::OnOK(){//TODO:AddextravalidationhereUpdateData(true);CDialog::OnOK();}例题(5)7.1protected:CStringArraym_strArray;CArrayView::CArrayView(){//TODO:addconstructioncodeherem_strArray.SetSize(5,5);m_strArray[0]=111111111111;m_strArray[2]=333333333333;m_strArray[4]=555555555555;}5.调用对话框并处理数组(1)在视类中添加数组对象并初始化例题(5)7.1(2)编辑菜单资源在菜单“编辑”下添加两个菜单项如下:ID号CaptionID_EDIT_ADD设置添加ID_EDIT_DELETE删除(3)添加ID_EDIT_ADD菜单项的消息响应函数例题(5)7.1voidCArrayView::OnEditAdd(){CDlgAdddlg;dlg.m_nIndex=0;dlg.m_strValue=Default;dlg.m_nRadio=0;intresult=dlg.DoModal();if(result==IDOK){if(dlg.m_nRadio==0)m_strArray.SetAtGrow(dlg.m_nIndex,dlg.m_strValue);elseif(dlg.m_nRadio==1)m_strArray.Add(dlg.m_strValue);elsem_strArray.InsertAt(dlg.m_nIndex,dlg.m_strValue,1);Invalidate();}}例题(5)7.1voidCArrayView::OnEditDelete(){CDlgDeletedlg;dlg.m_nRadio=0;dlg.m_nIndex=0;intresult=dlg.DoModal();if(result==IDOK){if(dlg.m_nRadio==0)m_strArray.RemoveAll();else{if(m_strArray.GetUpperBound()dlg.m_nIndex)AfxMessageBox(Theindexislargethanarrayupperbound!);elsem_strArray.RemoveAt(dlg.m_nIndex);}Invalidate();}}(4)添加ID_EDIT_DELETE菜单项的消息响应函数例题(5)7.1voidCArrayView::OnDraw(CDC*pDC){CArrayDoc*pDoc=GetDocument();ASSERT_VALID(pDoc);TEXTMETRICtextMetric;pDC-GetTextMetrics(&textMetric);intfontHeight=textMetric.tmHeight;intcount=m_strArray.GetSize();intdisplayPos=10;for(intx=0;xcount;++x){CStringstrValue=m_strArray.GetAt(x);pDC-TextOut(10,displayPos,strValue);displayPos+=fontHeight;}}(5)重载视类的OnDraw()函数7.2链表类学习目标1.了解MFC中的链表类及其常用成员函数。2.利用MFC中的链表类处理数据。7.2返回第7章模板类ClistCTypedPtrList非模板类CObListCPtrListCStringList7.2MFC的链表类1.CList(intnBlockSize=10);2.TYPEGetHead()const;3.TYPEGetTail()const;4.RemoveHead()5.RemoveTail()6.原型1:POSITIONAddHead(ARG_TYPEnewElement);原型2:voidAddHead(CList*pNewList);7.原型1:POSITIONAddTail(ARG_TYPEnewElement);原型2:voidAddTail(CList*pNewList);8.RemoveAll()9.POSITIONGetHeadPosition()const;10.POSITIONGetTailPosition()const;11.TYPEGetNext(POSITION&rPosition)const;12.TYPEGetPrev(POSITION&rPosition)const;13.TYPEGetAt(POSITIONposition)const;14.voidSetAt(POSITIONpos,ARG_TYPEnewElement);7.2MFC链表类的常用成员函数——以Clist为例15.voidRemoveAt(POSITIONposition);16.POSITIONInsertBefore(POSITI