我必须声明自己的 wxFrame 类吗?

Must I declare own wxFrame class?

本文关键字:wxFrame 类吗 自己的 声明      更新时间:2023-10-16

我正在创建带有用户界面的简单c ++应用程序。我已经编写了自己的类,其中包含所需的机制,现在我必须使用 wxWidgets 显示它的一些组件。MyApp包含该类的对象,但在处理事件时也需要MyFrame它们。如果不传递指向该对象的指针来MyFrame和实现MyApp::OnInit函数中的所有内容,它会舒服得多。这可能吗?

// should contain informations about game state, players, gameboard, etc
class MyApp : public wxApp {
GameBoard gb;
Player player;
bool lightTheme;
public:
virtual bool OnInit();
};
// should contain only window layout, button events, etc
class MyFrame : public wxFrame {
GameBoard *gb;
Player *player;
bool *lightTheme;
std::vector<wxStaticBitmap*> cardImages;
// some events here
void displayImages(wxGridSizer*);
public:
MyFrame(GameBoard*, Player*, bool*);
};

要回答标题问题,您不必声明MyFrame,只需创建并直接使用wxFrame并使用Bind()来连接不同的事件处理程序。在除最简单的情况之外的所有情况下,通常都方便使用MyFrame类来集中窗口的数据和事件处理程序,但无论如何都不需要这样做。

如果您决定将所有内容保留在MyApp中,您可能会发现 wxGetApp() 对于从代码中的不同位置访问它很有用。