[Visual C++应用程序设计实验报告之对话框和按钮控件] c程序设计报告总结

  实验步骤与调试过程

 1.创建工作文件夹

  2.创建一个基于对话框的应用程序Ex_Research

  3.设计“上网问卷调查”对话框

  (1)按教程的步骤先将该对话框应用程序上机练习并通过

  (2)添加一个静态文本控件,标题为“你每天上网的平均时间”,保留默认的标识符

  (3)添加4个单选按钮控件,在其属性对话框中,分别将其标题设置为“<1小时”、“<2小时”、“<3小时”、“>3小时”,标示符分别为IDC_TIME_L1、 IDC_TIME_L2 、IDC_TIME_L3、 IDC_TIME_M4

  (4)选中第一个单选按钮的Group属性选项。

 (5)划蚀刻线,添加静态图片控件,在其属性对话框中将其类型属性调整为“框架”,颜色属性选择“蚀刻”。

 4.修改代码:

  (1)在OnInitDialog函数中添加如下代码:

  CheckRadioButton(IDC_TIME_L1,IDC_TIME_M3,IDC_TIME_L1);

  (2)在OnOk函数中添加如下代码:

 str=str+"\n你每天平均上网的时间: \n";

 nID=GetCheckedRadioButton(IDC_TIME_L1,IDC_TIME_M3);

 GetDlgItemText(nID,strCtrl);

 str=str+strCtrl;

 5.编译运行并测试

 实验结果

 正常运行,可以按照要求显示。

 由于初始化时已经设置了默认选,运行程序弹出对话框中每个提问均有选现象被选中,这样就避免了用户对某些问题未选择而带来的错误。当用户选择完毕后,单击确认后,便弹出另一个对话框,将用户所选择的信息显示出来。改程序界面简单明了,方便用户理解操作,而且对用户选择的内容及时反馈。

 疑难小结

 通过本次实验熟练掌握了对话框的使用和控件创建和使用的方法

 主要算法和程序清单

 BOOL CEx_ResearchDlg::OnInitDialog()

 {

  CDialog::OnInitDialog();

  // Add "About..." menu item to system menu.

  // IDM_ABOUTBOX must be in the system command range.

  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

  CheckRadioButton(IDC_AGE_L18,IDC_AGE_M38,IDC_AGE_18T27);

  CheckRadioButton(IDC_CM_FTTL,IDC_CM_OTHER,IDC_CM_FTTL);

  CButton* pBtn=(CButton*)GetDlgItem(IDC_DO_POP);

  pBtn->SetCheck(1); //使“收发邮件”复选框选中

 

  return TRUE; // return TRUE unless you set the focus to a control

 }

 d CEx_ResearchDlg::OnOK()

 {

  // TODO: Add extra validation here

  CString str,strCtrl;

  str="你的年龄";

  UINT nID=GetCheckedRadioButton(IDC_AGE_L18,IDC_AGE_M38);

  GetDlgItemText(nID,strCtrl);

  str=str+strCtrl;

  str=str+"\n你使用的接入方式:";

  nID=GetCheckedRadioButton(IDC_CM_FTTL,IDC_CM_OTHER);

  GetDlgItemText(nID,strCtrl);

  str=str+strCtrl;

  str=str+"\n你上网主要是:";

  UINT nCheckIDs[4]={IDC_DO_POP,IDC_DO_READ,IDC_DO_GAME,IDC_DO_OTHER};

  CButton* pBtn;

  for(int i=0;i<4;i++)

  {

  pBtn=(CButton*)GetDlgItem(nCheckIDs[i]);

  if(pBtn->GetCheck())

  {

  pBtn->GetWindowText(strCtrl);

  str=str+strCtrl;

  str=str+" ";

  }

  }

  MessageBox(str);

  CDialog::OnOK();

 }

 void CEx_ResearchDlg::OnPaint()

 {

  if (IsIconic())

  {

  CPaintDC dc(this); // device context for painting

  SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

  // Center icon in client rectangle

  int cxIcon = GetSystemMetrics(SM_CXICON);

  int cyIcon = GetSystemMetrics(SM_CYICON);

  CRect rect;

  GetClientRect(&rect);

  int x = (rect.Width() - cxIcon + 1) / 2;

  int y = (rect.Height() - cyIcon + 1) / 2;

  // Draw the icon

  dc.DrawIcon(x, y, m_hIcon);

  }

  else

  {

  CDialog::OnPaint();

  }

 }

推荐访问:告之 控件 对话框 程序设计 按钮