问题
My wxWidgets program should work on both desktop PC and Windows tablet. Both use Windows 10 Pro 64-bit version. How can I make it, that a text with a changing size is inside a dialog no matter where it is displayed?
This is what I do now:
TrackDialog::TrackDialog(wxWindow* parent,wxWindowID id,const wxPoint& pos,
const wxSize& size)
{
//(*Initialize(TrackDialog)
wxBoxSizer* BoxSizer4;
wxBoxSizer* BoxSizer5;
wxBoxSizer* BoxSizer2;
wxBoxSizer* BoxSizer1;
wxBoxSizer* BoxSizer3;
wxBoxSizer* pPanelSizer;
Create(parent, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxSTAY_ON_TOP|wxSUNKEN_BORDER, _T("wxID_ANY"));
Panel1 = new wxPanel(this, ID_PANEL1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL, _T("ID_PANEL1"));
pPanelSizer = new wxBoxSizer(wxVERTICAL);
BoxSizer1 = new wxBoxSizer(wxHORIZONTAL);
StaticSelectText = new wxStaticText(Panel1, ID_STATICTEXT1, _("Select a *.xml file."), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT1"));
BoxSizer1->Add(StaticSelectText, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
pPanelSizer->Add(BoxSizer1, 0, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
BoxSizer2 = new wxBoxSizer(wxHORIZONTAL);
XmlFilePickerCtrl = new wxFilePickerCtrl(Panel1, ID_FILEPICKERCTRL1, _T("./resources/"), wxEmptyString, _T("*.xml"), wxDefaultPosition, wxDefaultSize, wxFLP_FILE_MUST_EXIST|wxFLP_OPEN|wxFLP_USE_TEXTCTRL, wxDefaultValidator, _T("ID_FILEPICKERCTRL1"));
BoxSizer2->Add(XmlFilePickerCtrl, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
pPanelSizer->Add(BoxSizer2, 0, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
BoxSizer3 = new wxBoxSizer(wxHORIZONTAL);
StaticNumberText = new wxStaticText(Panel1, ID_STATICTEXT2, _("Tracknumber \n"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT2"));
StaticNumberText->Disable();
wxFont StaticNumberTextFont(10,wxSWISS,wxFONTSTYLE_NORMAL,wxNORMAL,false,_T("Arial"),wxFONTENCODING_DEFAULT);
StaticNumberText->SetFont(StaticNumberTextFont);
BoxSizer3->Add(StaticNumberText, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
pPanelSizer->Add(BoxSizer3, 0, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
BoxSizer4 = new wxBoxSizer(wxHORIZONTAL);
ChoiceTrack = new wxChoice(Panel1, ID_CHOICE1, wxDefaultPosition, wxDefaultSize, 0, 0, 0, wxDefaultValidator, _T("ID_CHOICE1"));
ChoiceTrack->Disable();
BoxSizer4->Add(ChoiceTrack, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
pPanelSizer->Add(BoxSizer4, 0, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
BoxSizer5 = new wxBoxSizer(wxHORIZONTAL);
ButtonOk = new wxButton(Panel1, ID_BUTTON1, _("OK"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON1"));
BoxSizer5->Add(ButtonOk, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
pPanelSizer->Add(BoxSizer5, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
Panel1->SetSizer(pPanelSizer);
pPanelSizer->Fit(Panel1);
pPanelSizer->SetSizeHints(Panel1);
Center();
Connect(ID_FILEPICKERCTRL1,wxEVT_COMMAND_FILEPICKER_CHANGED,(wxObjectEventFunction)&TrackDialog::OnXmlFilePickerCtrlFileChanged);
Connect(ID_CHOICE1,wxEVT_COMMAND_CHOICE_SELECTED,(wxObjectEventFunction)&TrackDialog::OnChoiceTrackSelect1);
Connect(ID_BUTTON1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&TrackDialog::OnButtonOkClick);
//*)
SetSize(DoGetBestClientSize());
Fit();
}
The text is changed from "Track number" to one of the options below:
void TrackDialog::setOutputAndChoice()
{
std::vector<wxString> storedTracks = m_xmlReader.getNamesOfTracks(m_path);
int numberOfTracks = storedTracks.size();
StaticNumberText->Enable();
wxString text;
// Make sure there are tracks stored in the file.
if(numberOfTracks > 0){
text = "There are ";
text += wxString::Format(wxT("%i"), numberOfTracks);
text += " tracks stored in the selected file. Select one."
"\nTrack 1 is the inner track.";
}
else{
text = "Opening file failed. Please chose another"
"file of the right format.";
}
StaticNumberText->SetLabel(text);
}
And this is what it looks like on desktop PC (on the tablet it looks the same, thanks to SetSize(DoGetBestClientSize());
)
What can I do so that all of the text is displayed? I already tried to set it in the constructor before I call SetSizerAndFit()
but then my program crashes if I want to close it...
Thank you for your help.
Edit: I put a panel in the sizer for the StaticNumberText
and the text in it and I am able to display all of the text now, though now there is some nasty space underneath the StaticText:
There has to be a better way to properly layout dialogs with changing text...
回答1:
Sizers need an action to get into game. User changing the size of the main (or dialog) window is definitely an action.
You can also trigger the action asking the dialog window to fit into its children. Call Fit()
at the end of your TrackDialog::setOutputAndChoice()
回答2:
Maybe the sizer for the StaticText there be inside the sizer for both TextCtrl and Button. Then the sizer for the StaticText will take the width of the TextCtrl. Better, put the sizer for the StaticText, below and independently of the previous sizer.
The proportion of the StaticText, and the Panel in where is inside, must be set at 0.
来源:https://stackoverflow.com/questions/41096324/size-wxdialogs-so-all-of-the-text-is-inside-the-dialog