'2024/02/02'에 해당되는 글 2건

  1. 2024.02.02 [wxWidgets] Button 추가하고, 이벤트 처리
  2. 2024.02.02 [wxWidgets] 문자열 출력하기

 

 wxWidgets에서 윈도우에 존재하는 패널에서 간단한 버튼을 넣을려고한다.

 

    wxButton* myButton = new wxButton(panel1, wxID_ANY, wxT("Click Me"), 
                                      wxPoint(10, 10), wxDefaultSize, 0);

 

 이렇게 하면, 간단한 버튼이 생성되고, panel1에 추가 된다.

 

 버튼을 눌렀을때는 button에 wxEVT_BUTTON이라는 이벤트가 발생한다.

 그래서, myButton에 Bind하여 이벤트를 인식하도록 한다.

 

    myButton->Bind(wxEVT_BUTTON, &MyFrame::OnQuit, this);

 

이렇게 하면, OnQuit 메소드를 호출하여 동작하고, 개별로 동작했으면 좋겠으면 다음과 같이 작성하면 된다.

    myButton->Bind(wxEVT_BUTTON, &MyFrame::OnButtonClick, this);

 

이렇게 하면, OnButtonClick이라는 함수에서 동작하게 된다.

 

OnButtonClick은 개발자가 임의로 제작하면 되기에 '선언'과 '정의'를 하면, 해당 버튼이 클릭이 되었을때, 메소드를 호출하게 된다.

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

 

 이렇게 되면, 버튼을 클릭했을때, 이벤트를 처리하면서 알림창이 뜨게 된다.

 

 

Posted by JunkMam
,

 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");
}

 

 이렇게 변경된다.

Posted by JunkMam
,