将行尾写入 wxTextCtrl

Writing end of line to wxTextCtrl

我试图将文本写入 wxWidgets 应用程序中的 wxTextctrl 对象。示例代码如下。出于某种原因,我无法换行。我在网上找到的大多数示例都做了其中一项在这里不起作用的事情。

代码:

#include "wx/wx.h"

using namespace std;

class MainWindow : public wxFrame {
 public:
  MainWindow(const wxString& title);
  wxTextCtrl* textctrl;
};

class MyApp : public wxApp {
 public:
  virtual bool OnInit();
  MainWindow* main_window;
};

MainWindow::MainWindow(const wxString& title)
    : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(280, 180)) {
  // Add text window:
  textctrl =
      new wxTextCtrl(this, -1, wxT(""), wxPoint(-1, -1), wxSize(250, 150));

  // This writes to the middle of the text white space, but does not understand
  // endl \n or \r\n
  ostream log_stream(textctrl);
  log_stream << "Hey Joe! \n";
  log_stream << "Buckle up!\r\n";
  log_stream << "Lorem ipsum dolor sit amet" << endl;

  wxStreamToTextRedirector redirect(textctrl);
  cout << "Not yet " << endl;
  cout << "One more \n";
  cout << "Just one more \r\n";

  cout << "Fine, I quit.";

  Centre();
}

此外,由于 window 大小,文本不会自动断开。

您需要创建具有 wxTE_MULTILINE 样式的 wxTextCtrl。然后,您通常只需要 \n 即可为每个平台正确解释。