옵션을 띄우는 간단한 장치로 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(20, 20), 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 |
이렇게 해서 적용하면, 제대로 동작하는걸 확인이 가능하다.
이렇게 해서 메뉴가 추가되고,
이런창이 띄워지면서 처리가 가능하다.
'프로그래밍 > wxWidgets' 카테고리의 다른 글
[wxWidgets] 파일 내용을 TextCtrl에 적용하기. (0) | 2024.02.06 |
---|---|
[wxWidgets] 윈도우 제목 변경 기능 (0) | 2024.02.05 |
[wxWidgets] 텍스트 컨트롤 창 가득 채우기. (0) | 2024.02.04 |
[wxWidgets] 상태바 추가하기. (0) | 2024.02.04 |
[wxWidgets] Button 추가하고, 이벤트 처리 (0) | 2024.02.02 |