프로그래밍/wxWidgets

[wxWidgets] 문자열 출력하기

JunkMam 2024. 2. 2. 00:00

 wxWidgets에서 글자를 표시하기 위해서는 wxStaticText라는 클래스를 조작할 수 있어야한다.

 wxStaticText를 추가하기 위해서는 다음과 같은 소스를 추가하면 된다.

    // 정적 텍스트 생성
    wxStaticText* labelText = new wxStaticText(panel1, wxID_ANY, "Text", wxPoint(20, 60), wxDefaultSize);

 

 

 이 labelText에서 값을 누르면, 값이 변경되길 원한다면, 버튼에서 사용했던 OnButtonClicked에서 다음과 같이 수정해주면 된다.

 

void MyFrame::OnButtonClick(wxCommandEvent& event)
{
    // 버튼 클릭 시 수행할 작업
    wxMessageBox("버튼이 클릭되었습니다!", "알림", wxOK | wxICON_INFORMATION, this);
    labelText->SetLabel("Test Text");
}

 

 

 여기서 중요한 점은 labelText가 전역변수여야한다.

 그래서, 전체 소스를 본다면

#include "wxMain.h"

wxIMPLEMENT_APP(MyApp);

// 정적 텍스트 생성
wxStaticText* labelText;
bool MyApp::OnInit()
{
    MyFrame* frame = new MyFrame("Serial Graph");
    frame->Show(true);
    return true;
}

MyFrame::MyFrame(const wxString& title)
    : wxFrame(NULL, wxID_ANY, title)
{
    wxMenu* menuFile = new wxMenu;
    menuFile->Append(ID_QUIT, "E&xit\tAlt-X", "프로그램 종료");

    wxMenuBar* menuBar = new wxMenuBar;
    menuBar->Append(menuFile, "&File");

    SetMenuBar(menuBar);
    wxPanel* panel1 = new wxPanel(this, wxID_ANY);

    // 버튼 추가
    wxButton* myButton = new wxButton(panel1, wxID_ANY, wxT("Click Me"),
        wxPoint(10, 10), wxDefaultSize, 0);

    // 정적 텍스트 생성
    labelText = new wxStaticText(panel1, wxID_ANY, "Text", wxPoint(20, 60), wxDefaultSize);

    // 이벤트 핸들러 연결
    Bind(wxEVT_MENU, &MyFrame::OnQuit, this, ID_QUIT);
    myButton->Bind(wxEVT_BUTTON, &MyFrame::OnButtonClick, this);
}

void MyFrame::OnQuit(wxCommandEvent& event)
{
    Close(true);
}

void MyFrame::OnButtonClick(wxCommandEvent& event)
{
    // 버튼 클릭 시 수행할 작업
    wxMessageBox("버튼이 클릭되었습니다!", "알림", wxOK | wxICON_INFORMATION, this);
    labelText->SetLabel("Test Text");
}

 

 이렇게 변경된다.