wxWidgets의 wxTextCtrl를 가지고 왔을때, std::stringstream으로 해서 받아와서 그런지. find에서 줄 바꿈을 제대로 처리 못하는 문제점이 있다.

 그래서 줄 바꿈이 있을시 검색하는 기능으로 수정했다.

 

 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
 
// FindDialog를 다음 문자을 검색했을때 처리하는 이벤트
void MyFrame::OnFindNext(wxFindDialogEvent& event) {
    wxString searchString = event.GetFindString();
    int flags = event.GetFlags();
 
    // 검색 시작 위치를 결정합니다.
    long startPos = m_textControl->GetInsertionPoint();
    long textLength = m_textControl->GetValue().length();
 
    // 대소문자 구분 여부를 확인합니다.
    wxString textToSearch = m_textControl->GetValue();
    if (!(flags & wxFR_MATCHCASE))
    {
        textToSearch.MakeLower();
        searchString.MakeLower();
    }
 
    // 검색을 시작합니다.
    long foundPos = -1;
    if (flags & wxFR_DOWN) // "다음 찾기"인 경우
    {
        foundPos = textToSearch.find(searchString, startPos);
        // 검색된 위치가 현재 커서 위치보다 앞인 경우 다음 위치부터 다시 검색
        if (foundPos == startPos) {
            foundPos = textToSearch.find(searchString, startPos + 1);
        };
    }
    else // "이전 찾기"인 경우 (옵션에 따라)
    {
        wxString textToStartPos = textToSearch.Mid(0, startPos);
        foundPos = textToStartPos.rfind(searchString);
    }
 
    if (foundPos != wxString::npos)
    {
        int lengths = searchString.length();
 
        // \r\n 기준으로 검색된 위치를 찾습니다.
        long lineStart = textToSearch.rfind('\n', foundPos);
        if (lineStart != wxString::npos)
        {
            long lineEnd = textToSearch.find('\n', foundPos);
            if (lineEnd == wxString::npos)
                lineEnd = textToSearch.length();
 
            // 검색된 텍스트가 보이도록 스크롤합니다.
            m_textControl->ShowPosition(lineStart);
 
            // 검색된 텍스트를 선택합니다.
            m_textControl->SetSelection(lineStart, lineEnd+1);
        }
        else
        {
            // \n이 없는 경우 해당 위치로 이동하고 텍스트를 선택합니다.
            m_textControl->SetSelection(foundPos, foundPos + lengths);
            m_textControl->ShowPosition(foundPos);
        }
 
        m_textControl->SetFocus();
    }
    else
    {
        wxMessageBox("Search string not found.""Find", wxOK | wxICON_INFORMATION);
    }
}
 
cs


 이렇게 하면, 줄 바꿈이 있어도 제대로된 값을 검색할 수 있다.

 여기서 lineEnd+1이 붙은 것은 줄바꿈은 \r\n이라는 형태로 행이 바뀌고, 다시 초기 부분으로 바뀌는 부분이 있어서 +1을 하게 되었다.

 buffer의 길이에서 이런 개행 부분을 제대로 처리하는 변수로 바꾸면 문제가 없을 것으로 보인다.

Posted by JunkMam
,

 wxWidgets에서 검색 기능을 구현했는데.

 제대로 안되는 문제점이 있는걸 확인된다.

 커서 위치를 갱신해주지 않고, 그냥 해당 페이지 위치로 이동하는 문제점이 있는데.

 이걸 해결해주기 위해서 다음과 같이 수정한다.

 

 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
 
// FindDialog를 다음 문자을 검색했을때 처리하는 이벤트
void MyFrame::OnFindNext(wxFindDialogEvent& event) {
    wxString searchString = event.GetFindString();
    int flags = event.GetFlags();
 
    // 검색 시작 위치를 결정합니다.
    long startPos = m_textControl->GetInsertionPoint();
    long textLength = m_textControl->GetValue().length();
 
    // 대소문자 구분 여부를 확인합니다.
    wxString textToSearch = m_textControl->GetValue();
    if (!(flags & wxFR_MATCHCASE))
    {
        textToSearch.MakeLower();
        searchString.MakeLower();
    }
 
    // 검색을 시작합니다.
    long foundPos = -1;
    if (flags & wxFR_DOWN) // "다음 찾기"인 경우
    {
        foundPos = textToSearch.find(searchString, startPos);
        // 검색된 위치가 현재 커서 위치보다 앞인 경우 다음 위치부터 다시 검색
        if (foundPos == startPos) {
            foundPos = textToSearch.find(searchString, startPos + 1);
        };
    }
    else // "이전 찾기"인 경우 (옵션에 따라)
    {
        wxString textToStartPos = textToSearch.Mid(0, startPos);
        foundPos = textToStartPos.rfind(searchString);
    }
 
    if (foundPos != wxString::npos)
    {
        int lengths = searchString.length();
 
        m_textControl->ShowPosition(foundPos);
        m_textControl->SetSelection(foundPos, foundPos + lengths);
 
        m_textControl->SetFocus();
    }
    else
    {
        wxMessageBox("Search string not found.""Find", wxOK | wxICON_INFORMATION);
    }
}
cs

 

이렇게 하면, 커서 위치가 이동하고, 선택하며, 해당 단어까지 선택하는 기능이 완성하게 된다.

'm_textControl->ShowPosition(foundPos);'는 검색이 된 단어의 위치로 이동하게 되는 것이고, 

'm_textControl->SetSelection(foundPos, foundPos + lengths);'는 해당 단어를 선택하는 기능을 처리하는 것이다.

'm_textControl->SetFocus();'는 단어를 표시하기 위해서 적용하는 것으로 m_textControl에 검색했을때, 처리가 가능해진다.

Posted by JunkMam
,

 찾기(Find)을 구현하기 위해서는 문자열 비교 및 검색 기능을 구현해야되는데.

 wxWidgets에서는 기본적으로 제공해준다.

 여기서 wxFindReplaceDialog라는 창을 제공해주는데.

 이걸 사용하기 위해선 'fdrepdlg.h' 라는 헤더 파일을 포함해줘야한다.

 

wxMain.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 
// Find Dialog
#include <wx/fdrepdlg.h>
 
class MyFrame : public wxFrame
{
public:
    ...
private:
    // 메모장 기능해주는 장치.
    wxTextCtrl* m_textControl;
 
    // 찾기 기능을 처리하기 위한 창.
    wxFindReplaceDialog* m_findDialog;
    wxFindReplaceData m_findData; // 찾기 및 바꾸기 데이터.
 
    ...
};
cs

 

이렇게 하면, wxFindReplaceDialog의 창을 불러와서 특정 문자열을 찾아서 제공하도록 할 수 있다.

 

이제, Edit의 하위 메뉴인 Find를 처리해주는 이벤트 핸들러와 Dialog로 데이터를 제공 받는 이벤트 핸들러들을 만든다.

Find를 처리해주는 이벤트 핸들러를 OnFind라고 하고,

FindReplace의 이벤트 핸들러는 OnFindClose, OnFindNext로 한다.

 

wxMain.h

1
2
3
4
    void OnFind(wxCommandEvent& event);
    // FindDialog 이벤트를 받는 핸들러
    void OnFindClose(wxFindDialogEvent& event); // FindDialog가 닫혔을시.
    void OnFindNext(wxFindDialogEvent& event); // FindDialog로 다음 문자을 검색할시.
cs

 

이제, 하위 메뉴를 추가 및 이벤트 핸들러를 연결한다.

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
    m_menuEdit = new wxMenu;
    m_menuEdit->Append(wxID_UNDO, "&Undo\tCtrl+Z""Undo the last action");
    m_menuEdit->Append(wxID_REDO, "&Redo\tCtrl+Y""Redo the last undone action");
    m_menuFile->AppendSeparator();
    m_menuEdit->Append(wxID_CUT, "Cu&t\tCtrl+X""Cut the selected text to the clipboard");
    m_menuEdit->Append(wxID_COPY, "&Copy\tCtrl+C""Copy the selected text to the clipboard");
    m_menuEdit->Append(wxID_PASTE, "&Paste\tCtrl+V""Paste text from the clipboard");
    m_menuFile->AppendSeparator();
    m_menuEdit->Append(wxID_FIND, "&Findl\tCtrl+F""Find text");
    m_menuFile->AppendSeparator();
    m_menuEdit->Append(ID_SELECT_ALL, "&Select All\tCtrl+A""Select all text");
    m_menuFile->AppendSeparator();
    m_menuEdit->AppendCheckItem(ID_DATETIME, "&DateTime\tF5""DateTime");
 
    // 메뉴 Edit에 관련된 이벤트 핸들러
    Bind(wxEVT_MENU, &MyFrame::OnUndo, this, wxID_UNDO);
    Bind(wxEVT_MENU, &MyFrame::OnRedo, this, wxID_REDO);
    Bind(wxEVT_MENU, &MyFrame::OnCut, this, wxID_CUT);
    Bind(wxEVT_MENU, &MyFrame::OnCopy, this, wxID_COPY);
    Bind(wxEVT_MENU, &MyFrame::OnPaste, this, wxID_PASTE);
 
    Bind(wxEVT_MENU, &MyFrame::OnFind, this, wxID_FIND);
 
    Bind(wxEVT_MENU, &MyFrame::OnSelectAll, this, ID_DATETIME);
    Bind(wxEVT_MENU, &MyFrame::OnInsertDateTime, this, ID_DATETIME);
 
 
    // FindDialog 이벤트 핸들러 적용.
    Bind(wxEVT_FIND_CLOSE, &MyFrame::OnFindClose, this);
    Bind(wxEVT_FIND, &MyFrame::OnFindNext, this);
cs

 

이제, 이벤트 핸들러를 구현한다.

먼저 Find 메뉴를 처리하는 이벤트 핸들러의 구현은 다음과 같이 한다.

1
2
3
4
5
6
void MyFrame::OnFind(wxCommandEvent& event) {
    if (!m_findDialog) {
        m_findDialog = new wxFindReplaceDialog(this&m_findData, "Find");
        m_findDialog->Show(true);
    }
}
cs

 

이후에 FindReplaceDialog를 처리하기 위한 이벤트 핸들러는 다음과 같이 한다.

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
// FindDialog를 닫았을때 처리하는 이벤트
void MyFrame::OnFindClose(wxFindDialogEvent& event) {
    m_findDialog->Destroy();
    m_findDialog = nullptr;
}
 
// FindDialog를 다음 문자을 검색했을때 처리하는 이벤트
void MyFrame::OnFindNext(wxFindDialogEvent& event){
    wxString searchString = event.GetFindString();
    int flags = event.GetFlags();
 
    // 검색 시작 위치를 결정합니다.
    long startPos = m_textControl->GetInsertionPoint();
    long textLength = m_textControl->GetValue().length();
 
    // 대소문자 구분 여부를 확인합니다.
    wxString textToSearch = m_textControl->GetValue();
    if (!(flags & wxFR_MATCHCASE))
    {
        textToSearch.MakeLower();
        searchString.MakeLower();
    }
 
    // 검색을 시작합니다.
    long foundPos = -1;
    if (flags & wxFR_DOWN) // "다음 찾기"인 경우
    {
        foundPos = textToSearch.find(searchString, startPos);
        // 검색된 위치가 현재 커서 위치보다 앞인 경우 다음 위치부터 다시 검색
        if (foundPos == startPos) {
            foundPos = textToSearch.find(searchString, startPos + 1);
        };
    }
    else // "이전 찾기"인 경우 (옵션에 따라)
    {
        wxString textToStartPos = textToSearch.Mid(0, startPos);
        foundPos = textToStartPos.rfind(searchString);
    }
 
    if (foundPos != wxString::npos)
    {
        m_textControl->ShowPosition(foundPos);
    }
    else
    {
        wxMessageBox("Search string not found.""Find", wxOK | wxICON_INFORMATION);
    }
}
cs

 

여기서, OnFindClose는 FindReplaceDialog가 종료되면, 반환하도록 만들어서 창을 제거하도록 만든다.

OnFindNext는 FindReplaceDialog의 상태를 보고 거기에 맞춰 검색하도록 하는 장치이다.

단어를 선택하지 않고, 현재 위치로 이동하여 자동으로 스크롤로 이동하도록 만든다.

Posted by JunkMam
,

 C에서는 동작하는 과정을 나눠지는 기능으로 제공하는 '조건문'이 있다.

 참과 거짓으로 처리하는 if문과 특정 값에서 이동하는 switch문이 있다.

 

 if문

 if문은 if( 조건 ) 으로 구성되며, 조건이 참(0이외의 값)과 거짓(0)으로 적용된다.

1
2
3
if( a ){
 // a가 1이상일경우.
}
cs

 

이렇게 되면, // a가 1이상일 경우에 관련된 동작을 작업할 수 있다.

조건에 맞지 않을 경우엔 else을 사용한다.

1
2
3
4
5
if( a ){
 // a가 1이상일경우.
}else{
 // a가 0일 경우.
}
cs

 

여기서 조건이 맞지 않았지만, 다른 조건일때 처리하는 방법으론 else뒤에 if(조건)을 넣는다.

1
2
3
4
5
6
7
if( a ){
 // a가 1이상일경우.
}else if( b ){
 // a가 0이지만, b는 1이상일 경우.
}else{
 // a와 b가 모두 다 0일 경우.
}
cs

 

여기서 이런 경우가 있을 수 있다.

 a가 1, 2, ,3 ,4가 될 경우가 있다고 해보자.

그러면, if문을 사용할 경우엔 다음과 같아진다.

1
2
3
4
5
6
7
8
9
if( a == 1 ){
 // a가 1일때
}else if( a == 2 ){
 // a가 2일때
}else if( a == 3 ){
 // a가 3일때
}else if( a == 4 ){
 // a가 4일때
}
cs

 

이럴때, if else로 조건문을 매번 확인해서 이동하는 것이 아니라.

특정 값에만 이동하도록 만드는 방법이 있는데.

그것이 바로 switch문이다.

 

switch문은 다음과 같이 적용된다.

1
2
3
4
5
6
7
8
9
switch( 값 ){
case 특정값:{
    break;
}
default:{
    // 값이 프로그래머가 정의 내리지 않는 값일 경우.
    break;
}
}
cs

 

이걸 전체적으로 다 적용한다면, 다음과 같이 된다.

 
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
#include <stdio.h>
 
#include <stdlib.h>
 
int main(int argc, char** argv){
    unsigned int a = 0;
    unsigned char steps = atoi(argv[1]);
 
    if(steps > 3){
        a = 100;
    }else{
        switch(steps){
        case 0:{
            a++;
            break;
        }
        case 1:
        case 2:{
            a = 5;
            break;
        }
        case 3:{
            a = 15;
            break;
        }
        }
    }
    printf("a: %d", a);
    return 0;
}
 
cs

 

 

 이렇게 하면,

 실행파일 뒤에 추가적으로 넣은 argv의 값을 보고(숫자가 들어가야된다.), 그 값에 맞춰서 a의 값을 정하여 값이 출력하게 된다.

 

'프로그래밍 > C 언어' 카테고리의 다른 글

[C] 연산자  (0) 2024.02.16
[C] 상수와 변수  (0) 2024.02.15
[C] 기본적으로 지원해주는 출력  (0) 2024.02.14
[C] 기본 구조  (1) 2024.02.13
Posted by JunkMam
,

 wxMain.h에는 이벤트 핸들러를 추가해준다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 
    // Edit 메뉴에 적용할 이벤트 핸들러
    void OnUndo(wxCommandEvent& event);
    void OnRedo(wxCommandEvent& event);
    void OnCut(wxCommandEvent& event);
    void OnCopy(wxCommandEvent& event);
    void OnPaste(wxCommandEvent& event);
 
 
    // 메뉴 동적 활성화 하기 위한 이벤트 핸들러
    void OnUpdateUndo(wxUpdateUIEvent& event);
    void OnUpdateRedo(wxUpdateUIEvent& event);
 
    void OnUpdateCut(wxUpdateUIEvent& event);
    void OnUpdateCopy(wxUpdateUIEvent& event);
    void OnUpdatePaste(wxUpdateUIEvent& event);
cs

 

 wxMain.cpp에는 메뉴 추가 및 이벤트 핸들러를 연결한다.

1
2
3
4
5
6
7
8
9
10
    m_menuEdit = new wxMenu;
    m_menuEdit->Append(wxID_UNDO, "&Undo\tCtrl+Z""Undo the last action");
    m_menuEdit->Append(wxID_REDO, "&Redo\tCtrl+Y""Redo the last undone action");
    m_menuFile->AppendSeparator();
    m_menuEdit->Append(wxID_CUT, "Cu&t\tCtrl+X""Cut the selected text to the clipboard");
    m_menuEdit->Append(wxID_COPY, "&Copy\tCtrl+C""Copy the selected text to the clipboard");
    m_menuEdit->Append(wxID_PASTE, "&Paste\tCtrl+V""Paste text from the clipboard");
    m_menuEdit->Append(ID_SELECT_ALL, "&Select All\tCtrl+A""Select all text");
    m_menuFile->AppendSeparator();
    m_menuEdit->AppendCheckItem(ID_DATETIME, "&DateTime\tF5""DateTime");
cs

 

이벤트 핸들러 연결

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    // 메뉴 Edit에 관련된 이벤트 핸들러
    Bind(wxEVT_MENU, &MyFrame::OnUndo, this, wxID_UNDO);
    Bind(wxEVT_MENU, &MyFrame::OnRedo, this, wxID_REDO);
    Bind(wxEVT_MENU, &MyFrame::OnCut, this, wxID_CUT);
    Bind(wxEVT_MENU, &MyFrame::OnCopy, this, wxID_COPY);
    Bind(wxEVT_MENU, &MyFrame::OnPaste, this, wxID_PASTE);
    Bind(wxEVT_MENU, &MyFrame::OnSelectAll, this, ID_DATETIME);
    Bind(wxEVT_MENU, &MyFrame::OnInsertDateTime, this, ID_DATETIME);
 
 
    // 메뉴바 갱신 이벤트 핸들러
    Bind(wxEVT_UPDATE_UI, &MyFrame::OnUpdateUndo, this, wxID_UNDO);
    Bind(wxEVT_UPDATE_UI, &MyFrame::OnUpdateRedo, this, wxID_REDO);
 
    Bind(wxEVT_UPDATE_UI, &MyFrame::OnUpdateCut, this, wxID_CUT);
    Bind(wxEVT_UPDATE_UI, &MyFrame::OnUpdateCopy, this, wxID_COPY);
    Bind(wxEVT_UPDATE_UI, &MyFrame::OnUpdatePaste, this, wxID_PASTE);
cs

 

이벤트 핸들러인 OnPaste와 OnUpdatePaste을 구현한다.

1
2
3
4
5
6
7
8
9
10
11
12
13
// 붙여넣기 기능.
void MyFrame::OnPaste(wxCommandEvent& event) {
    if (m_textControl->CanPaste()) {
        m_textControl->Paste();
    }
}
 
// 붙여넣기 기능 활성화.
void MyFrame::OnUpdatePaste(wxUpdateUIEvent& event)
{
    event.Enable(m_textControl->CanPaste());
}
 
cs

 

이렇게 하면, 마우스로 붙여넣기 메뉴를 사용해서 클립보드로 저장된걸 붙여넣을 수 있다.

Posted by JunkMam
,

 컴퓨터는 계산을 해주는 존재이고, 그걸 명령하기 위한 코드를 작성하는 것 중에 하나가 C언어이다.

 그래서, C에서는 간단한 연산자를 제공해준다.

 간단하게 사칙연산(+, -, *, /)와 나머지를 계산(%)이 있다.

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
#include <stdio.h>
 
int main() {
    int a = 4;
    int b = 3;
 
    int sum = 0;
    int sub = 0;
    int mul = 0;
    int div = 0;
    int mod = 0;
 
    // 덧셈
    sum = a+b;
    // 뺄셈
    sub = a-b;
    // 곱셈
    mul = a*b;
    // 나눗셈
    div = a/b;
    // 나머지
    mod = a%b;
 
    printf("덧셈 %d\n", sum);
    printf("뺄셈 %d\n", sub);
    printf("곱셈 %d\n", mul);
    printf("나눗셈 %d\n", div);
    printf("나머지 %d\n", mod);
 
    return 0;
}
 
cs

 

결과는 다음과 같이 나온다.

 

관계 연산자로 두개의 변수를 비교해서 참과 거짓을 표시하는 연산자이다.

같음, 다름, 미만, 초과, 이하, 이상(==, !=, <, >, <=, >=)을 적용할 수 있다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>
 
int main() {
    int a = 4;
    int b = 3;
 
    printf("같음 %d\n", a==b);
    printf("다름 %d\n", a!=b);
    printf("미만 %d\n", a<b);
    printf("초과 %d\n", a>b);
    printf("이하 %d\n", a<=b);
    printf("이상 %d\n", a>=b);
 
    return 0;
}
 
cs

 

논리 연산자로 한개 이상의 변수 혹은 조건들을 평가할때, 사용된다.

AND, OR, NOT(&&, ||, !)으로 조건문이나 반복문 같이 '조건'에 사용된다.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
 
int main() {
    int a = 4;
    int b = 3;
 
    printf("AND %d\n", (a==b)&&(a<b));
    printf("OR %d\n", (a==b)||(a<b));
    printf("NOT %d\n"!(a<b));
 
    return 0;
}
 
cs

 

 

비트 연산자는 변수를 이진수로 보고, 그걸 연산하는 용도로 사용된다.

AND, OR, XOR, NOT, 왼쪽 Shift, 오른쪽 Shift(&, |, ^, ~, <<, >>)로 하드웨어 연산 및 암호등 다양한 곳에 사용된다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>
 
int main() {
    int a = 4;
    int b = 3;
 
    printf("AND %d\n", a&1); //(B00000100) & (B00000001) = B00000000(0)
    printf("OR %d\n", a|1); // (B00000100) | (B00000001) = B00000101(5)
    printf("XOR %d\n", a^1); // (B00000100) ^ (B00000001) = B00000101(5)
    printf("NOT %d\n", ~a); // ~(B00000100) = (B11111011)(-5)
    printf("<< %d\n", a<<1); // (B00000100) << (B00000001) = B00001000(8)
    printf(">> %d\n", a>>1); // (B00000100) >> (B00000001) = B00000010(2)
 
    return 0;
}
 
cs

 

대입연산자(=)는 간단하게 변수에 데이터를 저장하는 연산자이다.

거기서 C는 간단한 연산을 덧붙여서 간단하게 표현할 수 있는 기능을 제공하는데.

원래 있던 변수 a에서 5을 더한다거나, 할때 a = a+5을 더 줄여서 a+=5로 작성할 수 있다.

이건 모든 대부분의 연산자들에게 적용이 된다.

 

위와 동일하게 증감 연산자라고해서 1씩 증가시켜야 되는 경우(반복문등으로 시그마, 파이, 수열 등 급수 연산할때) 사용이 되는 ++, --도 지원해준다.

 

 

 

 

'프로그래밍 > C 언어' 카테고리의 다른 글

[C] 제어(1) - 조건문 -  (0) 2024.02.17
[C] 상수와 변수  (0) 2024.02.15
[C] 기본적으로 지원해주는 출력  (0) 2024.02.14
[C] 기본 구조  (1) 2024.02.13
Posted by JunkMam
,

메뉴 Edit에 복사 메뉴 추가하는 방법은 다음과 같다.

wxMain.h에 이벤트 핸들러 추가.

1
2
3
4
5
6
7
8
9
10
11
12
13
    // Edit 메뉴에 적용할 이벤트 핸들러
    void OnUndo(wxCommandEvent& event);
    void OnRedo(wxCommandEvent& event);
    void OnCut(wxCommandEvent& event);
    void OnCopy(wxCommandEvent& event);
 
 
    // 메뉴 동적 활성화 하기 위한 이벤트 핸들러
    void OnUpdateUndo(wxUpdateUIEvent& event);
    void OnUpdateRedo(wxUpdateUIEvent& event);
 
    void OnUpdateCut(wxUpdateUIEvent& event);
    void OnUpdateCopy(wxUpdateUIEvent& event);
cs

 

Copy에 관련된 이벤트 핸들러 추가 완료한 후

Edit 하위 메뉴에 Copy을 추가하고, Copy관련된 이벤트 핸들러 연결 및 구현을 한다.

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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#include "wxMain.h"
 
wxIMPLEMENT_APP(MyApp);
 
bool MyApp::OnInit()
{
    MyFrame* frame = new MyFrame("No Title - Notepad");
    frame->Show(true);
    return true;
}
 
MyFrame::MyFrame(const wxString& title)
    : wxFrame(NULL, wxID_ANY, title)
{
    m_menuFile = new wxMenu;
    m_menuFile->Append(wxID_NEW, "&New\tCtrl-N""New a Notepad");
    m_menuFile->Append(ID_NEW_WINDOW, "&New Windows\tCtrl+Shift+N""Open a new window.");
    m_menuFile->Append(wxID_OPEN, "&Open\tCtrl-O""Open a file");
    m_menuFile->Append(wxID_SAVE, "&Save\tCtrl-S""Save the file");
    m_menuFile->Append(ID_SAVE_AS, "&Save As\tCtrl+Shift-S""Save the file");
    m_menuFile->AppendSeparator();
    m_menuFile->Append(ID_QUIT, "E&xit\tAlt-X""Exit Program");
 
    m_menuEdit = new wxMenu;
    m_menuEdit->Append(wxID_UNDO, "&Undo\tCtrl+Z""Undo the last action");
    m_menuEdit->Append(wxID_REDO, "&Redo\tCtrl+Y""Redo the last undone action");
    m_menuFile->AppendSeparator();
    m_menuEdit->Append(wxID_CUT, "Cu&t\tCtrl+X""Cut the selected text to the clipboard");
    m_menuEdit->Append(wxID_COPY, "&Copy\tCtrl+C""Copy the selected text to the clipboard");
    m_menuEdit->Append(ID_SELECT_ALL, "&Select All\tCtrl+A""Select all text");
    m_menuFile->AppendSeparator();
    m_menuEdit->AppendCheckItem(ID_DATETIME, "&DateTime\tF5""DateTime");
 
    m_menuFormat = new wxMenu;
    m_menuFormat->AppendCheckItem(ID_WordWarp, "Word &Wrap\tCtrl+W""Toggle word wrapping.");
    m_menuFormat->Append(ID_FontSetting, "&Font""Font Set Menu");
 
    m_menuView = new wxMenu;
    m_menuView->AppendCheckItem(ID_StatusBar, "Show &StatusBar""Toggle Status Bar.");
    
    m_menuBar = new wxMenuBar;
    m_menuBar->Append(m_menuFile, "&File");
    m_menuBar->Append(m_menuEdit, "&Edit");
    m_menuBar->Append(m_menuFormat, "F&omat");
    m_menuBar->Append(m_menuView, "&View");
 
    SetMenuBar(m_menuBar);
 
    m_textControl = new wxTextCtrl(this, wxID_ANY, "", wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE);
 
    // sizer를 생성하여 텍스트 컨트롤의 크기를 조정합니다.
    wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
    sizer->Add(m_textControl, 1, wxEXPAND | wxALL, 0); // wxEXPAND는 컨트롤이 sizer의 가능한 모든 공간을 차지하도록 합니다. 1은 비율을 의미하며, 이 경우 다른 컨트롤이 없으므로 전체 크기를 차지합니다.
 
    // 프레임에 sizer를 설정합니다.
    this->SetSizer(sizer);
    this->Layout(); // sizer를 강제로 다시 계산하여 적용합니다.
 
    // 폰트 설정
    wxFont font(16, wxFONTFAMILY_TELETYPE, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD);
    m_textControl->SetFont(font);
 
    //m_statusBar = new wxStatusBar(this);
    m_statusBar = CreateStatusBar();
    SetStatusText("Ready");
    // ID_StatusBar 메뉴 항목을 자동으로 체크되어 있도록 설정
    m_menuView->Check(ID_StatusBar, true);
 
    // 이벤트 핸들러 연결
    Bind(wxEVT_MENU, &MyFrame::OnNew, this, wxID_NEW);
 
    // MyFrame 생성자 또는 초기화 함수 내
    Bind(wxEVT_MENU, &MyFrame::OnNewWindow, this, ID_NEW_WINDOW);
 
    Bind(wxEVT_MENU, &MyFrame::OnOpen, this, wxID_OPEN);
    Bind(wxEVT_MENU, &MyFrame::OnSave, this, wxID_SAVE);
    Bind(wxEVT_MENU, &MyFrame::OnSaveAs, this, ID_SAVE_AS);
 
    Bind(wxEVT_MENU, &MyFrame::OnQuit, this, ID_QUIT);
 
    // 메뉴 Edit에 관련된 이벤트 핸들러
    Bind(wxEVT_MENU, &MyFrame::OnUndo, this, wxID_UNDO);
    Bind(wxEVT_MENU, &MyFrame::OnRedo, this, wxID_REDO);
    Bind(wxEVT_MENU, &MyFrame::OnCut, this, wxID_CUT);
    Bind(wxEVT_MENU, &MyFrame::OnCopy, this, wxID_COPY);
    Bind(wxEVT_MENU, &MyFrame::OnSelectAll, this, ID_DATETIME);
    Bind(wxEVT_MENU, &MyFrame::OnInsertDateTime, this, ID_DATETIME);
 
    // 메뉴 Format에 관련된 이벤트 핸들러
    Bind(wxEVT_MENU, &MyFrame::OnToggleWordWrap, this, ID_WordWarp);
    Bind(wxEVT_MENU, &MyFrame::OnFontSetting, this, ID_FontSetting);
 
    // 메뉴 View에 관련된 이벤트 핸들러
    Bind(wxEVT_MENU, &MyFrame::OnToggleStatusBar, this, ID_StatusBar);
 
    // 메뉴바 갱신 이벤트 핸들러
    Bind(wxEVT_UPDATE_UI, &MyFrame::OnUpdateUndo, this, wxID_UNDO);
    Bind(wxEVT_UPDATE_UI, &MyFrame::OnUpdateRedo, this, wxID_REDO);
 
    Bind(wxEVT_UPDATE_UI, &MyFrame::OnUpdateCut, this, wxID_CUT);
    Bind(wxEVT_UPDATE_UI, &MyFrame::OnUpdateCopy, this, wxID_COPY);
 
    // 이벤트 처리기 등록
    Bind(MY_CUSTOM_EVENT, &MyFrame::OnMyCustomEvent, this);
}
 
void MyFrame::OnQuit(wxCommandEvent& event)
{
    Close(true);
}
 
void MyFrame::OnNew(wxCommandEvent& event) {
    // 텍스트 컨트롤의 내용을 비웁니다.
    m_textControl->Clear();
 
    // 타이틀에 메시지를 새로 작성합니다.
    wxString titleNames = "No Title";
    titleNames += " - Notepad";
    SetTitle(titleNames);
 
    // 상태 표시줄에 메시지를 표시합니다.
    SetStatusText("New document created.");
}
 
// MyFrame 클래스 내
void MyFrame::OnNewWindow(wxCommandEvent& event)
{
    // 새로운 창을 생성하고 표시
    MyFrame* newFrame = new MyFrame("No Title - Notepad");
    newFrame->Show(true);
}
 
void MyFrame::OnOpen(wxCommandEvent& event)
{
    wxFileDialog openFileDialog(this, _("Open TXT file"), """",
        "TXT files (*.txt)|*.txt", wxFD_OPEN | wxFD_FILE_MUST_EXIST);
 
    wxString openFileName = m_currentFileName;
    wxString openFilePath = m_currentFilePath;
 
    switch (openFileDialog.ShowModal()) {
    case wxID_OK: {
        openFileName = openFileDialog.GetFilename();
        // 세이브 및 적용이 완료했을 경우.
        openFilePath = openFileDialog.GetPath();
        break;
    }
    case wxID_CANCEL: {
        return// 사용자가 취소했을 때
    }
    }
 
    std::ifstream file(openFilePath.ToStdString());
    // 파일을 열고 텍스트 컨트롤에 내용을 로드합니다.
    if (m_textControl->LoadFile(openFilePath)) {
        std::stringstream buffer;
        buffer << file.rdbuf(); // 파일의 내용을 buffer에 읽어 들입니다.
        file.close(); // 파일을 닫습니다.
 
        // textControl의 내용을 갱신합니다.
        m_textControl->SetValue(buffer.str());
        //textControl->SetLabelText(buffer.str());
        m_currentFileName = openFileName;
        m_currentFilePath = openFilePath;
        
        wxString titleNames = m_currentFileName;
        titleNames += " - Notepad";
        // 타이틀을 열린 파일의 이름으로 설정합니다.
        SetTitle(titleNames);
 
    }
    else {
        wxMessageBox("Cannot open File!""Error", wxOK | wxICON_ERROR);
    }
}
 
void MyFrame::OnSaveAs(wxCommandEvent& event)
{
    wxFileDialog saveFileDialog(this, _("Save TXT file"), """",
        "TXT files (*.txt)|*.txt", wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
 
    wxString saveAsFileName;
    wxString saveAsFilePath;
 
    switch (saveFileDialog.ShowModal()) {
    case wxID_OK: {
        saveAsFileName = saveFileDialog.GetFilename();
        saveAsFilePath = saveFileDialog.GetPath();
        break;
    }
    case wxID_CANCEL: {
        return;
    }
    }
 
    // 사용자가 선택한 파일 경로를 저장합니다.
    if (!m_textControl->SaveFile(saveAsFilePath))
    {
        wxMessageBox("Could not save the file!""Error", wxOK | wxICON_ERROR);
        return;
    }
 
    // 파일이 성공적으로 저장되었다면, 현재 파일 경로를 업데이트하고 타이틀을 설정합니다.
    m_currentFileName = saveAsFileName;
    m_currentFilePath = saveAsFilePath;
    wxString titleNames = m_currentFileName;
    SetTitle(titleNames + " - Notepad"); // 타이틀 업데이트
 
    // 상태 표시줄에 메시지를 표시합니다.
    SetStatusText("File saved successfully.");
}
 
void MyFrame::OnSave(wxCommandEvent& event)
{
    wxFileDialog saveFileDialog(this, _("Save TXT file"), """",
        "TXT files (*.txt)|*.txt", wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
 
    wxString saveFileName = m_currentFileName;
    wxString saveFilePath = m_currentFilePath;
 
    // 경로가 비어있는 경우엔.
    if (m_currentFilePath.IsEmpty()) {
        switch (saveFileDialog.ShowModal()) {
        case wxID_CANCEL: {
            return;
        }
 
        case wxID_OK: {
            saveFileName = saveFileDialog.GetFilename();
            // 세이브 및 적용이 완료했을 경우.
            saveFilePath = saveFileDialog.GetPath();
            break;
        }
        }
    }
 
    // 현재 텍스트 컨트롤의 내용을 파일에 저장합니다.
    if (!m_textControl->SaveFile(saveFilePath)) {
        // 현재 텍스트의 내용을 파일에 저장하지 못한다면.
        wxMessageBox("Could Not save the File!""Error", wxOK);
        return;
    }
 
    m_currentFileName = saveFileName;
    m_currentFilePath = saveFilePath;
 
    wxString titleNames = m_currentFileName;
    titleNames += " - Notepad";
    // 타이틀을 열린 파일의 이름으로 설정합니다.
    SetTitle(titleNames);
}
 
void MyFrame::OnUndo(wxCommandEvent& event) {
    if (m_textControl->CanUndo()) {
        m_textControl->Undo();
    }
}
 
void MyFrame::OnRedo(wxCommandEvent& event)
{
    if (m_textControl->CanRedo())
    {
        m_textControl->Redo();
    }
}
 
void MyFrame::OnCut(wxCommandEvent& event) {
    if (m_textControl->CanCut()) {
        m_textControl->Cut();
    }
}
 
void MyFrame::OnCopy(wxCommandEvent& event) {
    if (m_textControl->CanCopy()) {
        m_textControl->Copy();
    }
}
 
void MyFrame::OnSelectAll(wxCommandEvent& event) {
    m_textControl->SelectAll();
}
 
void MyFrame::OnInsertDateTime(wxCommandEvent& event) {
 
    wxDateTime now = wxDateTime::Now();
    wxString dateTimeStr = now.Format();
 
    m_textControl->WriteText(dateTimeStr);
}
 
void MyFrame::OnToggleWordWrap(wxCommandEvent& event)
{
    bool isChecked = m_menuFormat->IsChecked(ID_WordWarp);
    m_textControl->SetWindowStyleFlag(isChecked ? (m_textControl->GetWindowStyleFlag() | wxTE_WORDWRAP) : (m_textControl->GetWindowStyleFlag() & ~wxTE_WORDWRAP));
    m_textControl->Refresh(); // 화면 갱신
}
 
void MyFrame::OnFontSetting(wxCommandEvent& event)
{
    /*dialog = new wxOptionDialog(this, wxID_ANY, "Settings");
    dialog->ShowModal();*/
 
    wxFontData fontData;
    fontData.SetInitialFont(m_textControl->GetFont());
    fontData.SetColour(m_textControl->GetForegroundColour());
 
    wxFontDialog fontDialog(this, fontData);
    if (fontDialog.ShowModal() == wxID_OK)
    {
        wxFontData retData = fontDialog.GetFontData();
        wxFont font = retData.GetChosenFont();
        wxColour colour = retData.GetColour();
 
        m_textControl->SetFont(font);
        m_textControl->SetForegroundColour(colour);
    }
}
 
void MyFrame::OnToggleStatusBar(wxCommandEvent& event)
{
    bool isChecked = m_menuView->IsChecked(ID_StatusBar);
 
    // StatusBar의 보임/숨김 상태를 설정합니다.
    if (m_statusBar) { // m_statusBar가 유효한지 확인합니다.
        m_statusBar->Show(isChecked);
    }
 
    // 프레임의 레이아웃을 갱신하여 m_textControl이 남은 공간을 가득 채우도록 합니다.
    this->Layout();
 
    // 프레임의 크기를 조정하여 내부 컨트롤들이 적절하게 배치되도록 합니다.
    // 이 부분은 필요에 따라 추가하거나 생략할 수 있습니다.
    this->SendSizeEvent();
}
 
// 메뉴바 갱신 이벤트 핸들러
void MyFrame::OnUpdateUndo(wxUpdateUIEvent& event)
{
    event.Enable(m_textControl->CanUndo());
}
 
void MyFrame::OnUpdateRedo(wxUpdateUIEvent& event)
{
    event.Enable(m_textControl->CanRedo());
}
 
void MyFrame::OnUpdateCut(wxUpdateUIEvent& event)
{
    event.Enable(m_textControl->CanCut());
}
 
void MyFrame::OnUpdateCopy(wxUpdateUIEvent& event)
{
    event.Enable(m_textControl->CanCopy());
}
 
// 이벤트 처리 함수 구현
void MyFrame::OnMyCustomEvent(MyCustomEvent& event)
{
    const wxFont info = event.GetwxFont();
 
    int fontSize = info.GetPointSize();
    // 이벤트와 함께 전달된 정보 처리
    m_textControl->SetFont(info);
 
    //textControl->SetFont(font);
    //m_dialog->Destroy(); // dialog를 안전하게 삭제
    //delete m_dialog;
}
cs

 

 

Posted by JunkMam
,

 wxWidgets에서 wxID_CUT과 TextCtrl에서 Cut을 지원해준다.

 

 wxMain.h에서는 이벤트 핸들러를 추가해주고,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 
    // Edit 메뉴에 적용할 이벤트 핸들러
    void OnUndo(wxCommandEvent& event);
    void OnRedo(wxCommandEvent& event);
    void OnCut(wxCommandEvent& event);
    void OnSelectAll(wxCommandEvent& event);
    void OnInsertDateTime(wxCommandEvent& event);
 
 
 
    // 메뉴 동적 활성화 하기 위한 이벤트 핸들러
    void OnUpdateUndo(wxUpdateUIEvent& event);
    void OnUpdateRedo(wxUpdateUIEvent& event);
 
    void OnUpdateCut(wxUpdateUIEvent& event);
cs

 

 

wxMain.cpp에서 하위 메뉴와 이벤트 핸들러를 연결하고, 구현한다.

 

하위 메뉴 추가

1
2
3
4
5
6
7
8
    m_menuEdit = new wxMenu;
    m_menuEdit->Append(wxID_UNDO, "&Undo\tCtrl+Z""Undo the last action");
    m_menuEdit->Append(wxID_REDO, "&Redo\tCtrl+Y""Redo the last undone action");
    m_menuFile->AppendSeparator();
    m_menuEdit->Append(wxID_CUT, "Cu&t\tCtrl+X""Cut the selected text to the clipboard");
    m_menuEdit->Append(ID_SELECT_ALL, "&Select All\tCtrl+A""Select all text");
    m_menuFile->AppendSeparator();
    m_menuEdit->AppendCheckItem(ID_DATETIME, "&DateTime\tF5""DateTime");
cs

 

이벤트 핸들러 연결

1
2
3
4
5
6
7
8
9
10
11
12
    // 메뉴 Edit에 관련된 이벤트 핸들러
    Bind(wxEVT_MENU, &MyFrame::OnUndo, this, wxID_UNDO);
    Bind(wxEVT_MENU, &MyFrame::OnRedo, this, wxID_REDO);
    Bind(wxEVT_MENU, &MyFrame::OnCut, this, wxID_CUT);
    Bind(wxEVT_MENU, &MyFrame::OnSelectAll, this, ID_DATETIME);
    Bind(wxEVT_MENU, &MyFrame::OnInsertDateTime, this, ID_DATETIME);
 
    // 메뉴바 갱신 이벤트 핸들러
    Bind(wxEVT_UPDATE_UI, &MyFrame::OnUpdateUndo, this, wxID_UNDO);
    Bind(wxEVT_UPDATE_UI, &MyFrame::OnUpdateRedo, this, wxID_REDO);
 
    Bind(wxEVT_UPDATE_UI, &MyFrame::OnUpdateCut, this, wxID_CUT);
cs

 

이벤트 핸들러 구현

1
2
3
4
5
6
7
8
void MyFrame::OnCut(wxCommandEvent& event) {
    m_textControl->Cut();
}
 
void MyFrame::OnUpdateCut(wxUpdateUIEvent& event)
{
    event.Enable(m_textControl->CanCut());
}
cs

 

이렇게 해서 TextCtrl에서 잘라내는 기능을 구현할 수 있다.

 

 

 

Posted by JunkMam
,

 wxWidgets의 textCtrl에 실행 취소(Undo)와 같이 다시 실행(Redo)가 존재한다.

이걸 이용해서 구현이 가능하다.

 

Redo를 하기 위한 이벤트 핸들러를 추가하자.

 wxMain.h

1
2
3
4
5
    // Edit 메뉴에 적용할 이벤트 핸들러
    void OnUndo(wxCommandEvent& event);
    void OnRedo(wxCommandEvent& event);
    void OnSelectAll(wxCommandEvent& event);
    void OnInsertDateTime(wxCommandEvent& event);
cs

 

Undo와 같이 Redo가 가능할 경우에 활성화를 할수 있게. 선언하자.

 wxMain.h

1
2
3
    // 메뉴 동적 활성화 하기 위한 이벤트 핸들러
    void OnUpdateUndo(wxUpdateUIEvent& event);
    void OnUpdateRedo(wxUpdateUIEvent& event);
cs

 

이제 wxMain.cpp에 작업을 하자.

먼저 메뉴를 추가하자.

wxMain.cpp

1
2
3
4
5
6
7
    m_menuEdit = new wxMenu;
    m_menuEdit->Append(wxID_UNDO, "&Undo\tCtrl+Z""Undo the last action");
    m_menuEdit->Append(wxID_REDO, "&Redo\tCtrl+Y""Redo the last undone action");
    m_menuFile->AppendSeparator();
    m_menuEdit->Append(ID_SELECT_ALL, "&Select All\tCtrl+A""Select all text");
    m_menuFile->AppendSeparator();
    m_menuEdit->AppendCheckItem(ID_DATETIME, "&DateTime\tF5""DateTime");
cs

 

정의 및 이벤트 핸들러를 연결한다.

wxMain.cpp

1
2
3
4
5
6
7
8
9
10
    // 메뉴 Edit에 관련된 이벤트 핸들러
    Bind(wxEVT_MENU, &MyFrame::OnUndo, this, wxID_UNDO);
    Bind(wxEVT_MENU, &MyFrame::OnRedo, this, wxID_REDO);
    Bind(wxEVT_MENU, &MyFrame::OnInsertDateTime, this, ID_DATETIME);
    Bind(wxEVT_MENU, &MyFrame::OnSelectAll, this, ID_DATETIME);
 
 
    // 메뉴바 갱신 이벤트 핸들러
    Bind(wxEVT_UPDATE_UI, &MyFrame::OnUpdateUndo, this, wxID_UNDO);
    Bind(wxEVT_UPDATE_UI, &MyFrame::OnUpdateRedo, this, wxID_REDO);
cs

 

메뉴와 메뉴바 갱신에 관련된 이벤트 핸들러를 정의한다.

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
void MyFrame::OnUndo(wxCommandEvent& event) {
    if (m_textControl->CanUndo()) {
        m_textControl->Undo();
    }
}
 
void MyFrame::OnRedo(wxCommandEvent& event)
{
    if (m_textControl->CanRedo())
    {
        m_textControl->Redo();
    }
}
 
// 메뉴바 갱신 이벤트 핸들러
void MyFrame::OnUpdateUndo(wxUpdateUIEvent& event)
{
    event.Enable(m_textControl->CanUndo());
}
 
// 메뉴바 갱신 이벤트 핸들러
void MyFrame::OnUpdateRedo(wxUpdateUIEvent& event)
{
    event.Enable(m_textControl->CanRedo());
}
cs

 

이렇게하면, 다시 시작을 구현할 수 있다.

 

Posted by JunkMam
,

 C에서는 수를 정의 하거나, 값을 저장할 수 있는 기능을 제공한다.

 C에서는 수를 정의하는 것으로는 매크로라는 것으로

 '#define'을 사용한다.

1
2
3
 
#define PI 3.14
#define POW(x) ((x)*(x))
cs

 

이렇게 사용이 가능하다.

PI는 3.14라는 상수를 정의해주고, POW라는 것은 제곱을 표현하게 된다.

 

매크로는 특정 문자을 복사해서 덮어 씌우는 것으로 다음과 적용하는 것과 동일하게 할 수 있다.

 

1
2
3
4
5
6
7
8
#define PI 3.14
 
int main(int argc, char** argv){
    3.14+1;
    PI+1;
 
    return 0;
}
cs

 

3.14+1과 PI+1은 동일한 결과물을 가진다.

그렇다면, 매크로를 사용하는 이유가 무엇일까?

예을 들어서 (x+1)(x+2)...(x+100)이런식의 글을 작성되어 있다고 한다면,

x = 1일때, (1+1)(1+2)...(1+100)이렇게 가능하듯이.

#define X라는 것을 정의 해놓는다고 한다면,

(X+1)(X+2)...(X+100)이렇게 되었을때,

#define X 1

 

이렇게 하면, X=1일때 이런식으로 선언해서 사용할 수 있게 되는 것이다.

즉, 반복적으로 사용될 '상수'를 정의내려서 적용할 수 있다.

반대로 특정 연산을 정의 내려서 처리할 수 있다.

위에 작성한

#define POW(x)가 그것이다.

여기서 POW(x)는 '매크로 함수'라고 칭하며, 함수처럼 x라는 매개 변수를 받아서 '연산'을 복사한다.

 

단순한 연산이지만 자주 사용될 '연산'은 '매크로 함수'를 사용해서 컴파일러가 자동으로 복사 할 수 있다.

 

C는 계산한 결과를 저장하는 기능도 제공해준다.

이걸 '변수'라고 칭하며, 종류를 정해져있다.

C에서 기본적으로 제공해주는 '변수'의 종류는 다음과 같다.

 

변수 유형 설명 범위 예시
char 단일 문자를 저장하거나, 정수형으로 데이터 최소 단위이다. -128 ~ 127 char ch = 'A';
char baseNum = 200;
int 정수를 저장하는 기본 데이터 유형 Bit에 따라서 범위가 달라짐.
16Bit: -32768 ~ 32767
32Bit: -2147483648 ~ 2147483647
int num = 100;
flaot 실수를 저장하는데 사용되며, 단정밀도 부동 소수점 수를 나타낸다. 1.175494351 E - 38 ~ 3.402823466 E + 38 float realNum = 3.14f;
double 실수를 저장하는데 사용되며, 배정밀도 부동 소수점 수를 나타낸다. 2.2250738585072014 E - 308 ~ 1.7976931348623158 E + 308 double realNums = 3.14;
short 정수를 저장하는 데이터 유형. int보다 적은 범위를 저장한다.
16bit일때랑 똑같다.
-32768 ~ 32767 short num = 100;
long 정수를 저장하는 데이터 유형.
int보다 큰 범위를 저장한다.
64bit이상일때 사용됨.
-2,147,483,648 ~ 2,147,483,647 long num = 100;

 

 

여기서 접두사가 무엇이 붙냐에 따라서 변수의 상태가 달라지는 경우가 있다.

예을 들어서 unsigned가 있다.

unsigned란, 부호가 존재하지 않는 것으로 변수 유형에서 범위를 무조건 상수로 적용된 상태를 뜻한다

unsigned char라면, 0~255까지 지원하는 것 처럼 적용이 된다.

 

C와 C++같은 언어들은 컴파일러마다 다르지만, 실수랑 정수를 그냥 연산할 경우 정수 기준으로 계산이 되어서 결과를 저장하는 경우가 있다.

 

예을 들어서 int a = 1; float b = 3.14f;라는게 있다면,

a+b를 하면, 4가 결과물로 저장하게 된다.

이 경우 (a*1.0)+b을 사용해줘야한다.

 

예을 들어서 다음과 같이 적용했는데. 소수점이 0이 된다면, 컴파일러와 CPU가 그것을 지원해주지 않는 경우라고 이해하자.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>
 
#define PI 3.14
 
int main() {
    int a = 1;
    float b = PI;
 
    float c = a+b;
    float d = (a*1.0)+b;
 
    printf("%f\n", c);
    printf("%f\n", d);
 
    return 0;
}
 
cs

 

'프로그래밍 > C 언어' 카테고리의 다른 글

[C] 제어(1) - 조건문 -  (0) 2024.02.17
[C] 연산자  (0) 2024.02.16
[C] 기본적으로 지원해주는 출력  (0) 2024.02.14
[C] 기본 구조  (1) 2024.02.13
Posted by JunkMam
,