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

  1. 2024.02.04 [wxWidgets] 텍스트 컨트롤 창 가득 채우기.
  2. 2024.02.04 [wxWidgets] 상태바 추가하기.

wxMain.h

#pragma once
#include "wx/wx.h"
#include <wx/filedlg.h>
#include <wx/textctrl.h>
#include <wx/splitter.h>

enum
{
    ID_QUIT,
};

enum {
    MY_EVENT_ID = 10001,
};

class MyApp : public wxApp
{
public:
    virtual bool OnInit();
};

class MyFrame : public wxFrame
{
public:
    MyFrame(const wxString& title);

    void OnQuit(wxCommandEvent& event);

private:
    wxTextCtrl* textControl;

    void OnOpen(wxCommandEvent& event);
    void OnSave(wxCommandEvent& event);
    void OnButtonClick(wxCommandEvent& event);
};

 

 

wxMain.c

#include "wxMain.h"

wxIMPLEMENT_APP(MyApp);

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(wxID_OPEN, "&Open\tCtrl-O", "Open a file");
    menuFile->Append(wxID_SAVE, "&Save\tCtrl-S", "Save the file");
    menuFile->AppendSeparator();
    menuFile->Append(ID_QUIT, "E&xit\tAlt-X", "프로그램 종료");

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

    SetMenuBar(menuBar);

    textControl = new wxTextCtrl(this, wxID_ANY, "", wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE);

    // sizer를 생성하여 텍스트 컨트롤의 크기를 조정합니다.
    wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
    sizer->Add(textControl, 1, wxEXPAND | wxALL, 0); // wxEXPAND는 컨트롤이 sizer의 가능한 모든 공간을 차지하도록 합니다. 1은 비율을 의미하며, 이 경우 다른 컨트롤이 없으므로 전체 크기를 차지합니다.

    // 프레임에 sizer를 설정합니다.
    this->SetSizer(sizer);
    this->Layout(); // sizer를 강제로 다시 계산하여 적용합니다.


    CreateStatusBar();
    SetStatusText("Ready");

    // 이벤트 핸들러 연결
    Bind(wxEVT_MENU, &MyFrame::OnQuit, this, ID_QUIT);
    Bind(wxEVT_MENU, &MyFrame::OnOpen, this, wxID_OPEN);
    Bind(wxEVT_MENU, &MyFrame::OnSave, this, wxID_SAVE);
}

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

void MyFrame::OnOpen(wxCommandEvent& event)
{
    wxFileDialog openFileDialog(this, _("Open TXT file"), "", "",
        "TXT files (*.txt)|*.txt", wxFD_OPEN | wxFD_FILE_MUST_EXIST);
    if (openFileDialog.ShowModal() == wxID_CANCEL)
        return; // 사용자가 취소했을 때

    // 파일을 열고 텍스트 컨트롤에 내용을 로드합니다.
    textControl->LoadFile(openFileDialog.GetPath());
}

void MyFrame::OnSave(wxCommandEvent& event)
{
    wxFileDialog saveFileDialog(this, _("Save TXT file"), "", "",
        "TXT files (*.txt)|*.txt", wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
    if (saveFileDialog.ShowModal() == wxID_CANCEL)
        return; // 사용자가 취소했을 때

    // 현재 텍스트 컨트롤의 내용을 파일에 저장합니다.
    textControl->SaveFile(saveFileDialog.GetPath());
}
Posted by JunkMam
,

 윈도우 창에서 하단에서 작은 창이 존재하면서 상태값이 표시되는 부분이 있는데.

 이것을 StatusBar라고한다.

 

 wxWidgets에서는 StatusBar를 추가하기 위해서 다음의 함수를 지원해준다.

 

CreateStatusBar();

 

 

 이렇게 하면, 다음과 같은 창이 띄워진다.

 

 

 

그리고, 이 상태 값을 글자로 표시하기 위해서는 다음의 함수를 지원한다.

 

    SetStatusText("");

 

 

 이걸 제대로 적용한 전체 소스이다.

 wxMain.h

#pragma once
#include "wx/wx.h"
#include <wx/filedlg.h>
#include <wx/textctrl.h>
#include <wx/splitter.h>

enum
{
    ID_QUIT,
};

enum {
    MY_EVENT_ID = 10001,
};

class MyApp : public wxApp
{
public:
    virtual bool OnInit();
};

class MyFrame : public wxFrame
{
public:
    MyFrame(const wxString& title);

    void OnQuit(wxCommandEvent& event);

private:
    wxTextCtrl* textControl;

    void OnOpen(wxCommandEvent& event);
    void OnSave(wxCommandEvent& event);
    void OnButtonClick(wxCommandEvent& event);
};

 

 

 wxMain.c

#include "wxMain.h"

wxIMPLEMENT_APP(MyApp);

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(wxID_OPEN, "&Open\tCtrl-O", "Open a file");
    menuFile->Append(wxID_SAVE, "&Save\tCtrl-S", "Save the file");
    menuFile->AppendSeparator();
    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);

    textControl = new wxTextCtrl(this, wxID_ANY, "", wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE);

    CreateStatusBar();
    SetStatusText("Ready");

    // 이벤트 핸들러 연결
    Bind(wxEVT_MENU, &MyFrame::OnQuit, this, ID_QUIT);
    Bind(wxEVT_MENU, &MyFrame::OnOpen, this, wxID_OPEN);
    Bind(wxEVT_MENU, &MyFrame::OnSave, this, wxID_SAVE);
}

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

void MyFrame::OnOpen(wxCommandEvent& event)
{
    wxFileDialog openFileDialog(this, _("Open TXT file"), "", "",
        "TXT files (*.txt)|*.txt", wxFD_OPEN | wxFD_FILE_MUST_EXIST);
    if (openFileDialog.ShowModal() == wxID_CANCEL)
        return; // 사용자가 취소했을 때

    // 파일을 열고 텍스트 컨트롤에 내용을 로드합니다.
    textControl->LoadFile(openFileDialog.GetPath());
}

void MyFrame::OnSave(wxCommandEvent& event)
{
    wxFileDialog saveFileDialog(this, _("Save TXT file"), "", "",
        "TXT files (*.txt)|*.txt", wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
    if (saveFileDialog.ShowModal() == wxID_CANCEL)
        return; // 사용자가 취소했을 때

    // 현재 텍스트 컨트롤의 내용을 파일에 저장합니다.
    textControl->SaveFile(saveFileDialog.GetPath());
}

 

 상태바를 컨트롤하기 위해서 SetStatusText등을 사용하면, 상태값을 적용할 수 있다.

 

Posted by JunkMam
,