简体   繁体   English

替换 wxWidgets 中 wxFrame 的 sizer

[英]Replace sizer of a wxFrame in wxWidgets

I am writing a small Minesweeper game just to get familiar with wxWidgets (Windows, wxWidgets 3.1.4).我正在编写一个小型扫雷游戏,只是为了熟悉 wxWidgets(Windows,wxWidgets 3.1.4)。 The app can handle one game well, and now I would like to add the "new game" functionality.该应用程序可以很好地处理一个游戏,现在我想添加“新游戏”功能。 For the layout, I am using a wxGridSizer .对于布局,我使用的是wxGridSizer

My first approach was to create a new wxGridSizer with the new fields in it, and just replace the current sizer with the new one.我的第一种方法是创建一个包含新字段的新wxGridSizer ,然后将当前的 sizer 替换为新的。 Even though I figured out how to reuse the old sizer (and it is probably also a better solution in general), I am kind of curious how can I replace the sizer properly.即使我想出了如何重用旧的sizer(一般来说它可能也是一个更好的解决方案),我还是很好奇如何正确更换sizer。

I was able to simplify my problem to this:我能够将我的问题简化为:


#include <wx/wxprec.h>
#ifndef WX_PRECOMP
  #include <wx/wx.h>
#endif

class MyApp : public wxApp {
public:
  virtual bool OnInit();
};
class MyFrame : public wxFrame {
public:
  MyFrame(const wxString &title, const wxPoint &pos, const wxSize &size);

private:
  void OnExit(wxCommandEvent &event);
  void OnRefill(wxCommandEvent &event);
  wxDECLARE_EVENT_TABLE();
};

enum { ID_Refill = 1 };

wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(ID_Refill, MyFrame::OnRefill)
EVT_MENU(wxID_EXIT, MyFrame::OnExit)
wxEND_EVENT_TABLE() wxIMPLEMENT_APP(MyApp);

bool MyApp::OnInit() {
  MyFrame *frame = new MyFrame("Hello World", wxPoint(50, 50), wxSize(300, 200));
  frame->Show(true);
  return true;
}

MyFrame::MyFrame(const wxString &title, const wxPoint &pos, const wxSize &size)
  : wxFrame(NULL, wxID_ANY, title, pos, size) {
  wxMenu *menu = new wxMenu;
  menu->Append(ID_Refill, "&Refill...\tCtrl-R", "Creates new layout");
  wxMenuBar *menuBar = new wxMenuBar;
  menuBar->Append(menu, "&Menu");
  SetMenuBar(menuBar);
  wxGridSizer *sizer = new wxGridSizer(2, 2, 1, 1);
  SetSizer(sizer);

  for (auto i = 0; i < 4; i++) {
    wxButton *button = new wxButton(this, wxID_ANY, std::to_string(i), wxDefaultPosition, wxDefaultSize, 0);
    sizer->Add(button, wxALL, 0);
  }
}

void MyFrame::OnExit(wxCommandEvent &) {
  Close(true);
}

void MyFrame::OnRefill(wxCommandEvent &) {
  Freeze();
  GetSizer()->Clear(true);
  wxGridSizer *sizer = new wxGridSizer(3, 3, 1, 1);
  SetSizer(sizer);
  for (auto i = 0; i < 9; i++) {
    wxButton *button = new wxButton(this, wxID_ANY, std::string("Refilled") + std::to_string(i), wxDefaultPosition,
                                    wxDefaultSize, 0);
    sizer->Add(button, wxALL, 0);
  }
  sizer->Layout();
  Thaw();
  Refresh();
}

The problem is after the OnRefill function the app only shows the first button ( Refilled0 ), but not the rest of the buttons.问题是在OnRefill function 之后,应用程序只显示第一个按钮( Refilled0 ),而不是按钮的 rest 。

Before OnRefill :OnRefill之前:

显示所有按钮

After OnRefill : OnRefill之后:

只显示第一个按钮

My question is how can I replace the sizer of MyFrame properly?我的问题是如何正确更换MyFrame的 sizer? Based on what I understood from the examples and the documentation, this should work, but I guess I am missing something.根据我从示例和文档中了解到的内容,这应该可行,但我想我遗漏了一些东西。

To replace the sizer, you just need to make one small change to the MyFrame::OnRefill method.要替换 sizer,您只需对MyFrame::OnRefill方法进行一点更改。

Instead of calling sizer->Layout();而不是调用sizer->Layout(); simply call Layout();只需调用Layout(); . . I'm not entirely sure why calling Layout for the sizer doesn't work.我不完全确定为什么为 sizer 调用 Layout 不起作用。

The full method looks like this:完整的方法如下所示:

void MyFrame::OnRefill(wxCommandEvent &) {
  Freeze();
  GetSizer()->Clear(true);
  wxGridSizer *sizer = new wxGridSizer(3, 3, 1, 1);
  SetSizer(sizer);
  for (auto i = 0; i < 9; i++) {
    wxButton *button = new wxButton(this, wxID_ANY, std::string("Refilled") + std::to_string(i), wxDefaultPosition,
                                    wxDefaultSize, 0);
    sizer->Add(button, wxALL, 0);
  }
  Layout();
  Thaw();
  Refresh();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM