wxWIdgets에서 Font 설정을 보낸 후에 받는 곳에선 다음과 같이 처리하면 된다.
1
2
3
4
5
6
7
8
9
10
11
12
13
|
// 이벤트 처리 함수 구현
void MyFrame::OnMyCustomEvent(MyCustomEvent& event)
{
wxFont info = event.GetwxFont();
// 이벤트와 함께 전달된 정보 처리
//textControl->SetFont(info);
wxFont font(info.GetPointSize(), info.GetFamily(), info.GetStyle(), info.GetWeight());
textControl->SetFont(font);
//dialog->Destroy(); // dialog를 안전하게 삭제
//delete dialog;
}
|
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
#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_FontStyle, "Font&Style", "Font Style Set Menu");
wxMenuBar* menuBar = new wxMenuBar;
menuBar->Append(menuFile, "&File");
menuBar->Append(menuOptions, "&F&omat");
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(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_FontStyle);
// 이벤트 처리기 등록
Bind(MY_CUSTOM_EVENT, &MyFrame::OnMyCustomEvent, this);
}
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)
{
dialog = new wxOptionDialog(this, wxID_ANY, "Settings");
dialog->ShowModal();
//if (dialog.ShowModal() == wxID_OK)
//{
// // 사용자가 설정을 변경하고 OK를 클릭했을 때의 처리
// SetStatusText("Settings Updated");
//}
}
// 이벤트 처리 함수 구현
void MyFrame::OnMyCustomEvent(MyCustomEvent& event)
{
wxFont info = event.GetwxFont();
int fontSize = info.GetPointSize();
// 이벤트와 함께 전달된 정보 처리
textControl->SetFont(info);
//textControl->SetFont(font);
dialog->Destroy(); // dialog를 안전하게 삭제
delete dialog;
}
|
cs |
'프로그래밍 > wxWidgets' 카테고리의 다른 글
[wxWidgets] 새 글 기능 구현하기. (0) | 2024.02.11 |
---|---|
[wxWidgets] 체크 할 수 있는 메뉴 추가하기. (1) | 2024.02.11 |
[wxWidgets] Font 변경하는 창으로 수정하기. (0) | 2024.02.09 |
[wxWidgets] Custom Event을 제작하기. (0) | 2024.02.09 |
[wxWidgets] Event 전송하기. (1) | 2024.02.08 |