C,,实验五
实验题目:
年级:
姓名:
学号:
指导老师:
完成日期:
实验目的:
实验内容:
程序:
段(添加程序段 1 1开 :打开 MFC ClassWizard到,切换到 s Messsage Maps 页面,为对话框添加G WM_INITDIALOG 消息映射,并增加下列代码。)BOOL CCityDlg::OnInitDialog(){ CDialog::OnInitDialog();m_dwZipCode = 100000;// 设置初始的邮政编码 UpdateData(FALSE);// 将邮政编码显示在控件中 // 使[ [ 删除] ] 按钮灰显 GetDlgItem(IDC_BUTTON_DEL)->EnableWindow(FALSE);return TRUE;}(添加程序段 2 2 :在 e CCityDlg::IsValidate 函数输入下列代码)BOOL CCityDlg::IsValidate(){ UpdateData();m_strCity.TrimLeft();if(m_strCity.IsEmpty()){ MessageBox(_T("城市名输入无效!"));return FALSE;} return TRUE;} e //IsValidate 函数的功能是判断城市名编辑框中的内容是否是有效的字符 // 串。代码中,t TrimLeft 是 是 g CString 类的一个成员函数,用来去除字符串 // 左边的空格。
(添加程序段3 3 :打开 MFC ClassWizard,切换到 Messs age Maps 页面, , 为按钮 IDC_BUTTON_ADD添加 D BN_CLICKED 的消息映射,并增加下列代码)void CCityDlg::OnButtonAdd(){ if(!IsValidate())return;int nIndex = m_ListBox.FindStringExact(-1, m_strCity);if(nIndex!= LB_ERR){ MessageBox(_T("该城市已添加!"));return;} nIndex = m_ListBox.AddString(m_strCity);
m_ListBox.SetItemData(nIndex, m_dwZipCode);}((添加程序段 4 4 :用 d MFC ClassWizard 为列表框 1 IDC_LIST1 添加 E N_SELCHANGE 的消息映射,并增加下列代码。将会在编辑框中显示出城市名和邮政编码。))void CCityDlg::OnSelchangeList1(){ int nIndex = m_ListBox.GetCurSel();if(nIndex!= LB_ERR){ m_ListBox.GetText(nIndex, m_strCity);m_dwZipCode = m_ListBox.GetItemData(nIndex);UpdateData(FALSE);// 使用当前列表项所关联的内容显示在控件上 GetDlgItem(IDC_BUTTON_DEL)->EnableWindow(TRUE);} }(添加程序段 5 5 :用 d MFC ClassWizard 为按钮 IDC_BUTTON_DEL L 添加 D BN_CLICKED 的消息映射,并增加下列代码。)void CCityDlg::OnButtonDel(){ int nIndex = m_ListBox.GetCurSel();if(nIndex!= LB_ERR){ m_ListBox.DeleteString(nIndex);} else GetDlgItem(IDC_BUTTON_DEL)->EnableWindow(FALSE);}(添加程序段 6 6 :插入新增选项)void CCityDlg::OnButtonInsert(){ if(!IsValidate())return;int nIndex=m_ListBox.FindStringExact(-1,m_strCity);if(nIndex!=LB_ERR){ MessageBox(_T("城市名重名"));return;} nIndex=m_ListBox.GetCurSel();if(nIndex!=LB_ERR){ nIndex=m_ListBox.InsertString(nIndex,m_strCity);m_ListBox.SetItemData(nIndex,m_dwZipCode);} }(添加程序段 7 7 :列表框的多项选择)void CCityDlg::OnBUTTONMultiSel(){ int nCount=m_ListBox.GetSelCount();// 获取用户选中的项数
if(LB_ERR==nCount)return;int *buffer=new int[nCount];/ // / 开辟缓冲区 // 将各个选项的索引号内容放在缓冲区中 m_ListBox.GetSelItems(nCount,buffer);CString str=_T("");// 对字符串对象的初始化 m_strCity=_T("");// 清空显示城市名的编辑框 for(int i=0;i 实验内容: 程序: (添加程序段 1 1)BOOL CEx_FormDoc::OnNewDocument(){ if(!CDocument::OnNewDocument())return FALSE;return TRUE;}(添加程序段 2 2)void CEx_FormDoc::Serialize(CArchive& ar){ if(ar.IsStoring()){ // 得到主窗口指针 CMainFrame * pFrame=(CMainFrame *)AfxGetApp()->GetMainWnd();// 得到当前活动的视图指针 CEx_FormView* pView=(CEx_FormView*)pFrame->GetActiveView();m_strText=pView->m_strEdit;ar< 实验内容: 程序: (添加程序段 1 1)int nRes = m_wndTestBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD|WS_VISIBLE| CBRS_TOP |CBRS_GRIPPER|CBRS_TOOLTIPS|CBRS_FLYBY|CBRS_SIZE_DYNAMIC,CRect(0,0,0,0),AFX_IDW_TOOLBAR + 10);if(!nRes ||!m_wndTestBar.LoadToolBar(IDR_TOOLBAR1)){ TRACE0("Failed to create toolbarn");return-1;// fail to create } // 让工具栏可以停靠任何一边 m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);m_wndTestBar.EnableDocking(CBRS_ALIGN_ANY);EnableDocking(CBRS_ALIGN_ANY);// 让框架窗口可以被停靠 DockControlBar(&m_wndToolBar);// 让工具栏停靠在框架窗口 DockControlBar(&m_wndTestBar);return 0;}(添加程序段 2 2)int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct){ // 关闭测试工具栏 ShowControlBar(&m_wndTestBar, FALSE, FALSE);return 0;}(添加程序段 3 3)void CMainFrame::OnViewTest(){ m_NewMenu.Detach();// 使菜单对象和菜单句柄分离 m_NewMenu.LoadMenu(IDR_MENU1);SetMenu(NULL);// 清除应用程序菜单 SetMenu(&m_NewMenu);// 设置应用程序菜单 // 显示测试工具栏 ShowControlBar(&m_wndTestBar, TRUE, FALSE);// 关闭主工具栏 ShowControlBar(&m_wndToolBar, FALSE, FALSE);} void CMainFrame::OnTestReturn(){ m_NewMenu.Detach();// 使菜单对象和菜单句柄分离 m_NewMenu.LoadMenu(IDR_MAINFRAME);SetMenu(NULL);SetMenu(&m_NewMenu);// 关闭测试工具栏 ShowControlBar(&m_wndTestBar, FALSE, FALSE);// 显示主工具栏 ShowControlBar(&m_wndToolBar, TRUE, FALSE);} 结果: 实验内容: 程序: ((添加程序段 1 1 :将状态栏 s indicators 数组的定义改为下列代码))Static UINT indicators[]= { ID_TEXT_PANE }(添加程序段 2 2 :由于鼠标移动消息 E WM_MOUSEMOVE 在 在 e CMainFrame 类映射后不起作用,只能映射到 w CEx_SDIMouseView。 类中。e CMainFrame 类定义的成员变量,需要在 CEx_SDIMouseView类中添加访问 CMainFe rame 类的代码。)void CEx_SDIMouseView::OnMouseMove(UINT nFlags, CPoint point){ CString str;// 获得主窗口指针 CMainFrame* pFrame=(CMainFrame*)AfxGetApp()->m_pMainWnd;// 获得主窗口中的状态栏指针 CStatusBar* pStatus=&pFrame->m_wndStatusBar;if(pStatus){ str.Format("X=%d, Y=%d",point.x, point.y);// 格式化文本 pStatus->SetPaneText(1,str);// 更新第二个窗格的文本 } CView::OnMouseMove(nFlags, point);}(添加程序段 3 3)void CEx_SDIMouse1View::OnLButtonDblClk(UINT nFlags, CPoint point){ CString str;// 获得主窗口指针 CMainFrame* pFrame=(CMainFrame*)AfxGetApp()->m_pMainWnd;// 获得主窗口中的状态栏指针 CStatusBar* pStatus=&pFrame->m_wndStatusBar;if(pStatus){ str.Format("双击鼠标");// 格式化文本 pStatus->SetPaneText(1,str);// 更新第二个窗格的文本 } CView::OnLButtonDblClk(nFlags, point);} 结果:
