wx位图按钮读取类参数
wxBitmapButton reading class parameters
我已经尝试解决这个问题几个小时了,但我的wxWidgets知识(我是初学者)和在线寻找答案都没有帮助我解决这个问题。
我创建了一个名为 Field 的类,其中包含三个参数 int x、int y 和 wxBitmapButton 按钮。现在,我要做的是,当单击该按钮时,连接到该按钮的事件处理程序将从包含所用按钮的同一类实例中读取 x 和 y。
本质上,我想要实现的是通过单击字段::按钮来读取给定的坐标字段::x,字段::y。有人可以帮助我完成这项任务吗?
我假设Field
本身不是一个小部件(如果是这样,事情是相似的,你只需要改变它的创建方式)。一种写法是:
struct Field
{
Field(int x_, int y_) : x(x_), y(y_) { }
void set_button(wxBitmapButton* btn)
{
button = btn;
button->Bind(wxEVT_BUTTON, [this](wxCommandEvent&)
{
//Do whatever you want with x and y
//(they're accessed through the captured this pointer).
//For example:
wxMessageBox(std::to_wstring(x) + ", " + std::to_wstring(y));
});
}
int x;
int y;
wxBitmapButton* button = nullptr;
};
要测试它,您可以创建一个简单的窗口,如下所示:
struct test_frame : wxFrame
{
test_frame() : wxFrame(nullptr, wxID_ANY, L"Test"), fld(3, 7) { }
//fld doesn't have to be a member of the wxFrame-derived class;
//it just needs to live at least as long as the button it references.
//This is just an example that satisfies that condition.
Field fld;
};
并像这样初始化所有内容:
auto main_frame = new test_frame();
auto btn = new wxBitmapButton(main_frame, wxID_ANY, your_bitmap);
main_frame->fld.set_button(btn);
main_frame->Show();
单击按钮时,您将弹出一个消息框,显示3, 7
(当然x
和y
中的任何值)。
所有这些代码都假设你有一个相当最新的编译器 - 如您所见,它使用了相当多的 C++11 功能。当然,这一切都可以通过许多其他方式完成,但现代C++使事情变得如此美好和简单,我就是无法抗拒......
相关文章:
- 如何使用Luacneneneba API正确读取字符串和表参数
- 使用 istream 参数读取的 istream 函数
- 如果需要转换,我可以在读取参数的同时将其移动到另一个参数吗?
- 读取大文件(>2GB)(文本文件包含以太网数据)并通过不同参数随机访问数据的最佳方法是什么?
- ReadFile() 读取的字节数少于其参数中的指定值
- 在 Windows 中使用 boost::p rogram_options 从命令行参数读取 Unicode 字符
- (C/C++)fscanf_s从txt文件以字符形式读取数组时缺少整数参数错误
- 读取用户必须提供一些参数的文件
- C++添加检查以避免读取两个参数
- Unix 读取缓冲区参数
- 将指针作为参数分配给类变量,然后使用它抛出读取访问违规
- 我如何在燃料中读取指令,然后使用来自Infile的参数将函数形状写入外档
- 命令行参数读取文本文件
- opencl openGL互操作内核参数用于读取 写入
- 从文件中读取的二维数组作为参数
- 提升program_options:在 3D 矢量中读取作为命令行参数
- 我如何读取可变数量的参数
- 如何实现将用户输入读取到提供的所有变量中的可变参数模板
- 尝试从命令行参数读取字符串"$$"
- 将.dat文件中的参数读取到C++中