sigslot.h的使用



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);
}




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值