sigslot.h的使用

来源:互联网 发布:dota2网络连接不上 编辑:程序博客网 时间:2024/05/01 02:40
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 constructorvirtual ~CBtnTest();sigslot::signal0<> Onbtndown;sigslot::signal0<> Onbtnup;// Dialog Dataenum { IDD = IDD_MFCTEST_DIALOG };protected:virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV supportDECLARE_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 defaultOnbtndown();CButton::OnLButtonDown(nFlags, point);}void CBtnTest::OnLButtonUp(UINT nFlags, CPoint point){// TODO: Add your message handler code here and/or call defaultOnbtnup();CButton::OnLButtonUp(nFlags, point);}


#include "BtnTest.h"// CmfctestDlg dialogclass CmfctestDlg : public CDialog,public sigslot::has_slots<>{// Constructionpublic:CmfctestDlg(CWnd* pParent = NULL);// standard constructorCImage m_img;CImage m_img2;// Dialog Dataenum { IDD = IDD_MFCTEST_DIALOG };protected:virtual void DoDataExchange(CDataExchange* pDX);// DDX/DDV support// Implementationprotected:HICON m_hIcon;// Generated message map functionsvirtual 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 0