demo1 (创建一个属性表单)
demo2 (创建向导对话框)
demo3 (定制向导属性对话框的控制按钮)
demo4 (为属性页的单选按钮进行判断)
demo5 (为属性页的列表框进行判断)
demo6 (为属性页的复选框进行判断)
demo7 (为属性页的组合框进行判断)
demo8 (将属性页中的数据输出)
//demo1 (创建一个属性表单) /* 类CPropertyPage的对象表示一张属性表的各页,或者说是被看作是标签对话框。同标准对话框一样,要为属性表中每一页从CPropertyPage类中派生一个新类。要使用CPropertyPage派生对象,首先要创建一个CPropertySheet对象,然后为属性表中的每一页创建一个对象。为表中的每一页调用CPropertySheet::AddPage函数,然后对一个模式属性表调用CPropertySheet::DoModal函数来显示此属性表。对一个非模式属性表调用CPropertySheet::Create来显示此属性表。 */ //在资源视图中,插入三个属性为Dialog的属性页IDD_PROPPAGE_LARGE[English(U.S.)] //更改每个属性页的语言和字体。 //为每个属性页添加控件,如静态文本,组框,单选按钮,复选框,组合框,列表框。 //分别为每个属性页生成类CProp1,Cprop2,Cprop3,均继承自CPropertyPage类。 //在类视图中添加新类CPropSheet,继承自CPropertySheet类。 //在CPropSheet类中添加3个成员变量,类型分别为CProp1,Cprop2,Cprop3(添加头文件) //在CPropSheet的构造函数中调用AddPage() //在Menu上添加菜单项,并视图类中添加响应函数,生成CPropSheet类对象,并显示。 void CPropView::OnProp() { // TODO: Add your command handler code here CPropSheet ps("属性表单"); ps.DoModal(); }
//demo2 (创建向导对话框) /* To create a wizard-type dialog box, follow the same steps you would follow to create a standard property sheet, but call SetWizardMode before you call DoModal. To enable the wizard buttons, call SetWizardButtons, using flags to customize their function and appearance. To enable the Finish button, call SetFinishText after the user has taken action on the last page of the wizard. */ //步骤同demo1 //调用Domodal()之前,先调用SetWizardMode() void CPropView::OnProp() { // TODO: Add your command handler code here CPropSheet ps("属性表单"); ps.SetWizardMode(); ps.DoModal(); }
//demo3 (定制向导属性对话框的控制按钮) //A set of flags that customize the function and appearance of the wizard buttons. //Typically, you should call SetWizardButtons from CPropertyPage::OnSetActive. CPropertySheet::SetWizardButtons void SetWizardButtons( DWORD dwFlags ); //This member function is called by the framework when the page is chosen by the user and becomes //the active page. CPropertyPage::OnSetActive virtual BOOL OnSetActive( ); //之前步骤同demo1 //分别在CProp1、CProp2、CProp1三个类中重写虚函数OnSetActive( ) ////调用Domodal()之前,先调用SetWizardMode() BOOL CProp1::OnSetActive() { // TODO: Add your specialized code here and/or call the base class ((CPropSheet *)GetParent())->SetWizardButtons(PSWIZB_NEXT); return CPropertyPage::OnSetActive(); } BOOL CProp2::OnSetActive() { // TODO: Add your specialized code here and/or call the base class ((CPropSheet *)GetParent())->SetWizardButtons(PSWIZB_BACK|PSWIZB_NEXT); return CPropertyPage::OnSetActive(); } BOOL CProp3::OnSetActive() { // TODO: Add your specialized code here and/or call the base class ((CPropSheet *)GetParent())->SetWizardButtons(PSWIZB_FINISH|PSWIZB_BACK); return CPropertyPage::OnSetActive(); } void CPropView::OnProp() { // TODO: Add your command handler code here CPropSheet ps("属性表单"); ps.SetWizardMode(); ps.DoModal(); }
//demo4 (为属性页的单选按钮进行判断) //This member function is called by the framework when the user clicks on the Next button in a //wizard. CPropertyPage::OnWizardNext virtual LRESULT OnWizardNext(); //对提交的数据进行判断 //之前步骤同demo3 //在资源视图中,设置page1的第一个单选框属性为组(则其他单选框都为此组,知道遇到下一个组) //(为单选按钮判断)为第一个单选框在CProp1类中关联int型变量m_occupation //重写虚函数OnWizardNext(),当点击下一步时此函数被调用,判断用户是否进行了选择 LRESULT CProp1::OnWizardNext() { // TODO: Add your specialized code here and/or call the base class UpdateData(TRUE); if(m_occupation == -1) { MessageBox("请选择职业"); return -1; } return CPropertyPage::OnWizardNext(); }
//demo5 (为属性页的列表框进行判断) CListox::AddString //This method adds a string to the list box of a combo box. int AddString( LPCTSTR lpszString ); //为类表框添加数据,在WM_INITDIALOG的响应函数中,获取列表框的指针,调用AddString()(添加数据) //为列表框关联String类型变量(关联变量) //重写虚函数OnWizardNext(),当点击下一步时此函数被调用,判断用户是否进行了选择(判断变量) BOOL CProp1::OnInitDialog() { CPropertyPage::OnInitDialog(); //为列表框添加数据 ((CListBox *)GetDlgItem(IDC_LIST3))->AddString("北京"); ((CListBox *)GetDlgItem(IDC_LIST3))->AddString("天津"); ((CListBox *)GetDlgItem(IDC_LIST3))->AddString("沈阳"); ((CListBox *)GetDlgItem(IDC_LIST3))->AddString("珠海"); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } LRESULT CProp1::OnWizardNext() { // TODO: Add your specialized code here and/or call the base class UpdateData(TRUE); if(m_occupation == -1) { MessageBox("请选择职业"); return -1; } if(m_workAddr == "") { MessageBox("请选择地点"); return -1; } return CPropertyPage::OnWizardNext(); }
//demo6 (为属性页的复选框进行判断) //为在资源视图中建立类向导,为每个复选框分别关联Bool变量(关联变量) //重写虚函数OnWizardNext(),当点击下一步时此函数被调用,判断用户是否进行了选择(判断变量) LRESULT CProp2::OnWizardNext() { // TODO: Add your specialized code here and/or call the base class UpdateData(TRUE); if(m_football || m_basketball || m_volleyball || m_swim) return CPropertyPage::OnWizardNext(); else { MessageBox("请选择至少一个兴趣爱好"); return -1; } }
//demo7 (为属性页的组合框进行判断) CComboBox::AddString //This method adds a string to the list box of a combo box. int AddString( LPCTSTR lpszString ); CComboBox::SetCurSel//选择一条数据在组合框中显示 int SetCurSel( int nSelect ); CComboBox::GetCurSel //This method determines which item in the combo box is selected, and returns an index to the list. int GetCurSel( ) const; CComboBox::GetLBText//获取一条数据 int GetLBText( int nIndex, LPTSTR lpszText ) const; void GetLBText( int nIndex, CString& rString ) const; CPropertySheet::DoModal virtual int DoModal( ); CPropertySheet::DoModal virtual int DoModal( ); //在WM_INITDIALOG的响应函数中,获取组合框的指针,调用AddString() (添加数据) //在属性页中添加共有成员变量m_strSalary(CString) //重写OnWizardFinish() 虚函数,将选择的数据写入m_strSalary BOOL CProp3::OnInitDialog() { CPropertyPage::OnInitDialog(); // TODO: Add extra initialization here ((CComboBox *)GetDlgItem(IDC_COMBO1))->AddString("1000元以下"); ((CComboBox *)GetDlgItem(IDC_COMBO1))->AddString("1000-2000元"); ((CComboBox *)GetDlgItem(IDC_COMBO1))->AddString("2000-3000元"); ((CComboBox *)GetDlgItem(IDC_COMBO1))->AddString("3000元以上"); ((CComboBox *)GetDlgItem(IDC_COMBO1))->SetCurSel(0);//初始化 return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } BOOL CProp3::OnWizardFinish() { // TODO: Add your specialized code here and/or call the base class int index; index = ((CComboBox *)GetDlgItem(IDC_COMBO1))->GetCurSel();//获取索引 ((CComboBox *)GetDlgItem(IDC_COMBO1))->GetLBText(index,m_strSalary);//获取选择的数据 return CPropertyPage::OnWizardFinish(); }
//demo8 (将属性页中的数据输出) //在视图类中定义相关变量,并在构造函数中进行初始化 //提交后,收集属性页中数据,并使窗口无效 //窗口重绘后,在OnDraw中输出数据 private: int m_iOccupation; CString m_strWorkAddr; BOOL m_bLike[4]; CString m_strSalary; CPropView::CPropView() { // TODO: add construction code here m_iOccupation = -1; m_strWorkAddr = ""; memset(m_bLike,0,sizeof(m_bLike)); m_strSalary = ""; } void CPropView::OnProp() { // TODO: Add your command handler code here CPropSheet ps("属性表单"); ps.SetWizardMode(); if(ps.DoModal() == ID_WIZFINISH) { m_iOccupation = ps.m_prop1.m_occupation; m_strWorkAddr = ps.m_prop1.m_workAddr; m_bLike[0] = ps.m_prop2.m_football; m_bLike[1] = ps.m_prop2.m_basketball; m_bLike[2] = ps.m_prop2.m_volleyball; m_bLike[3] = ps.m_prop2.m_swim; m_strSalary = ps.m_prop3.m_strSalary; Invalidate();//引起窗口无效,在OnDraw()中重绘 } } void CPropView::OnDraw(CDC* pDC) { CPropDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); CFont font; font.CreatePointFont(300,"华文行楷"); CFont *pOldFont; pOldFont = pDC->SelectObject(&font); CString strTemp; strTemp = "你的职业: "; switch(m_iOccupation) { case 0: strTemp+="程序员"; break; case 1: strTemp+="系统工程师"; break; case 2: strTemp+="项目经理"; break; default: break; } pDC->TextOut(0, 0, strTemp); strTemp = "你的工作地点:"; strTemp += m_strWorkAddr; TEXTMETRIC tm; pDC->GetTextMetrics(&tm); pDC->TextOut(0, tm.tmHeight, strTemp); strTemp = "你的兴趣爱好:"; if(m_bLike[0]) strTemp += "足球 "; if(m_bLike[1]) strTemp += "篮球 "; if(m_bLike[2]) strTemp += "排球 "; if(m_bLike[3]) strTemp += "游泳 "; pDC->TextOut(0, tm.tmHeight*2, strTemp); strTemp = "你的薪资水平:"; strTemp += m_strSalary; pDC->TextOut(0, tm.tmHeight*3, strTemp); pDC->SelectObject(pOldFont); // TODO: add draw code for native data here }