wxWidgets C ++ - 在对话框中动态包装wxStaticText

时间:2017-01-18 03:26:03

标签: c++ dialog wxwidgets textwrapping

我无法使用wxWidgets 3.0.2在对话框中动态包装wxStaticText标签。我跟着其他问题的想法,如this one,我仍然有奇怪的效果。

我在文本上使用Wrap(int)函数,在wxEVT_SIZE事件的回调中,但它似乎对文本有意想不到的影响,而且似乎只是“棘轮”大小,并且在窗口扩展时不会再次换行。

绑定的主要部分是:

CTOR(...) {
    ....
    m_text->Bind(wxEVT_SIZE, &DIALOG_WRAPTEXT::onResize, this);
}

void CLASS::onResize( wxSizeEvent& event )
{
    m_text->Wrap( event.GetSize().GetWidth() );
    event.Skip();
}

首次显示对话框时,结果看起来没问题,但是当您调整较小并进行备份时,会得到以下结果:

After making narrower After making wide again

最低可重复的例子是:

#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);

在对话框中动态包装静态文本的正确方法是什么?

2 个答案:

答案 0 :(得分:1)

没有任何wrap(),wxStaticText使用wx 3.0.2在Windows上的下面的最小代码正确显示文本(包装在单词边界)。我可以调整对话框的大小(缩小,增长),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);

答案 1 :(得分:0)

遇到类似的问题。我必须在某处存储未包装的消息,当调整wxStaticText的大小时,设置消息并调用wrap。否则该线可能不会很好地包裹。

void MyFrame::onResize(wxSizeEvent& evt)
{
const auto w = evt.GetSize().GetWidth();
m_text->SetLabel(m_msg); // unwrapped message
m_text->Wrap(w);
evt.Skip();
}