Guidance
指路人
g.yi.org
Guidance Forums / wxWidgets (wxWindows) in C++ / Trying to center labels.

Register 
新用户注册
Search 搜索
首页 
Home Home
Software
Upload

  
Forum List • Thread List • Reply • Refresh • New Topic • Search • Previous • Next First 1 Last
Message1. Trying to center labels.
#4747
Posted by: 2004-07-07 19:57:20
Hi. I'm trying to do a label centered for the top of my frame. I've tryied several methods, but non of them center the Text on the panel. Could you help me?

 //Creacion y adiccion de la etiqueta superior.
 
  wxPanel* prenault = new wxPanel(this, -1, wxPoint(0,0), wxDefaultSize,
     0);
  prenault->SetBackgroundColour(wxColour(230,230,0));
 
  //renault->SetWindowStyle(wxALIGN_CENTRE);
  //renault->Center();

  wxLayoutConstraints *rc = new wxLayoutConstraints;
  rc->centreX.SameAs    (this, wxCentreX);   
  rc->top.SameAs        (this, wxTop);
  rc->width.SameAs      (this, wxWidth);
  rc->height.Absolute     (20);
 
  prenault->SetConstraints(rc);  

  wxStaticText* renault = new wxStaticText(prenault, -1, "Patrocinado por Renault F1");

  wxLayoutConstraints *rl = new wxLayoutConstraints;
  rl->centreX.SameAs    (prenault, wxCentreX);   
  //rl->top.SameAs        (this, wxTop);
  //rl->width.SameAs      (this, wxWidth);
  rl->height.SameAs     (prenault, wxHeight);
 
  renault->SetConstraints(rl);  
  // wxPoint(20,-1), wxDefaultSize, wxALIGN_CENTRE, "Patrocinado por Renault F1");
  //wxStaticText* renault = new wxStaticText(prenault, -1, "Patrocinado por Renault F1",
  //  wxPoint(this->GetSize().GetWidth()/2,0), wxDefaultSize, wxALIGN_RIGHT); 

  //renault->SetPosition(0,0);
  //renault->CenterOnParent(1);
  //renault->Refresh();
Message2. Re: Trying to center labels.
#4753
Posted by: upCASE 2004-07-08 01:38:28
Hi!
Use wxSizer instead. wxLayoutConstraint is considered deprecated.
upCASE
-----------------------------------
If it was hard to write, it should be hard to read!- Do. Or do not. There is no try!
Message3. Re: Trying to center labels.
#4777
Posted by: 2004-07-10 02:39:52
I've used sizers instead, but nothing changes. Could someone give me an example in which a label(Panel with StaticText inside) is centered, and when you resize the window, the resized panel contains the StaticText newly centered?
Message4. Re: Trying to center labels.
#4784
Posted by: upCASE 2004-07-10 22:59:47
Hi!
Everything changes with sizers :)
#include <wx/wx.h>
#include <wx/image.h>

#ifndef MYFRAME_H
#define MYFRAME_H

class MyFrame: public wxFrame {
    wxStaticText* label;
    wxPanel* panel;
public:
    MyFrame(wxWindow* parent, int id, const wxString& title, const wxPoint& pos=wxDefaultPosition, const wxSize& size=wxDefaultSize, long style=wxDEFAULT_FRAME_STYLE);
};
#endif // MYFRAME_H

#include "MyFrame.h"
MyFrame::MyFrame(wxWindow* parent, int id, const wxString& title, const wxPoint& pos, const wxSize& size, long style):
    wxFrame(parent, id, title, pos, size, wxDEFAULT_FRAME_STYLE)
{
    panel = new wxPanel(this, -1);
    label = new wxStaticText(panel, -1, wxT("I am the label"), wxDefaultPosition, wxDefaultSize, wxALIGN_CENTRE);

    SetTitle(wxT("Frame"));
    
    wxBoxSizer* topSizer = new wxBoxSizer(wxVERTICAL);
    wxBoxSizer* sizer = new wxBoxSizer(wxHORIZONTAL);
    sizer->Add(label, 1, wxALIGN_CENTER_VERTICAL, 0);
    panel->SetAutoLayout(true);
    panel->SetSizer(sizer);
    sizer->Fit(panel);
    sizer->SetSizeHints(panel);
    topSizer->Add(panel, 1, wxEXPAND, 0);
    SetAutoLayout(true);
    SetSizer(topSizer);
    topSizer->Fit(this);
    topSizer->SetSizeHints(this);
    Layout();
}
upCASE
-----------------------------------
If it was hard to write, it should be hard to read!- Do. Or do not. There is no try!
Message5. Re: Trying to center labels.
#4808
Posted by: 2004-07-13 03:58:08
Thanks a lot! That's been really helpful for me!
Message6. Re: Trying to center labels.
#4810
Posted by: upCASE 2004-07-13 17:06:47
Hi!
You're welcome!
Working and understanding sizers is one of the hard parts when working with wxWidgets. Although I know how they work in general, it's a lot easier to use a tool like wxGlade to create the first scetches of a GUI.
upCASE
-----------------------------------
If it was hard to write, it should be hard to read!- Do. Or do not. There is no try!
Forum List • Thread List • Reply • Refresh • New Topic • Search • Previous • Next First 1 Last
掌柜推荐
 
 
 
 
 
 
 
 
 
 
 
 
© Wed 2021-7-28  Guidance Laboratory Inc.
Email:webmaster1g.yi.org Hits:0