sigslot.h的获取路径:http://share.weiyun.com/0c1b5303ff4c3ea5ba9ce3de4a333eb2
可用于两个类之间的相互调用
#include "stdafx.h"
#include "sigslot.h"
class AAA
{
public:
sigslot::signal1<string> Onput;
sigslot::signal2<int, int> OnPutint;
};
class BBB:public sigslot::has_slots<>
{
public:
void bputstr(string str);
void bputint(int a,int b);
};
void BBB::bputstr(string str)
{
cout<<str<<endl;
}
void BBB::bputint(int a,int b)
{
cout<<"a="<<a<<",b="<<b<<endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
AAA a;
BBB b;
a.Onput.connect(&b,&BBB::bputstr);
a.OnPutint.connect(&b,&BBB::bputint);
a.Onput("this is a test!");
a.OnPutint(1,9);
getchar();
return 0;
}
也可用于类和成员变量之间的相互调用
#include "stdafx.h"
#include "sigslot.h"
class AAA
{
public:
sigslot::signal1<string> Onput;
sigslot::signal2<int, int> OnPutint;
};
class BBB:public sigslot::has_slots<>
{
public:
BBB();
~BBB();
AAA a;
void bputstr(string str);
void bputint(int a,int b);
};
BBB::BBB()
{
a.Onput.connect(this,&BBB::bputstr);
a.OnPutint.connect(this,&BBB::bputint);
}
BBB::~BBB()
{
}
void BBB::bputstr(string str)
{
cout<<str<<endl;
}
void BBB::bputint(int a,int b)
{
cout<<"a="<<a<<",b="<<b<<endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
BBB b;
b.a.Onput("this is a test!");
b.a.OnPutint(1,9);
getchar();
return 0;
}
用于MFC中,控件变量消息传递,如对话框上有个按钮,按钮按下松开的消息需要发送给对话框,由对话框处理,使用sigslot就不用发送消息实现了。
class CBtnTest : public CButton
{
DECLARE_DYNAMIC(CBtnTest)
public:
CBtnTest(CWnd* pParent = NULL); // standard constructor
virtual ~CBtnTest();
sigslot::signal0<> Onbtndown;
sigslot::signal0<> Onbtnup;
// Dialog Data
enum { IDD = IDD_MFCTEST_DIALOG };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
};
<pre name="code" class="cpp">void CBtnTest::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
Onbtndown();
CButton::OnLButtonDown(nFlags, point);
}
void CBtnTest::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
Onbtnup();
CButton::OnLButtonUp(nFlags, point);
}
#include "BtnTest.h"
// CmfctestDlg dialog
class CmfctestDlg : public CDialog,public sigslot::has_slots<>
{
// Construction
public:
CmfctestDlg(CWnd* pParent = NULL); // standard constructor
CImage m_img;
CImage m_img2;
// Dialog Data
enum { IDD = IDD_MFCTEST_DIALOG };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected:
HICON m_hIcon;
// Generated message map functions
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnBnClickedBtnOpenimg();
void CreateStretchImage(CImage* pImage,CImage* DstImage,int height,int width);
afx_msg void OnBnClickedBtnSlot();
void OnBtnDown();
void OnBtnUp();
CBtnTest m_btnslot;
}
CmfctestDlg::CmfctestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CmfctestDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_btnslot.Onbtndown.connect(this,&CmfctestDlg::OnBtnDown);
m_btnslot.Onbtnup.connect(this,&CmfctestDlg::OnBtnUp);
}
最后
以上就是文艺可乐最近收集整理的关于sigslot.h的使用的全部内容,更多相关sigslot.h内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复