我在使用 wxWidgets 3.0.2 使 wxStaticText
标签动态包装在对话框中时遇到问题。我遵循了其他问题的想法,例如 this one ,位我还有奇效。
我在 wxEVT_SIZE
事件的回调中对文本使用了 Wrap(int)
函数,但它似乎对文本产生了意想不到的影响,而且似乎只会“缩小”尺寸,并且不会随着窗口的扩大而再次换行。
绑定(bind)的主要部分是:
CTOR(...) {
....
m_text->Bind(wxEVT_SIZE, &DIALOG_WRAPTEXT::onResize, this);
}
void CLASS::onResize( wxSizeEvent& event )
{
m_text->Wrap( event.GetSize().GetWidth() );
event.Skip();
}
第一次显示对话框时结果看起来不错,但是当你调整大小并返回时,你会得到这个结果:
一个最小可重现的例子是:
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
class DIALOG_WRAPTEXT: public wxDialog
{
public:
DIALOG_WRAPTEXT( wxWindow* parent,
const wxString& aTitle, const wxSize aSize );
private:
void onResize( wxSizeEvent& evt );
wxBoxSizer* m_itemBoxSizer;
wxStaticText* m_text;
};
DIALOG_WRAPTEXT::DIALOG_WRAPTEXT(
wxWindow* parent, const wxString& aTitle, const wxSize aSize ):
wxDialog( parent, wxID_ANY, aTitle,
wxPoint( -1, -1 ), aSize,
wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
{
m_itemBoxSizer = new wxBoxSizer( wxVERTICAL );
SetSizer( m_itemBoxSizer );
wxString msg("Lots and lots of text to wrap hopefully. "
"Lots and lots of text to wrap hopefully. "
"Lots and lots of text to wrap hopefully. "
"Lots and lots of text to wrap hopefully. "
"Lots and lots of text to wrap hopefully. "
"Lots and lots of text to wrap hopefully. "
);
m_text = new wxStaticText( this, wxID_ANY, msg );
// wxEXPAND makes no difference
m_itemBoxSizer->Add( m_text, 1, wxALIGN_TOP | wxALL | wxEXPAND, 5 );
// Bind to m_text or this, same effect
m_text->Bind(wxEVT_SIZE, &DIALOG_WRAPTEXT::onResize, this);
}
void DIALOG_WRAPTEXT::onResize( wxSizeEvent& event )
{
//m_text->Freeze(); // makes no difference
const auto w = event.GetSize().GetWidth();
wxLogDebug( "Wrap to width: %d",w ); // produces sensible values
m_text->Wrap( w );
//m_text->Thaw();
event.Skip();
}
class MyApp: public wxApp
{
public:
bool OnInit() override
{
auto d = new DIALOG_WRAPTEXT(NULL, "Dialog title", wxSize(200, 200));
d->ShowModal();
d->Destroy();
}
};
wxIMPLEMENT_APP(MyApp);
在对话框中动态包装静态文本的正确方法是什么?
最佳答案
在没有任何 wrap() 的情况下,wxStaticText 在 wx 3.0.2 的窗口上使用以下最小代码正确显示文本(在字边界处换行)。我可以调整对话框的大小(缩小、增大)并且 wxStaticText 会正确地自行更新。这对您的用例来说还不够吗?您确定需要使用 wrap 功能吗?
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
class DIALOG_WRAPTEXT : public wxDialog
{
public:
DIALOG_WRAPTEXT(wxWindow* parent,
const wxString& aTitle, const wxSize aSize);
private:
void onResize(wxSizeEvent& evt);
wxBoxSizer* m_itemBoxSizer;
wxStaticText* m_text;
};
DIALOG_WRAPTEXT::DIALOG_WRAPTEXT(
wxWindow* parent, const wxString& aTitle, const wxSize aSize) :
wxDialog(parent, wxID_ANY, aTitle,
wxPoint(-1, -1), aSize,
wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
{
m_itemBoxSizer = new wxBoxSizer(wxVERTICAL);
SetSizer(m_itemBoxSizer);
wxString msg("Lots and lots of text to wrap hopefully. "
"Lots and lots of text to wrap hopefully. "
"Lots and lots of text to wrap hopefully. "
"Lots and lots of text to wrap hopefully. "
"Lots and lots of text to wrap hopefully. "
"Lots and lots of text to wrap hopefully. "
);
m_text = new wxStaticText(this, wxID_ANY, msg);
// wxEXPAND makes no difference
m_itemBoxSizer->Add(m_text, 1, wxALIGN_TOP | wxALL | wxEXPAND, 5);
// Act on dialog resize
Bind(wxEVT_SIZE, &DIALOG_WRAPTEXT::onResize, this);
}
void DIALOG_WRAPTEXT::onResize(wxSizeEvent& event)
{
// layout everything in the dialog
Layout();
event.Skip();
}
class MyApp : public wxApp
{
public:
bool OnInit() override
{
auto d = new DIALOG_WRAPTEXT(NULL, "Dialog title", wxSize(200, 200));
d->ShowModal();
d->Destroy();
return true;
}
};
wxIMPLEMENT_APP(MyApp);
关于c++ - wxWidgets C++ - 在对话框中动态包装 wxStaticText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41710869/