wxWidgets는 Sizer라고하여, 자동으로 위치를 잡아주는 클래스가 있다.

 

 Sizer을 사용하기 위해서 wxSizer을 포함시킨다.

 

 wxOptionDialog.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#pragma once
 
#include <wx/wx.h>
#include <wx/combobox.h>
#include <wx/fontenum.h>
 
#include <wx/sizer.h>
 
#ifndef __WX_WIDGETS_OPTION_DIALOG_H__
#define __WX_WIDGETS_OPTION_DIALOG_H__
 
// ID 값 정의
enum
{
    ID_Options = wxID_HIGHEST + 1 // 사용자 정의 ID
};
 
class wxOptionDialog : public wxDialog
{
public:
    wxOptionDialog(wxWindow* parent, wxWindowID id, const wxString& title,
        const wxPoint& pos = wxDefaultPosition,
        const wxSize& size = wxDefaultSize,
        long style = wxDEFAULT_DIALOG_STYLE);
};
 
#endif
cs

 

 

wxOptionDialog.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 
#include "wxOptionsDialog.h"
 
wxOptionDialog::wxOptionDialog(wxWindow* parent, wxWindowID id, const wxString& title,
    const wxPoint& pos,
    const wxSize& size,
    long style)
    : wxDialog(parent, id, title, pos, size, style) {
 
    // 컨트롤을 담을 메인 사이저 생성
    wxBoxSizer* mainSizer = new wxBoxSizer(wxVERTICAL);
 
    // 설정 대화 상자의 내용을 여기에 구성
    // 예: 설정 옵션을 위한 컨트롤 추가
    wxStaticText* staticText = new wxStaticText(this, wxID_ANY, "Font Family", wxPoint(2020), wxDefaultSize);
    mainSizer->Add(staticText, 0, wxALL, 10);
 
    // 콤보박스 생성 및 옵션 추가
    wxComboBox* comboBox = new wxComboBox(this, wxID_ANY, "", wxPoint(2050), wxSize(150-1));
 
    // 사용 가능한 폰트 패밀리 목록을 가져옵니다.
    wxArrayString fontFamilies = wxFontEnumerator::GetFacenames(wxFONTENCODING_SYSTEM, false);
 
    // 콤보박스에 폰트 패밀리를 추가합니다. foreach문 사용.
    for (const wxString& fontFamily : fontFamilies) {
        comboBox->Append(fontFamily);
    }
 
    mainSizer->Add(comboBox, 0, wxALL | wxEXPAND, 10);
 
    // OK 및 Cancel 버튼 추가
    wxSizer* buttonSizer = CreateStdDialogButtonSizer(wxOK | wxCANCEL);
    mainSizer->Add(buttonSizer, 0, wxALL | wxCENTER, 10);
 
    // 메인 사이저를 대화 상자에 설정
    SetSizer(mainSizer);
    mainSizer->SetSizeHints(this); // 이 호출은 대화 상자가 최소 크기로 축소되지 않도록 합니다.
}
cs

 

 이렇게 해서 메인 역할을 하는 BoxSizer로 수직으로 정렬해놓고,

 버튼에 관련된 Sizer로 버튼을 추가하고 나열하도록 해서 출력되는걸 확인 할 수 있다.

 

Posted by JunkMam
,

 wxWidgets에서는 FontFamily를 가지고 열거하는 'wxFontEnumerator' 클래스가 존재한다.

 

 그래서 이걸 사용하기 위해서 wxOptions.h와 wxOptions.cpp을 다음과 같이 수정한다.

 

 wxOptions.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#pragma once
 
#include <wx/wx.h>
#include <wx/combobox.h>
#include <wx/fontenum.h>
 
#ifndef __WX_WIDGETS_OPTION_DIALOG_H__
#define __WX_WIDGETS_OPTION_DIALOG_H__
 
// ID 값 정의
enum
{
    ID_Options = wxID_HIGHEST + 1 // 사용자 정의 ID
};
 
class wxOptionDialog : public wxDialog
{
public:
    wxOptionDialog(wxWindow* parent, wxWindowID id, const wxString& title,
        const wxPoint& pos = wxDefaultPosition,
        const wxSize& size = wxDefaultSize,
        long style = wxDEFAULT_DIALOG_STYLE);
};
 
#endif
cs

 

 wxOptions.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 
#include <wx/wx.h>
 
#include "wxOptionsDialog.h"
 
wxOptionDialog::wxOptionDialog(wxWindow* parent, wxWindowID id, const wxString& title,
    const wxPoint& pos,
    const wxSize& size,
    long style)
    : wxDialog(parent, id, title, pos, size, style) {
 
    // 설정 대화 상자의 내용을 여기에 구성
    // 예: 설정 옵션을 위한 컨트롤 추가
    new wxStaticText(this, wxID_ANY, "Font Family", wxPoint(2020), wxDefaultSize);
 
    // 콤보박스 생성 및 옵션 추가
    wxComboBox* comboBox = new wxComboBox(this, wxID_ANY, "", wxPoint(2050), wxSize(150-1));
 
    // 사용 가능한 폰트 패밀리 목록을 가져옵니다.
    wxArrayString fontFamilies = wxFontEnumerator::GetFacenames(wxFONTENCODING_SYSTEM, false);
 
    // 콤보박스에 폰트 패밀리를 추가합니다. foreach문 사용.
    for (const wxString& fontFamily : fontFamilies) {
        comboBox->Append(fontFamily);
    }
 
    // OK 및 Cancel 버튼 추가
    CreateStdDialogButtonSizer(wxOK | wxCANCEL);
}
cs

 

 이렇게 사용하면, FontFamily를 아주 간단하게 출력할 수 있는걸 확인 할 수 있다.

Posted by JunkMam
,

 

 wxWidgets에서 콤보박스를 추가하는 방법은 다음과 같다.

 

1
2
 
    wxComboBox* comboBox = new wxComboBox(this, wxID_ANY, "", wxPoint(2050), wxSize(150-1));
cs

 

 이걸 이용해서 설정창에서 콤보박스 추가하는 소스를 만들면

 

 wxOptionDialog.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#pragma once
 
#include <wx/wx.h>
#include <wx/combobox.h>
 
#ifndef __WX_WIDGETS_OPTION_DIALOG_H__
#define __WX_WIDGETS_OPTION_DIALOG_H__
 
// ID 값 정의
enum
{
    ID_Options = wxID_HIGHEST + 1 // 사용자 정의 ID
};
 
class wxOptionDialog : public wxDialog
{
public:
    wxOptionDialog(wxWindow* parent, wxWindowID id, const wxString& title,
        const wxPoint& pos = wxDefaultPosition,
        const wxSize& size = wxDefaultSize,
        long style = wxDEFAULT_DIALOG_STYLE);
};
 
#endif
cs

 

wxOptionDialog.cpp

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 
#include <wx/wx.h>
 
#include "wxOptionsDialog.h"
 
wxOptionDialog::wxOptionDialog(wxWindow* parent, wxWindowID id, const wxString& title,
    const wxPoint& pos,
    const wxSize& size,
    long style)
    : wxDialog(parent, id, title, pos, size, style) {
 
    // 설정 대화 상자의 내용을 여기에 구성
    // 예: 설정 옵션을 위한 컨트롤 추가
    new wxStaticText(this, wxID_ANY, "Settings Placeholder", wxPoint(2020), wxDefaultSize);
 
    // 콤보박스 생성 및 옵션 추가
    wxComboBox* comboBox = new wxComboBox(this, wxID_ANY, "", wxPoint(2050), wxSize(150-1));
    comboBox->Append("Option 1");
    comboBox->Append("Option 2");
    comboBox->Append("Option 3");
 
    // OK 및 Cancel 버튼 추가
    CreateStdDialogButtonSizer(wxOK | wxCANCEL);
}
cs

 

 

 이렇게 넣고, Option메뉴를 누르면, 다음과 같은 창이 완성된다.

 

Posted by JunkMam
,

 

 wxWidgets에서 Font를 변경하는 기능을 사용하기 위해서 wxWidgets에서 다음을 지원해준다.

 

1
2
3
4
5
 
    // 폰트 설정
    wxFont* font = new wxFont(16, wxFONTFAMILY_TELETYPE, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD);
    textControl->SetFont(*font);
 
cs

 

이렇게 해서 wxFont를 사용해서 폰트의 설정을 저장하거나 받아 올 수 있다.

 font->GetPointSize()을 이용하면, 폰트의 사이즈를 얻을 수 있고, font->GetFamilyString()을 사용하면, 폰트의 종류를 얻을 수도 있다.

 

간단하게 설정을 하는 방법으로 다음과 같이 적용할 수 있다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#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""프로그램 종료");
 
    wxMenu* menuOptions = new wxMenu;
    menuOptions->Append(ID_Options, "&Options""Options Setting");
 
    wxMenuBar* menuBar = new wxMenuBar;
    menuBar->Append(menuFile, "&File");
    menuBar->Append(menuOptions, "&Options");
 
    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를 강제로 다시 계산하여 적용합니다.
 
    // 폰트 설정
    wxFont* font = new wxFont(16, wxFONTFAMILY_TELETYPE, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD);
    textControl->SetFont(*font);
 
    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);
 
    Bind(wxEVT_MENU, &MyFrame::OnSettings, this, ID_Options);
}
 
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// 사용자가 취소했을 때
 
    std::ifstream file(openFileDialog.GetPath().ToStdString());
    // 파일을 열고 텍스트 컨트롤에 내용을 로드합니다.
    if (textControl->LoadFile(openFileDialog.GetPath())) {
        std::stringstream buffer;
        buffer << file.rdbuf(); // 파일의 내용을 buffer에 읽어 들입니다.
        file.close(); // 파일을 닫습니다.
 
        // textControl의 내용을 갱신합니다.
        textControl->SetValue(buffer.str());
        //textControl->SetLabelText(buffer.str());
        // 타이틀을 열린 파일의 이름으로 설정합니다.
        SetTitle(openFileDialog.GetFilename());
 
    }
    else {
        wxMessageBox("Cannot open File!""Error", wxOK | wxICON_ERROR);
    }
}
 
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());
}
 
void MyFrame::OnSettings(wxCommandEvent& event)
{
    wxOptionDialog dialog(this, wxID_ANY, "Settings");
    if (dialog.ShowModal() == wxID_OK)
    {
        // 사용자가 설정을 변경하고 OK를 클릭했을 때의 처리
        SetStatusText("Settings Updated");
    }
}
cs

 

 

 이렇게 해서 wxWidgets에서 폰트를 개발자가 적용할 수 있다.

 이걸 이용해서 폰트를 개발자 외에 다른 사람들이 적용할 수 있게 변경하는 창을 만들면 적용이 가능하다.

Posted by JunkMam
,

 파일을 열면, TextCtrl에서 제대로된 파일 내용을 호출 하지 못하는 문제점이 있는데.

 이유는 wxWidgets에서 TextCtrl에 데이터를 옮기는 기능이 있어야하기 때문이다.

 

wxMain.h에 다음과 같은 방식으로 정의 한다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#pragma once
#include "wx/wx.h"
#include <wx/filedlg.h>
#include <wx/textctrl.h>
#include <wx/splitter.h>
 
// 파일을 읽어 들이기 위한 용도.
#include <fstream>
#include <sstream>
 
#include "wxOptionsDialog.h"
 
enum
{
    ID_QUIT,
};
 
enum {
    MY_EVENT_ID = 10001,
};
 
// ID 값 정의
enum
{
    ID_Settings_Menu = wxID_HIGHEST + 1 // 사용자 정의 ID
};
 
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);
 
    void OnSettings(wxCommandEvent& event);
};
 
cs

 

 wxMain.cpp에서 파일을 열면, 그 파일의 내용을 불러와서 작성할 수 있게 한다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#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""프로그램 종료");
 
    wxMenu* menuOptions = new wxMenu;
    menuOptions->Append(ID_Options, "&Options""Options Setting");
 
    wxMenuBar* menuBar = new wxMenuBar;
    menuBar->Append(menuFile, "&File");
    menuBar->Append(menuOptions, "&Options");
 
    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);
 
    Bind(wxEVT_MENU, &MyFrame::OnSettings, this, ID_Options);
}
 
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// 사용자가 취소했을 때
 
    std::ifstream file(openFileDialog.GetPath().ToStdString());
    // 파일을 열고 텍스트 컨트롤에 내용을 로드합니다.
    if (textControl->LoadFile(openFileDialog.GetPath())) {
        std::stringstream buffer;
        buffer << file.rdbuf(); // 파일의 내용을 buffer에 읽어 들입니다.
        file.close(); // 파일을 닫습니다.
 
        // textControl의 내용을 갱신합니다.
        textControl->SetValue(buffer.str());
        // 타이틀을 열린 파일의 이름으로 설정합니다.
        SetTitle(openFileDialog.GetFilename());
 
    }
    else {
        wxMessageBox("Cannot open File!""Error", wxOK | wxICON_ERROR);
    }
}
 
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());
}
 
void MyFrame::OnSettings(wxCommandEvent& event)
{
    wxOptionDialog dialog(this, wxID_ANY, "Settings");
    if (dialog.ShowModal() == wxID_OK)
    {
        // 사용자가 설정을 변경하고 OK를 클릭했을 때의 처리
        SetStatusText("Settings Updated");
    }
}
cs

 

 이렇게 해서 file.rdbuf을 이용해서 버퍼를 저장하고, 그걸 textControl에 출력하도록 하는 것으로 SetValue라는 메소드를 사용한다.

 SetLabelString이라는 것과 같이 있는데.

 여기서, SetLabelString은 줄 바꿈이 존재하지 않는 것이고, SetValue는 줄바꿈을 자동으로 적용해준다.

Posted by JunkMam
,

 wxWidgets에서 윈도우 제목 변경하기 위해선 wxWidets에서 이런 기능을 지원해준다.

 

1
    SetTitle("Test");
cs

 

 이렇게 하면, Test라는 타이틀을 적용하는것이 가능하다.

 

 

 이렇게 적용이 가능하다.

 이걸 이용해서 파일을 읽어 오면, 파일의 이름을 적용하는 방법이 가능해진다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 
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// 사용자가 취소했을 때
 
    // 파일을 열고 텍스트 컨트롤에 내용을 로드합니다.
    {
        // 열린 파일의 이름을 타이틀로 설정합니다.
        SetTitle(openFileDialog.GetFilename());
    }
}
cs

 

 

Posted by JunkMam
,

 

 옵션을 띄우는 간단한 장치로 Dialog를 적용하는 것이 있다.

 간단하게 옵션창을 띄우기 위해서 창에 관련된 정보를 다음과 같이 처리하기 위해서 "wxDialog"을 사용하는 방법이 있다.

 

SettingDialog.h라는 파일을 추가한다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#pragma once
 
#include <wx/wx.h>
 
#ifndef __WX_WIDGETS_SETTING_DIALOG_H__
#define __WX_WIDGETS_SETTING_DIALOG_H__
 
// ID 값 정의
enum
{
    ID_Settings = wxID_HIGHEST + 1 // 사용자 정의 ID
};
 
class SettingsDialog : public wxDialog
{
public:
    SettingsDialog(wxWindow* parent, wxWindowID id, const wxString& title,
        const wxPoint& pos = wxDefaultPosition,
        const wxSize& size = wxDefaultSize,
        long style = wxDEFAULT_DIALOG_STYLE);
};
 
#endif
cs

 

여기서 간단한 방법을 적용하는 방법으로 다음과 같이 한다.

 

Setting.Dialog.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 
#include <wx/wx.h>
 
#include "SettingsDialog.h"
 
SettingsDialog::SettingsDialog(wxWindow* parent, wxWindowID id, const wxString& title,
    const wxPoint& pos,
    const wxSize& size,
    long style)
    : wxDialog(parent, id, title, pos, size, style) {
 
    // 설정 대화 상자의 내용을 여기에 구성
    // 예: 설정 옵션을 위한 컨트롤 추가
    new wxStaticText(this, wxID_ANY, "Settings Placeholder", wxPoint(2020), wxDefaultSize);
    // OK 및 Cancel 버튼 추가
    CreateStdDialogButtonSizer(wxOK | wxCANCEL);
 
}
cs

 

이렇게 하면, wxWidgets에서 제공해주는 wxDialog를 적용이 가능하다.

 

제대로 적용한다면, 다음과 같이 사용한다.

 

wxMain.h에 다음과 같이 수정한다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#pragma once
#include "wx/wx.h"
#include <wx/filedlg.h>
#include <wx/textctrl.h>
#include <wx/splitter.h>
 
#include "SettingsDialog.h"
 
enum
{
    ID_QUIT,
};
 
enum {
    MY_EVENT_ID = 10001,
};
 
// ID 값 정의
enum
{
    ID_Settings_Menu = wxID_HIGHEST + 1 // 사용자 정의 ID
};
 
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);
 
    void OnSettings(wxCommandEvent& event);
};
 
cs

 

 

main에 제대로 적용하기 위해서

wxMain.cpp을 다음과 같이 수정한다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#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""프로그램 종료");
 
    wxMenu* menuOptions = new wxMenu;
    menuOptions->Append(ID_Settings, "&Options""Options Setting");
 
    wxMenuBar* menuBar = new wxMenuBar;
    menuBar->Append(menuFile, "&File");
    menuBar->Append(menuOptions, "&Options");
 
    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);
 
    Bind(wxEVT_MENU, &MyFrame::OnSettings, this, ID_Settings);
}
 
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());
}
 
void MyFrame::OnSettings(wxCommandEvent& event)
{
    SettingsDialog dialog(this, wxID_ANY, "Settings");
    if (dialog.ShowModal() == wxID_OK)
    {
        // 사용자가 설정을 변경하고 OK를 클릭했을 때의 처리
        SetStatusText("Settings Updated");
    }
}
cs

 

 이렇게 해서 적용하면, 제대로 동작하는걸 확인이 가능하다.

 

 

 이렇게 해서 메뉴가 추가되고,

 

 

 이런창이 띄워지면서 처리가 가능하다.

 

 

 

wxWidget_VS_2023_11_29_0830_ex00.zip
1.05MB

Posted by JunkMam
,

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
,

 

 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
,