C++仿C#实现事件处理

3 篇文章 0 订阅
测试
#include "beacon/beacon.hpp"
#include <cstdio>
#include <thread>

class mouseEvent : public beacon::args {
public:
    mouseEvent(int x, int y) : x(x), y(y) {}
    int x, y;
};

class object : public beacon::sender {
public:
  
};

class mouseHandler : public beacon::multi_handler {
public:

};

class Button {
public:
    void on_event(beacon::sender* sender, beacon::args* args) {
        auto event = args->to<mouseEvent>();
        printf("Mouse event at %d, %d\n", event.x, event.y);
    }
};




int main(int argc, char** argv) {
    mouseEvent event(10, 20);

    object sender;
    Button button;

    mouseHandler handler;
    handler.addListener<mouseEvent>(beacon::callback::bind(&Button::on_event, &button));
    handler.raise(&sender, &event);
    
    beacon::interval inter;
    inter.set(1000, [](const char* msg){ puts(msg); }, "Hello, World!");
    std::this_thread::sleep_for(std::chrono::milliseconds(10000));
    
    return 0;
}

g++ example.cpp -o test.exe

输出
Mouse event at 10, 20
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
编译错误

参考 

GitHub - ImanolFotia/beacon: Beacon is a multiparadigm C++ event management library with focus on fast and simple integration

C++ 仿QT信号槽二_c++模仿qt里面的信号与槽,做一个消息不同类消息传递-CSDN博客 

自定义窗口事件循环系统-CSDN博客 


创作不易,小小的支持一下吧!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码力码力我爱你

创作不易,小小的支持一下吧!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值