2012-01-04 26 views
1

我對wxWidgets和C++很陌生,但我熟悉其他工具包。我想這樣的佈局:C++ wxWidgets - ScrolledWindow沒有寬度的問題

My design

,這是什麼樣子。看起來好像我的窗口沒有寬度都:

enter image description here

所以這是我的代碼,嚴重註解。

這裏是我的代碼,我相信它儘可能接近我的意圖。 的設計是這樣的,每一個「發件人」,「在發送」和「消息」是基於一個可重用的面板上的獨特塊:

//CONTENTS OF GUI_MESSAGE_ITEM.H 

#ifndef GUIMESSAGEITEM_H 
#define GUIMESSAGEITEM_H 

#include "wx/panel.h" // Base class: wxPanel 
#include "wx/stattext.h" 
#include "sms_message.h" 
#include "wx/window.h" 
#include "wx/wx.h" 

class GUIMessageItem : public wxPanel { 

public: 
    GUIMessageItem(wxWindow* parent, wxWindowID winid, const SMSMessage& smsMessage); 
    ~GUIMessageItem(); 

private: 
    wxStaticText* stSender; 
    wxStaticText* stSentTime; 
    wxStaticText* stMessageContents; 
}; 

#endif // GUIMESSAGEITEM_H 

//CONTENTS OF GUI_MESSAGE_ITEM.CPP 

#include "gui_message_item.h" 

GUIMessageItem::GUIMessageItem(wxWindow* parent, wxWindowID winid, const SMSMessage& smsMessage) : 
    wxPanel(parent, winid), 
    stSender(new wxStaticText(this, winid, smsMessage.GetSender())), 
    stSentTime(new wxStaticText(this, winid, smsMessage.GetSentTime())), 
    stMessageContents(new wxStaticText(this, winid, smsMessage.GetMessage())) 
{ 
    wxColour blue(wxT("#2A2AF7")); 
    wxColour green(wxT("#56DB4F")); 
    wxFont originalFont = stSender->GetFont(); 
    wxFont boldFont(originalFont); 
    boldFont.SetWeight(wxFONTWEIGHT_BOLD); 
    wxSize stsMin(100, 60); 
    wxSize bodyMin(300, 100); 

    stSender->SetForegroundColour(blue); 
    stSentTime->SetForegroundColour(green); 
    stSender->SetFont(boldFont); 
    stSentTime->SetFont(boldFont); 
    stSender->SetMinSize(stsMin); 
    stSentTime->SetMinSize(stsMin); 

    stMessageContents->SetMinSize(bodyMin); 
    stMessageContents->Wrap(200); 

    wxBoxSizer* lines = new wxBoxSizer(wxVERTICAL); 
    wxBoxSizer* topLine = new wxBoxSizer(wxHORIZONTAL); 
    lines->AddSpacer(4); 
    topLine->AddSpacer(5); 
    this->SetSizer(lines); 

    topLine->Add(stSender, wxALIGN_LEFT); 
    topLine->Add(stSentTime, wxALIGN_RIGHT); 
    lines->Add(topLine); 
    lines->Add(stMessageContents, wxALIGN_CENTER_HORIZONTAL); 

    lines->SetMinSize(wxSize(400,400)); 
    this->FitInside(); 
    this->Layout(); 
} 

GUIMessageItem::~GUIMessageItem() 
{ 
} 

//MAIN CODE FOR THE WHOLE FORM 

MainFrameBase::MainFrameBase(wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, long style) : wxFrame(parent, id, title, pos, size, style) 
{ 
    this->SetSizeHints(wxDefaultSize, wxDefaultSize); 

    //Menu Bar stuff. 
    m_menuBar = new wxMenuBar(0); 
    m_menuFile = new wxMenu(); 
    wxMenuItem* menuFileExit; 
    menuFileExit = new wxMenuItem(m_menuFile, wxID_EXIT, wxString(_("E&xit")) + wxT('\t') + wxT("Alt+X"), wxEmptyString, wxITEM_NORMAL); 

    wxMenuItem* menuFileOpen; 
    menuFileOpen = new wxMenuItem(m_menuFile, wxID_OPEN, wxString(_("&Open")) + wxT('\t') + wxT("Alt+O"), wxEmptyString, wxITEM_NORMAL); 

    m_menuFile->Append(menuFileOpen); 
    m_menuFile->Append(menuFileExit); 
    m_menuBar->Append(m_menuFile, _("&File")); 

    this->SetMenuBar(m_menuBar); 

    //main sizer for whole interface 
    wxBoxSizer* mainSizer = new wxBoxSizer(wxVERTICAL); 

    this->SetSizer(mainSizer); 

    // Filter box section 
    wxStaticText* filterLabel = new wxStaticText(this, wxID_ANY, wxT("Filter by Sender:")); 

    m_filter = new wxComboBox(
     this, 
     wxID_ANY, 
     wxT(""), 
     wxDefaultPosition, 
     wxDefaultSize, 
     0, 
     NULL, 
     wxCB_DROPDOWN|wxCB_READONLY 
    ); 

    wxBoxSizer* filterSizer = new wxBoxSizer(wxHORIZONTAL); 
    filterSizer->Add(filterLabel); 
    filterSizer->Add(m_filter); 
    mainSizer->Add(filterSizer); 

    // List of Messages section //The issue must be here somewhere... 
    m_scrWin = new wxScrolledWindow(
     this, 
     wxID_ANY 
    ); 

    m_listSizer = new wxBoxSizer(wxVERTICAL); 
    m_scrWin->SetSizer(m_listSizer); 
    mainSizer->Add(m_scrWin, wxEXPAND); //m_scrWin should take the WHOLE of the interface. 

    //example msg 
    SMSMessage* exampleMessage = new SMSMessage(
     wxT("+44 07950 322 789"), 
     wxT("2011-13-07 13:22"), 
     wxT("Yo mate, what's up?") 
    ); 

    for (int i = 0; i < 6; i++) { 
     AddSMSMessagePanel(*exampleMessage); 
    } 

    //wxSize minimum(300,500); 

    m_scrWin->FitInside();    //Use fit inside to make the scrollwindow use the width of the items inside? without doing this I get no scrollbar at all... 
    //m_scrWin->SetMinSize(minimum); 
    //m_listSizer->SetMinSize(minimum); 
    //m_scrWin->EnableScrolling(true, true); 
    //m_scrWin->SetScrollRate(1,1); 
    m_scrWin->SetScrollRate(5, 5); 

    this->Layout(); 
    m_statusBar = this->CreateStatusBar(1, wxST_SIZEGRIP, wxID_ANY); 

    this->Centre(wxBOTH); 

    // Connect Events 
    this->Connect(wxEVT_CLOSE_WINDOW, wxCloseEventHandler(MainFrameBase::OnCloseFrame)); 
    this->Connect(menuFileOpen->GetId(), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrameBase::OnFileOpen)); 
    this->Connect(menuFileExit->GetId(), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrameBase::OnExitClick)); 
} 

void MainFrameBase::AddSMSMessagePanel(const SMSMessage& message) { 
    GUIMessageItem* gmi = new GUIMessageItem(m_scrWin, wxID_ANY, message); //object inherits from wxPanel 
    m_listSizer->Add(gmi); 
} 

我很抱歉在這裏帶來這樣那樣的具體問題,但我是C++和wxWidgets的新手,我花了大約5個小時試圖解決這個問題,我不知道我缺少什麼知識。

這是一個完整的源代碼的鏈接:https://github.com/PhillipTaylor/SMSReader

+0

請問我還能做些什麼來鼓勵對這個問題的回答嗎? – Philluminati 2012-01-04 14:15:55

+0

兩個建議:1.給它多一點時間,回答這些問題對大多數人來說並不是高優先級。 2.嘗試用較少的代碼重現此問題。 – ravenspoint 2012-01-04 14:47:08

回答

1

這是很多代碼看,我不假裝明白是怎麼回事。

您正在編寫和調試大量可以全部使用wxGrid替換的代碼,據我所見。使用wxGrid會使事情變得簡單得多,所有棘手的問題都已經過測試和調試。

+0

謝謝。有一個缺失的功能,我沒有包括它(現在顯示在上面)。我還添加了對m_listSizer-> Layout()的調用; (沒有在上面顯示),這沒有任何區別。 – Philluminati 2012-01-04 14:29:41

0

這是由DialogBlocks生成的一段代碼。應按照您所描述的方式創建佈局。

void test::CreateControls() 
{  
////@begin test content construction 
    test* itemPanel1 = this; 

    wxBoxSizer* itemBoxSizer2 = new wxBoxSizer(wxVERTICAL); 
    itemPanel1->SetSizer(itemBoxSizer2); 

    wxBoxSizer* itemBoxSizer3 = new wxBoxSizer(wxHORIZONTAL); 
    itemBoxSizer2->Add(itemBoxSizer3, 0, wxALIGN_LEFT|wxLEFT|wxRIGHT|wxTOP, 5); 

    wxStaticText* itemStaticText4 = new wxStaticText(itemPanel1, wxID_STATIC, _("Static text"), wxDefaultPosition, wxDefaultSize, 0); 
    itemBoxSizer3->Add(itemStaticText4, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5); 

    wxArrayString itemChoice5Strings; 
    wxChoice* itemChoice5 = new wxChoice(itemPanel1, ID_CHOICE, wxDefaultPosition, wxDefaultSize, itemChoice5Strings, 0); 
    itemBoxSizer3->Add(itemChoice5, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5); 

    wxArrayString itemSimpleHtmlListBox6Strings; 
    wxSimpleHtmlListBox* itemSimpleHtmlListBox6 = new wxSimpleHtmlListBox(itemPanel1, ID_SIMPLEHTMLLISTBOX, wxDefaultPosition, wxSize(400, 300), itemSimpleHtmlListBox6Strings, wxHLB_DEFAULT_STYLE); 
    itemBoxSizer2->Add(itemSimpleHtmlListBox6, 1, wxGROW|wxLEFT|wxRIGHT|wxBOTTOM, 5); 

////@end test content construction 
}