파일을 열면, 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
,

 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
,

 

Visual Studio에서 wxWidgets을 적용하기 위해서 설정.

 

 wxWidget에서 Visual Studio를 적용하기 위해선 제대로 지원을 해주지 않기 때문에 빈 프로젝트를 만들어 줘야한다.

 

이렇게 빈 프로젝트를 생성해준다.

 

그 다음은 'Visual Studio와 wxWidgets 연동하기.'여기에 설명되어 있는 방식으로 wxWidgets을 설정 해줘야한다.

 

여기서 외부 종속성에서 wxWidgets에 관련된 경로가 찾아지는데 시간이 걸린다.

 

간단하게 윈도우를 출력하는걸 표현하기 위해서 wxMain.h을 만들고, 다음과 같이 작성해보자.

#pragma once
#include "wx/wx.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:
};

 

 간단한 wxMain.cpp 관련된 정의를 다음과 같이 구현한다.

#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(ID_QUIT, "E&xit\tAlt-X", "프로그램 종료");

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

    SetMenuBar(menuBar);

    wxSplitterWindow* splitter = new wxSplitterWindow(this);
    wxPanel* panel1 = new wxPanel(splitter, wxID_ANY);
    wxPanel* panel2 = new wxPanel(splitter, wxID_ANY);

    splitter->SplitVertically(panel1, panel2);

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

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

 

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

 

 

 

Posted by JunkMam
,

 wxWidgets에서 GUI 어플리케이션을 개발할때, 메뉴를 추가하는건 다음을 필요로한다.

 

 1. 메뉴바 생성.

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

 

 wxWidgets에서 지원해주는 클래스로 wxMenuBar라는 클래스가 있다. 이 클래스는 윈도우 상단에 위치하는 메뉴바를 나타내는 것으로 여기서 메뉴의 항목이라는 부분이 있게 된다.

 

 윈도우의 'F'를 단축키를 가지는 'File'이라는 메뉴를 추가 할 수 있게 된다.

 여기서 하위 메뉴를 추가하고자한다면, 다음과 같은 작업을 하면된다.

    wxMenu* menuFile = new wxMenu;
    menuFile->Append(ID_QUIT, "E&xit\tAlt-X", "프로그램 종료");

 

이것을 메뉴를 추가를 이상없이 적용하고 위해선 다음과 같이 한다.

    SetMenuBar(menuBar);

 

 이렇게 하면, 윈도우에 적용하게 된다.

 

 각 메뉴에 대해서 이벤트를 처리하는 방법은 다음과 같다.

 

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

 

 wxEVT_MENU라는 것은 MENU에 대한 이벤트를 적용하는 것이 있다.

 MyFrame::OnQuit라는 클래스의 메소드를 바로 연결해서 이벤트 처리하는 기능을 넣을 수 있다.

Posted by JunkMam
,