如何在wxwidgets中添加包装文本面板

时间:2018-07-03 20:29:07

标签: wxwidgets word-wrap

这是对标准“最小”应用程序的修改,几乎可以满足我的要求。想法是在页面上放下一系列方框,每个方框都包含一大堆换行文本。这些文本框将与包含wxListView对象的框交替显示。

我可以看到两个潜在的错误来源:

  1. wxStaticText是正确使用的小部件吗?

  2. 是否存在我需要调整的参数?

这是代码,我对minimal.cpp的修改主要在“ RightPanel”和“ InnerPanel”类以及最后的“ extra”例程中。

// wxWidgets "Hello World" Program
// For compilers that support precompilation, includes "wx/wx.h".
#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();
private:
  void OnHello(wxCommandEvent& event);
  void OnExit(wxCommandEvent& event);
  void OnAbout(wxCommandEvent& event);
  void extra();
  };
enum { ID_Hello = 1 };

wxIMPLEMENT_APP(MyApp);

bool MyApp::OnInit() {
  MyFrame *frame = new MyFrame();
  frame->Show(true);
  return true;
  }

MyFrame::MyFrame() : wxFrame(NULL, wxID_ANY, "Hello World") {
  wxMenu *menuFile = new wxMenu;
  menuFile->Append(ID_Hello, "&Hello...\tCtrl-H",
                   "Help string shown in status bar for this menu item");
  menuFile->AppendSeparator();
  menuFile->Append(wxID_EXIT);
  wxMenu *menuHelp = new wxMenu;
  menuHelp->Append(wxID_ABOUT);
  wxMenuBar *menuBar = new wxMenuBar;
  menuBar->Append(menuFile, "&File");
  menuBar->Append(menuHelp, "&Help");
  SetMenuBar( menuBar );
  CreateStatusBar();
  SetStatusText("Welcome to wxWidgets!");
  Bind(wxEVT_MENU, &MyFrame::OnHello, this, ID_Hello);
  Bind(wxEVT_MENU, &MyFrame::OnAbout, this, wxID_ABOUT);
  Bind(wxEVT_MENU, &MyFrame::OnExit, this, wxID_EXIT);

  extra();
  }

void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event)) {
  Close(true);
  }
void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) {
  wxMessageBox("This is a wxWidgets Hello World example",
                 "About Hello World", wxOK | wxICON_INFORMATION);
  }
void MyFrame::OnHello(wxCommandEvent& WXUNUSED(event)) {
    wxLogMessage("Hello world from wxWidgets!");
  }

//--------------------------------------------
#include  "wx/splitter.h"
//--------------------------------------------
struct LeftPanel : public wxPanel  {
  LeftPanel(wxWindow* parent) : wxPanel(parent, wxID_ANY) {
    SetBackgroundColour(0xcbc0ff);
    }
  };

struct InnerPanel : public wxPanel  {
  InnerPanel(wxWindow* parent) : wxPanel(parent, wxID_ANY) {
    char *msg = "It is a truth universally acknowledged, that a long piece of "
                "text really should be wrapped"
                " ... but ... "
                "It is a truth universally acknowledged that great words will "
                "be misquoted";
    auto st = new wxStaticText( this, wxID_ANY, msg );
    st->SetBackgroundColour(0xf5f5f5);

    auto sizer = new wxBoxSizer(wxHORIZONTAL);
    sizer->AddSpacer(10);
    sizer->Add(st, wxSizerFlags(0).Expand().Border(wxALL, 5));
    sizer->AddSpacer(10);
    st->Wrap(-1);

    SetSizer(sizer);
    }
  };

struct RightPanel : public wxPanel  {
  RightPanel(wxWindow* parent) : wxPanel(parent, wxID_ANY) {
    SetBackgroundColour(0xeeccaa);
    wxFont fnt(wxFontInfo(12).FaceName("Arial"));
    SetFont(fnt);

    auto sizer = new wxBoxSizer(wxVERTICAL);
    for (int i=0; i<5; i++) {
      auto p = new InnerPanel(this);
      sizer->Add(p, wxSizerFlags(0).Expand());
      sizer->AddSpacer(10);
      }
    SetSizer(sizer);
    }
  };

void MyFrame::extra() {
  SetClientSize(wxSize(500, 600));

  auto vSplitter = new wxSplitterWindow(this, wxID_ANY);
  vSplitter->SetSashGravity(0.5);
  vSplitter->SetMinimumPaneSize(50);

  auto rPanel = new RightPanel(vSplitter);
  auto lPanel = new LeftPanel(vSplitter);
  vSplitter->SplitVertically(lPanel, rPanel, -300);

  wxBoxSizer* sizer = new wxBoxSizer(wxHORIZONTAL);
  sizer->Add(vSplitter, wxSizerFlags(1).Expand() );
  SetSizer(sizer);
  }

其中有一个wxSplitterWindow,因此我可以在更改外部面板的宽度时查看文本是否重新包装。

1 个答案:

答案 0 :(得分:1)

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> 不会自动换行,但是您可以使用wxStaticText方法手动换行。因此,如果您将呼叫添加到例如

Wrap()
在创建st->Wrap(GetTextExtent("It is a truth universall acknowledged, that").x); 后对您的代码

,它将在这种情况下自动换行。当然,您还可以定义一个st事件处理程序,以动态方式重新包装它。