7.1 例题(5) 5.调用对话框并处理数组 (1)在视类中添加数组对象并初始化 protected CStringArray m strArray: CArray view: CArray View M/ TODO: add construction code here m strArray SetSize(5, 5); m strArray[0=111111111111 m strArray[2]="3333333333 m strArray[4]="5555555555
例题(5) 7.1 protected: CStringArray m_strArray; CArrayView::CArrayView() { // TODO: add construction code here m_strArray.SetSize(5,5); m_strArray[0] = "111111111111"; m_strArray[2] = "333333333333"; m_strArray[4] = "555555555555"; } 5.调用对话框并处理数组 (1)在视类中添加数组对象并初始化
7.1 例题(5) (2)编辑菜单资源 在菜单“编辑”下添加两个菜单项如下: ID号 Caption D EDIT ADD 设置添加 ID EDIT DELETE 删除 (3)添加 ID EDIT ADD菜单项的消息响应函数
例题(5) 7.1 (2)编辑菜单资源 在菜单“编辑”下添加两个菜单项如下: ID号 Caption ID_EDIT_ADD 设置添加 ID_EDIT_DELETE 删除 (3)添加ID_EDIT_ADD菜单项的消息响应函数
void CArray View: OnEditAddo CDIgAdd dIg dlg. m index =0 dIg. m str'Value ="Default Radio =0 nt result = dlg DoModal if (result == IDOK) if (dlg. m rAdio ==0) m strArray SetAt Grow(dlg. m nIndex, dIg. m str Value) else if (dlg. m rAdio = 1) m strArray Add(dlg. m str Value) else m strArray InsertAt(dlg. m nIndex, dIg. m str'Value, 1) validated
例题(5) void CArrayView::OnEditAdd() 7.1 { CDlgAdd dlg; dlg.m_nIndex = 0; dlg.m_strValue = "Default"; dlg.m_nRadio = 0; int result = dlg.DoModal(); if (result == IDOK) { if (dlg.m_nRadio == 0) m_strArray.SetAtGrow(dlg.m_nIndex, dlg.m_strValue); else if (dlg.m_nRadio == 1) m_strArray.Add(dlg.m_strValue); else m_strArray.InsertAt(dlg.m_nIndex, dlg.m_strValue, 1); Invalidate(); } }
(4)添加 ID EDIT DELETE菜单项的消息响应函数7 void CArray View OnEditDeleteO CDIg Delete dlg dIg. m rAdio=0; dIg. m nIndex=0; int result=dlg DoModal if(result==IDOK) if(dIg. m_rAdio==O) m_strArray. RemoveAllo else if(m strArray GetUpper Bound<dlg. m nIndex) AfxMessage Box( " The index is large than array upper bound! " else m strArray Remove At(dIg. m_nIndex) Invalidate 0
例题(5) 7.1 void CArrayView::OnEditDelete() { CDlgDeletedlg; dlg.m_nRadio= 0; dlg.m_nIndex= 0; int result = dlg.DoModal(); if (result == IDOK) { if (dlg.m_nRadio==0) m_strArray.RemoveAll(); else { if(m_strArray.GetUpperBound()< dlg.m_nIndex) AfxMessageBox("The index is large than array upper bound!"); else m_strArray.RemoveAt(dlg.m_nIndex); } Invalidate(); } } (4)添加ID_EDIT_DELETE菜单项的消息响应函数
(5)重载视类的 OnDraw()函数 7 void CArray View: On Draw(CDC* pDC) CArray Doc* pDoc= GetDocumento; ASSERT VALID(p Doc TEXTMETRIC textMetric pDC->Get TextMetrics(&textMetric) int fontHeight textMetric tmHeight int count=m strArray. Getsizeo int display Pos =10 for(int X=0; X<count; ++x) CString strValue =m strArray GetAt(x) pDC->TextOut(10, display Pos, strValue) display Pos + fontHeight
例题(5) 7.1 void CArrayView::OnDraw(CDC* pDC) { CArrayDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); TEXTMETRIC textMetric; pDC->GetTextMetrics(&textMetric); int fontHeight = textMetric.tmHeight; int count = m_strArray.GetSize(); int displayPos = 10; for (int x=0; x<count; ++x) { CString strValue = m_strArray.GetAt(x); pDC->TextOut(10, displayPos, strValue); displayPos += fontHeight; } } (5)重载视类的OnDraw()函数