逃跑按钮

来源:互联网 发布:百度账号的淘宝暗语 编辑:程序博客网 时间:2024/04/28 08:43

逃跑按钮

    此处,我们为我们创建的按钮又定义了一个新的类,以便来捕获鼠标消息,而没直接用CButton类对象来直接捕获鼠标消息消息是因为:1、一个对话框上可能有多个按钮,而其他的按钮我们并不想让其实现逃跑功能;
                          2、我们只能为我们自己定义的类添加我们关心的消息处理函数(在ClassView中的相应类上点右键,选add windows message....);<此处也就是将1否决了,所以1就成了不是原因的原因了>
    注意:此处的鼠标移动消息,我们是让我们自己定义的按钮类(对象)来捕捉的,所以是CWeiXinBtn:: OnMouseMove()
    其实,此处的CWeiXinBtn::OnMouseMove()原理,内含有这样的意思:当我们定义的新按钮类的某对象的OnMouseMove()函数得到调用,则其肯定已经得到鼠标焦点了;又因为鼠标的移动是连续的,所以当某按钮一得到鼠标焦点,只要鼠标再有轻微的移动,就调用OnMouseMove()函数,便实现了按钮逃跑;
    可以肯定的说:在鼠标移到某控件上的瞬间,控件获取焦点这个事实和鼠标移动这个消息是同时发生的。

BOOL CTestDlg::OnInitDialog()   //不是在CNewButton类中赋值
{
 ......
 // TODO: Add extra initialization here
 m_btn1.m_pBtn=&m_btn2;        //让各自按钮保存另一个的地址

 m_btn2.m_pBtn=&m_btn1;       

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

 

void CWeiXinBtn::OnMouseMove(UINT nFlags, CPoint point)
{
 // TODO: Add your message handler code here and/or call default
 ShowWindow(SW_HIDE);  //当前隐藏
 m_pBtn->ShowWindow(SW_SHOW);   //另一个显示
 CButton::OnMouseMove(nFlags, point);
}

------------------------------------------------------------------------------------------------

属性表单和向导的创建

    一个属性页窗口就是一个对话框窗口;添加属性页的一个页,就在Insert对话框中选Dialoge中的IDD_PROPPAGE_.....,需要几个属性页,就重复这样的操作几次即可。
注意区分:单选框、复选框、列表框、组合框   一个组合框提供了编辑框加列表框的功能
    注意:在放置组合框的同时,我们要将其适当拉大;这样当程序运行时,我们点击向下按钮,就无法看到其中的内容了。
    组合框有三种类型:1、DROPDOWN:其是可以输入内容的,当然也有列表框的功能;2、simple:其编辑框和列表框是同时显示的,不需点击向下按钮;3、drop list:其编辑框是不能输入的,只能通过列表框进行选择。
    此处我们为我们的三个属性页生成三个新的类,并选择基类为CPropertyPage
    在上面的生成新类的向导的最后,VC++经常会有这样的提示:不能打开某某.h文件和相应的.cpp文件;其实,新类我们已经创建完成,只是其没有在ClassView中没有显示他们而已;解决办法:关闭工程,在工程目录下,将.clw文件删除,再次打开工程,为工程新建一个同名的clw文件即可。
    为工程添加一个以CPropertySheet为基类的新类,通过CPropertySheet::AddPage()函数添加属性页,通过CPropertySheet::DoModal() 或者 CPropertySheet::Create()函数显示属性页;
    属性页、属性表单:1、属性表单是属性页的集合;2、属性页是从CDialog中派生而来的;3、属性表单是从CWnd中派生而来的。
在两个构造函数中都添加。
插入属性页资源遇到的一个问题:如果我们要插入的属性页对话框上有中文,则需在属性页资源的属性中选择语言为中文;当然要是我们插入的是一般对话框,则不会有此类问题。
二、生成向导对话框
    我们只需要在上面的工作的基础上,在调用CPropertySheet::DoModal()之前,调用CPropertySheet:: SetWizardMode() 函数即可。
(上面的工作并不十分完美,即第一个对话框有“上一步”按钮,最后一个对话框有“下一步”按钮,且没“完成”按钮)我们可以通过重新定义虚函数virtual BOOL CPropertyPage::OnSetActive(),并在其中使用void CPropertySheet::SetWizardButtons(DWORD dwFlags)来加以改变。
(要重新定义类的虚函数,可以在类上点击右键,选“Add Virtual Function....”来添加。)
虚函数virtual BOOL CPropertyPage::OnSetActive()是由框架调用的,当用户选择属性页或激活向导对话框时调用,它的调用是在CPropertySheet::DoModal()之前调用的。
    在属性页的类中,要得到属性表单的对象指针,我们只需调用GetParent()函数,并强制类型转换即可,如:((CPropertySheet*)GetParent())->SetWizardButtons(PSWIZB_NEXT); 。
    单选按钮的分组,一次分组的结束是遇到另一个也具有group属性的单选按钮;索引号0、1、2......
    向导对话框中的属性页中的“下一步”按钮的消息响应是通过,在该属性页中重新定义虚函数OnWizardNext()来完成的。
 OnWizardNext()函数的返回值有含义:为0,对话框中的属性页转到下一页;为-1,对话框中的属性页不改变。
 组合框中的内容:1、在程序代码中添加的,(一般在BOOL CPropertyPage::OnInitDialog()函数中添);   2、在其属性中设置;
    组合框中的属性选项sort若被选中,则组合框自动为我们添加的内容排序;若取消该选项,则其安我们程序代码添加的顺序来显示相应的内容。
   int CComboBox::SetCurSel( int nSelect )函数来设置组合框的默认选项,
   当与控件相关联的变量的值为-1,表明没有选中任何选项;当值为0,表明选中第一个;为1,表明选中第二个;.......
   通过int CComboBox::GetCurSel()函数,获取当前选项的索引值;而void CComboBox::GetLBText( int nIndex, CString& rString )函数,则可以通过已有的索引值来得到相应的字符串的值;
窗口和对象并不是同一个概念。
字体:1、定义字体对象;2、创建字体;3、选入字体,并保存先前的字体;
属性表单类的CPropertySheet::DoModal()函数的返回值或者为ID_WIZFINISH,或者为IDCANCEL;我们可以通过判断其返回值,来决定下一不该如何做。
    OnInitDialog():窗口完全被创建,但还没有被显示时调用。

 

 //创建属性表单 PropSheet.h

 #include "Prop1.h"
#include "Prop2.h"
#include "Prop3.h"
class CPropSheet : public CPropertySheet
{
public:
 CPropSheet(UINT nIDCaption, CWnd* pParentWnd = NULL, UINT iSelectPage = 0);  //两个构造函数
 CPropSheet(LPCTSTR pszCaption, CWnd* pParentWnd = NULL, UINT iSelectPage = 0);

public:
 CProp1 m_prop1;
 CProp2 m_prop2;
 CProp3 m_prop3;
 virtual ~CPropSheet();

 };

 -----------------------------

//PropSheet.cpp

PropSheet::CPropSheet(UINT nIDCaption, CWnd* pParentWnd, UINT iSelectPage)
 :CPropertySheet(nIDCaption, pParentWnd, iSelectPage)
{
 AddPage(&m_prop1);  //将属性页加入属性表单
 AddPage(&m_prop2);
 AddPage(&m_prop3);
}

CPropSheet::CPropSheet(LPCTSTR pszCaption, CWnd* pParentWnd, UINT iSelectPage)
 :CPropertySheet(pszCaption, pParentWnd, iSelectPage)
{
 AddPage(&m_prop1);
 AddPage(&m_prop2);
 AddPage(&m_prop3);

 ------------------------------------

//PropView.cpp

#include "PropSheet.h"

CPropView::CPropView()
  //在视类中接收选择的信息
 // TODO: add construction code here
 m_iOccupation=-1;
 m_strWorkAddr="";
 memset(m_bLike,0,sizeof(m_bLike));
 m_strSalary="";
}
void CPropView::OnPropertysheet()
{
 // TODO: Add your command handler code here
 CPropSheet propSheet("维新属性表单程序");  //属性页的名称,向导中不显示

 //propSheet.DoModal();   //属性页模式


 propSheet.SetWizardMode();  //向导模式
 if(ID_WIZFINISH==propSheet.DoModal())  //点击了完成
    //在视类中接收选择的信息
  m_iOccupation=propSheet.m_prop1.m_occupation;
  m_strWorkAddr=propSheet.m_prop1.m_workAddr;
  m_bLike[0]=propSheet.m_prop2.m_football;
  m_bLike[1]=propSheet.m_prop2.m_basketball;
  m_bLike[2]=propSheet.m_prop2.m_volleyball;
  m_bLike[3]=propSheet.m_prop2.m_swim;
  m_strSalary=propSheet.m_prop3.m_strSalary;
  Invalidate();  //使窗口无效,引起窗口重绘
 }
}

//输出选择的信息

 void CPropView::OnDraw(CDC* pDC)
{
 CPropDoc* pDoc = GetDocument();
 ASSERT_VALID(pDoc);
 // TODO: add draw code for native data here
 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);
}

------------------------------------------

 BOOL CProp1::OnSetActive()
{
 // TODO: Add your specialized code here and/or call the base class
 ((CPropertySheet*)GetParent())->SetWizardButtons(PSWIZB_NEXT);  //第一页只有下一步
 return CPropertyPage::OnSetActive();
}

CProp1::CProp1() : CPropertyPage(CProp1::IDD)
{
 //{{AFX_DATA_INIT(CProp1)
 m_occupation = -1;
 m_workAddr = _T("");  //字符串的初始化
 //}}AFX_DATA_INIT
}

LRESULT CProp1::OnWizardNext()
{
 // TODO: Add your specialized code here and/or call the base class
 UpdateData();
 if(m_occupation==-1)   //初值设置成-1、成员函数m_occupation,m_workAddr都要设为public
 {
  MessageBox("请选择你的职业!");
  return -1;
 }
 if(m_workAddr=="")
 {
  MessageBox("请选择你的工作地点!");
  return -1;
 }
 return CPropertyPage::OnWizardNext();
}

BOOL CProp1::OnInitDialog()
{
 CPropertyPage::OnInitDialog();             //给列表框中添加内容
 
 // TODO: Add extra initialization here
 ((CListBox*)GetDlgItem(IDC_LIST1))->AddString("北京");
 ((CListBox*)GetDlgItem(IDC_LIST1))->AddString("天津");
 ((CListBox*)GetDlgItem(IDC_LIST1))->AddString("上海");
 return TRUE;  // return TRUE unless you set the focus to a control
               // EXCEPTION: OCX Property Pages should return FALSE
}

---------------------------------- 

BOOL CProp2::OnSetActive()
{
 // TODO: Add your specialized code here and/or call the base class
 ((CPropertySheet*)GetParent())->SetWizardButtons(PSWIZB_BACK | PSWIZB_NEXT);
 return CPropertyPage::OnSetActive();
}

LRESULT CProp2::OnWizardNext()
{
 // TODO: Add your specialized code here and/or call the base class
 UpdateData();
 if(m_football || m_basketball || m_volleyball || m_swim)
 {
  return CPropertyPage::OnWizardNext();
 }
 else
 {
  MessageBox("请选择你的兴趣爱好!");
  return -1;
 }
}

---------------------------------------- 

 BOOL CProp3::OnSetActive()
{
 // TODO: Add your specialized code here and/or call the base class
 ((CPropertySheet*)GetParent())->SetWizardButtons(PSWIZB_BACK | PSWIZB_FINISH);
 return CPropertyPage::OnSetActive();
}

BOOL CProp3::OnInitDialog()
{
 CPropertyPage::OnInitDialog();
 
 // TODO: Add extra initialization here
 ((CComboBox*)GetDlgItem(IDC_COMBO2))->AddString("1000元以下");
 ((CComboBox*)GetDlgItem(IDC_COMBO2))->AddString("1000-2000元");
 ((CComboBox*)GetDlgItem(IDC_COMBO2))->AddString("2000-3000元");
 ((CComboBox*)GetDlgItem(IDC_COMBO2))->AddString("3000元以上");

 ((CComboBox*)GetDlgItem(IDC_COMBO2))->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_COMBO2))->GetCurSel();   //获取选择的索引值
 ((CComboBox*)GetDlgItem(IDC_COMBO2))->GetLBText(index,m_strSalary);  //获取选择的内容
 return CPropertyPage::OnWizardFinish();
}

0 0
原创粉丝点击