본문 바로가기
C++ 200제/코딩 IT 정보

C# 메모장 프로그램 (윈도우 notepad 소스 코드)

by vicddory 2017. 6. 12.

C# 메모장 프로그램 (윈도우 notepad 소스 코드)


C# 메모장


소스 코드 - memo.zip


C# 메모장 프로그램 (윈도우 notepad 소스 코드)[C# 메모장 프로그램 (윈도우 notepad 소스 코드)]


기능별 구현은 아래 소스 보면서 참조하세요.


1. using


1
2
3
4
5
6
7
8
9
10
11
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Printing;
using System.Collections;
using Microsoft.Win32;
cs


2. 변수


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// 폼 파일 객체
Go GoForm;
Find FindForm;
Change ChangeForm;
 
// 전역 변수
public TextBox tb;
public int txtLine = 1;
public int moveLine = 0;
public char[] txt = null;
public int cLength = 0;
public string search = null;
 
private Font printFont;
private string streamToPrint = null;
private bool SaveCheck = false;
private bool EditText = false;
cs

3. FormClosing 이벤트


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
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
    if (EditText == false)
    {
        e.Cancel = true;
        return;
    }
    else if (EditText == true)
    {
 
    }
 
    if (EditText == false)
    {
        if (MessageBox.Show("저장""나가기", MessageBoxButtons.YesNoCancel)
                        == DialogResult.Yes)
        {
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                SaveYorN();
            }
            else if (saveFileDialog1.ShowDialog() == DialogResult.No)
            {
                this.Close();
            }
        }
    }
}
cs


4. TextChanged 이벤트


1
2
3
4
5
// TextBox 메인 텍스트 박스 RichTextBox
public void TextBox_TextChanged_1(object sender, EventArgs e)
{
    EditText = true;
}
cs


C# 메모장 프로그램 (윈도우 notepad 소스 코드)[C# 메모장 프로그램 (윈도우 notepad 소스 코드)]


5. File 이벤트, 열기, 저장, 다른 이름으로 저장, 페이지 설정, 인쇄, 끝내기


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
private void menu_NewFile_Click(object sender, EventArgs e)
{
    try
    {
        if (TextBox.Text != "")
        {
            if (MessageBox.Show(this"작업중인 문서 저장?""저장",
                              MessageBoxButtons.YesNoCancel) == DialogResult.Yes)
            {
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    SaveDocument(saveFileDialog1.FileName);
                }
            }
        }
    }
 
    catch (Exception Newe)
    {
        MessageBox.Show("에러 : " + Newe.Message, "Error");
    }
 
    finally
    {
        TextBox.Clear();
    }
 
    SaveCheck = false;
    EditText = false;
}
 
// 파일 - 열기
private void menu_OpenFile_Click(object sender, EventArgs e)
{
    try
    {
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
            OpenDocument(openFileDialog1.FileName);
    }
    catch (Exception Opene)
    {
        MessageBox.Show("에러 : " + Opene.Message, "Error");
    }
}
 
// 파일 - 저장
private void menu_SaveFile_Click(object sender, EventArgs e)
{
    SaveYorN();
}
 
// 파일 - 다른 이름으로 저장
private void menu_AFile_Click(object sender, EventArgs e)
{
    SaveYorN();
}
 
// 파일 - 페이지 설정
private void menu_PageFile_Click(object sender, EventArgs e)
{
    PrintDocument pd = new PrintDocument();
    pd.DocumentName = TextBox.Text;
    pageSetupDialog1.Document = pd;
    pageSetupDialog1.ShowDialog();
}
 
// 파일 - 인쇄
private void menu_PrintFile_Click(object sender, EventArgs epp)
{
    printFont = new Font("Arial"10);
    PrintDocument pd = new PrintDocument();
    pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage_1);
    pd.Print();
}
 
// 파일 - 끝내기
private void menu_EndFile_Click(object sender, EventArgs e)
{
    Application.Exit();
}
cs

6. 편집 기능


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
// 편집 - 찾기
private void menu_FindEdit_Click(object sender, EventArgs e)
{
    FindForm = new Find(TextBox);
    FindForm.Show(this);
}
 
// 편집 - 다음 찾기
private void menu_NextEdit_Click(object sender, EventArgs e)
{
 
}
 
// 편집 - 바꾸기
private void menu_ReEdit_Click(object sender, EventArgs e)
{
    ChangeForm = new Change(TextBox);
    ChangeForm.Show();
}
 
// 편집 - 이동
private void menu_GoEdit_Click(object sender, EventArgs e)
{
    GoForm = new Go(this);
    DialogResult result = GoForm.ShowDialog();
 
    moveLine = moveLine - 1;
    int caretLine = 0;
 
    int caretPos = TextBox.TextLength;//텍스트박스의 전체 문자열 길이를 반환
    char[] charText = TextBox.Text.ToCharArray();//전체 문자를 유니코드 배열에 복사
    for (int i = 0; i < caretPos; i++)//처음부터 문자열 끝까지 검사
    {
        if (charText[i].ToString().Equals("\n"))
        {
            caretLine++;
            if (caretLine == (moveLine))//이동할 라인과 같은 라인이 나오면...
            {
                //케럿의 위치를 반복한 위치(i)로 이동시킨 후 반복을 탈출
                TextBox.SelectionStart = i + 1;
                break;
            }
        }
    }
}
 
// 편집 - 모두선택
private void menu_AllEdit_Click(object sender, EventArgs e)
{
    TextBox.SelectAll();
}
 
// 편집 - 시간 / 날짜
private void menu_DateEdit_Click(object sender, EventArgs e)
{
    TextBox.Text = TextBox.Text.Insert(TextBox.SelectionStart, DateTime.Now.ToString());
}
cs


c# 윈도우 메모장 notepad[C# 메모장 프로그램 (윈도우 notepad 소스 코드)]


7. 글꼴 관리


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// 서식 - 자동 줄 바꿈
private void menu_WOri_Click(object sender, EventArgs e)
{
    TextBox.WordWrap = TextBox.WordWrap ? false : true;
}
 
// 서식 - 글꼴
private void menu_FOri_Click(object sender, EventArgs e)
{
    fontDialog1.ShowDialog();
    TextBox.Font = fontDialog1.Font;
}
 
// 서식 - 색상
private void menu_ColorOri_Click(object sender, EventArgs e)
{
    colorDialog1.ShowDialog();
    TextBox.SelectionColor = colorDialog1.Color;
}
cs


8. 메뉴 - 보기


1
2
3
4
5
6
7
8
9
10
11
// 보기 - 상태 표시줄
private void menu_StatusView_Click(object sender, EventArgs e)
{
    statusStrip1.Visible = statusStrip1.Visible ? false : true;
}
 
// 보기 - 실행 아이콘
private void menu_PlayView_Click(object sender, EventArgs e)
{
    toolStrip1.Visible = toolStrip1.Visible ? false : true;
}
cs


9. 메뉴 - 도움말


1
2
3
4
5
6
7
8
9
10
11
12
// 도움말 - 도움말 항목
private void menu_HangHelp_Click(object sender, EventArgs e)
{
    HangHelp hh = new HangHelp();
    hh.Show();
}
 
// 도움말 - 메모장 정보
private void menu_JungHelp_Click(object sender, EventArgs e)
{
 
}
cs

10. 실행 아이콘


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
// 아이콘 - 새로만들기
private void 새로만들기NToolStripButton_Click(object sender, EventArgs e)
{
    try
    {
        if (TextBox.Text != "")
        {
            if (MessageBox.Show(this"작업중인 문서 저장?""저장",
                             MessageBoxButtons.YesNoCancel) == DialogResult.Yes)
            {
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    SaveDocument(saveFileDialog1.FileName);
                }
            }
        }
    }
 
    catch (Exception Newe)
    {
        MessageBox.Show("에러 : " + Newe.Message, "Error");
    }
 
    finally
    {
        TextBox.Clear();
    }
}
 
// 아이콘 - 열기
private void 열기OToolStripButton_Click(object sender, EventArgs e)
{
    try
    {
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
            OpenDocument(openFileDialog1.FileName);
    }
    catch (Exception Opene)
    {
        MessageBox.Show("에러 : " + Opene.Message, "Error");
    }
}
 
// 아이콘 - 저장
private void 저장SToolStripButton_Click(object sender, EventArgs e)
{
    SaveYorN();
}
 
// 아이콘 - 인쇄
private void 인쇄PToolStripButton_Click(object sender, EventArgs e)
{
    printFont = new Font("Arial"10);
    PrintDocument pd = new PrintDocument();
    pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage_1);
    pd.Print();
}
 
// 아이콘 - 왼쪽 정렬
private void AlignLeft_Click(object sender, EventArgs e)
{
    TextBox.SelectionAlignment = HorizontalAlignment.Left;
}
 
// 아이콘 - 가운데 정렬
private void AlignCenter_Click(object sender, EventArgs e)
{
    TextBox.SelectionAlignment = HorizontalAlignment.Center;
}
 
// 아이콘 - 오른쪽 정렬
private void AlignRight_Click(object sender, EventArgs e)
{
    TextBox.SelectionAlignment = HorizontalAlignment.Right;
}
 
// 아이콘 - 잘라내기
private void 잘라내기UToolStripButton_Click(object sender, EventArgs e)
{
    TextBox.Cut();
}
 
// 아이콘 - 복사
private void 복사CToolStripButton_Click(object sender, EventArgs e)
{
    TextBox.Copy();
}
 
// 아이콘 붙여넣기
private void 붙여넣기PToolStripButton_Click(object sender, EventArgs e)
{
    TextBox.Paste();
}
 
// 아이콘 - 도움말
private void 도움말LToolStripButton_Click(object sender, EventArgs e)
{
 
}
cs


11. 파일 - 저장


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
// 파일 저장
private bool SaveYorN()
{
    try
    {
        if (SaveCheck == false)
        {
            if (MessageBox.Show("저장""메모장", MessageBoxButtons.YesNo)
                                          == DialogResult.Yes)
            {
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    SaveDocument(saveFileDialog1.FileName);
                    SaveCheck = true;
                    EditText = false;
                }
            }
        }
 
    }
    catch (Exception e)
    {
        MessageBox.Show("bool SaveYorn Error" + e.Source);
    }
    finally
    {
        TextBox.SelectionStart = 0;
    }
 
    return false;
}
 
// 파일 저장 문서
public void SaveDocument(string FileName)
{
    StreamWriter SaveText = new StreamWriter(FileName, falseSystem.Text.Encoding.Default);
    SaveText.Write(TextBox.Text);
    SaveText.Close();
}
cs


12. 문서 열기


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// 문서 열기
public void OpenDocument(string FileName)
{
    try
    {
        StreamReader sr = new StreamReader(FileName, System.Text.Encoding.Default);
        TextBox.Text = sr.ReadToEnd();
        TextBox.SelectionStart = 0;
        sr.Close();
        SaveCheck = false;
    }
    catch (Exception e)
    {
        MessageBox.Show("OpenDocument에서 에러 발생\n"
                                             + e.Message + "\n" + e.Source);
    }
}
cs


13. 인쇄


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
// 인쇄
private void pd_PrintPage_1(object sender, PrintPageEventArgs ev)
{
    streamToPrint = TextBox.Text;
    float linesPerPage = 0;
    float yPos = 0;
    int count = 0;
    float leftMargin = ev.MarginBounds.Left;
    float topMargin = ev.MarginBounds.Top;
    string line = null;
 
    if (streamToPrint == null) { MessageBox.Show("아하하하하하하"); }
    MessageBox.Show(streamToPrint);
 
    linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics);
    try
    {
        yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));
        ev.Graphics.DrawString(streamToPrint, printFont, Brushes.Black,
            leftMargin, yPos, new StringFormat());
    }
    catch (Exception epe)
    {
        MessageBox.Show(epe.Message + "\n" + epe.InnerException);
    }
 
    if (line != null)
        ev.HasMorePages = true;
    else
        ev.HasMorePages = false;
}
cs


C# 메모장 프로그램 (윈도우 notepad 소스 코드)

댓글