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

Must I declare own wxFrame class?

我正在创建带有用户界面的简单 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 class 集中 window 的数据和事件处理程序,但它是无论如何都不需要。

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