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++은 일을 매우 쉽고 간단하게 만들어 주는데, 저는 그냥 저항 할 수 없습니다 ...