我试图将文本写入wxTextctrl
应用程序中的wxWidgets
对象。示例代码如下。由于某种原因,我无法获得换行符。我在网上发现的大多数示例都执行了其中一项操作,而这在这里不起作用。
代码:
#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();
}
此外,由于窗口大小,文本不会自动中断。
您需要以wxTextCtrl
样式创建wxTE_MULTILINE
。然后,您通常只需要\n
,即可为每个平台正确解释。