간단한 워드패드.MDI 응용

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.Win32;


namespace fontchange
{
    public partial class Form1 : Form
    {       
        public Form1()
        {
            InitializeComponent();

            LoadSetting(); //레지스트리 읽어오기
        }

        private void Form1_Closing(Object sender, System.ComponentModel.CancelEventArgs e)
        {
            SaveSetting(); //레지스트리 저장
        }

        private void SaveSetting()
        {
            try
            {
                // 레지스트리 열기 
                RegistryKey rk = Registry.LocalMachine.OpenSubKey("Software", true);
                RegistryKey newkey = rk.CreateSubKey("NotePad");

                // 글꼴 스타일 저장
                newkey.SetValue("FontName", richTextBox1.Font.FontFamily.GetName(0));
                newkey.SetValue("FontSize", Convert.ToString(richTextBox1.Font.Size));

                // 글자색과 바탕색 저장
                newkey.SetValue("ForeColor", Convert.ToString(richTextBox1.ForeColor.ToArgb()));
                newkey.SetValue("BackColor", Convert.ToString(richTextBox1.BackColor.ToArgb()));
            }
            catch
            {
                MessageBox.Show("레지스트리 저장 중에 오류가 발생했습니다.", "오류 발생");
            }       
        }

        private void LoadSetting()
        {
         try
       {
             // 레지스트리 열기 
             RegistryKey subkey = Registry.LocalMachine.OpenSubKey( "Software\\NotePad" ) ;
             // 글꼴 스타일 읽어오기  
            string fname = subkey.GetValue( "FontName").ToString() ;
            Single fsize  = Convert.ToSingle( subkey.GetValue( "FontSize" ) );
            // Font 객체 생성 및 할당
            richTextBox1.Font = new System.Drawing.Font( fname, fsize );
             int color;
            // 글자색 읽어오기
              color = Convert.ToInt32( subkey.GetValue("ForeColor" ) );
              richTextBox1.ForeColor = System.Drawing.Color.FromArgb( color );
            // 바탕색 읽어오기
             color = Convert.ToInt32( subkey.GetValue("BackColor") );
             richTextBox1.BackColor = System.Drawing.Color.FromArgb( color );
         }
          catch
           {
                MessageBox.Show( "레지스트리 읽는 중에 오류가 발생했습니다.", "오류 발생" );
           }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (fontDialog1.ShowDialog() == DialogResult.OK)
            {
                richTextBox1.Font = fontDialog1.Font;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                richTextBox1.BackColor = colorDialog1.Color;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                richTextBox1.ForeColor = colorDialog1.Color;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {           
            OpenFileDialog oFD = new OpenFileDialog();
            oFD.FileName = "";
            oFD.Filter = "Text Files (.txt)|*.txt|All Files (*.*)|*.*|유난님 파일(.yunhwan)|*.yunhwan|RFT Files(*.rtf)|*.rtf";
            // text files(.txt)|*.txt < 이건, 위 설명 부분에 나오는 게 "text files(.txt)" 이거고
            // 아래에 *.txt 라고 나오는 게 아래 실제 파일 읽어들이는 곳에 붙는 설명.
            oFD.InitialDirectory = "C:\\";
            oFD.FilterIndex = 1;

            if (oFD.ShowDialog() == DialogResult.OK)
            {
                Stream strm = oFD.OpenFile();
                StreamReader reader = new StreamReader(strm);
                //텍스트 내용 읽어오기
                richTextBox1.Text = reader.ReadToEnd();
                //스트림 닫기
                reader.Close();
                strm.Close();
                this.Text = oFD.FileName;
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {           
            SaveFileDialog sf = new SaveFileDialog();
            sf.Title = "파일 저장";
            sf.Filter = "모든 파일 (*.*)|*.*|텍스트 (*.txt)|*.txt";
            sf.FilterIndex = 2;
            sf.InitialDirectory = "c:\\";
            sf.DefaultExt = "txt";
            sf.FileName = "noname";
            if (sf.ShowDialog() == DialogResult.OK)
            {
                string str = sf.FileName;
                StreamWriter stw = new StreamWriter(str);
                stw.Write(richTextBox1.Text);
                stw.Close();
            }
        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
           
        }

        private void 폴더보기ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog oFD = new OpenFileDialog();
            oFD.FileName = "dkdkddk";
            oFD.Filter = "Text Files (.txt)|*.txt|All Files (*.*)|*.*|유난님 파일(.yunhwan)|*.yunhwan";
            // text files(.txt)|*.txt < 이건, 위 설명 부분에 나오는 게 "text files(.txt)" 이거고
            // 아래에 *.txt 라고 나오는 게 아래 실제 파일 읽어들이는 곳에 붙는 설명.
            oFD.InitialDirectory = "C:\\";
            oFD.FilterIndex = 1;

            if (oFD.ShowDialog() == DialogResult.OK)
            {
                MessageBox.Show(oFD.FileName);
            }
        }

        private void 폴더지정ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveFileDialog1.Title = "파일 저장하기";
            saveFileDialog1.Filter = "모든 파일(*.*)|*.*|텍스트 파일(*.txt)|*.txt";
            saveFileDialog1.FilterIndex = 2;
            saveFileDialog1.InitialDirectory = "D:\\";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string str = saveFileDialog1.FileName;
                StreamWriter stw = new StreamWriter(str);
                stw.Write(richTextBox1.Text);
                stw.Close();
                this.Text = str;
            }
        }

        private void 글꼴바꾸기ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (fontDialog1.ShowDialog() == DialogResult.OK)
            {
                richTextBox1.Font = fontDialog1.Font;
            }
        }

        private void 바탕색바꾸기ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                richTextBox1.BackColor = colorDialog1.Color;
            }
        }

        private void 전경색바꾸기ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                richTextBox1.ForeColor = colorDialog1.Color;
            }
        }

        private void 끝내기ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveFileDialog1.Title = "파일 저장하기";
            saveFileDialog1.Filter = "모든 파일(*.*)|*.*|텍스트 파일(*.txt)|*.txt";
            saveFileDialog1.FilterIndex = 2;
            saveFileDialog1.InitialDirectory = "D:\\";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string str = saveFileDialog1.FileName;
                StreamWriter stw = new StreamWriter(str);
                stw.Write(richTextBox1.Text);
                stw.Close();
                this.Text = str;
            }
        }

        private void 끝내기ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
           
        }

        private void 끝내기ToolStripMenuItem2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void 모두선ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.SelectAll();
        }

        private void 모두삭제ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.DeselectAll();
        }

        private void 저장하기ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            saveFileDialog1.Title = "파일 저장하기";
            saveFileDialog1.Filter = "모든 파일(*.*)|*.*|텍스트 파일(*.txt)|*.txt";
            saveFileDialog1.FilterIndex = 2;
            saveFileDialog1.InitialDirectory = "D:\\";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string str = saveFileDialog1.FileName;
                StreamWriter stw = new StreamWriter(str);
                stw.Write(richTextBox1.Text);
                stw.Close();
                this.Text = str;
            }
        }

        private void 새이름으로ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveFileDialog1.Title = "파일 저장하기";
            saveFileDialog1.Filter = "모든 파일(*.*)|*.*|텍스트 파일(*.txt)|*.txt";
            saveFileDialog1.FilterIndex = 2;
            saveFileDialog1.InitialDirectory = "D:\\";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string str = saveFileDialog1.FileName;
                StreamWriter stw = new StreamWriter(str);
                stw.Write(richTextBox1.Text);
                stw.Close();
                this.Text = str;
            }
        }       

        private void 끝내기ToolStripMenuItem1_Click_1(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void toolStripMenuItem5_Click(object sender, EventArgs e)
        {
            richTextBox1.Cut();
        }

        private void toolStripMenuItem4_Click(object sender, EventArgs e)
        {
            richTextBox1.Paste();
        }

        private void toolStripMenuItem7_Click(object sender, EventArgs e)
        {
            if (richTextBox1.WordWrap)
            {
                richTextBox1.WordWrap = false;
                toolStripMenuItem7.Checked = false;
            }
            else
            {
                richTextBox1.WordWrap = true;
                toolStripMenuItem7.Checked = true;
            }
        }

        private void 전체선택ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.SelectAll();
        }

        private void 선택취소ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.DeselectAll();
        }

        private void 선택복사ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Copy();
        }

        private void 잘라내기ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Cut();
        }

        private void 붙여넣기ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Paste();
        }

        private void toolStripMenuItem9_Click(object sender, EventArgs e)
        {
            OpenFileDialog oFD = new OpenFileDialog();
            oFD.FileName = "";
            oFD.Filter = "Text Files (.txt)|*.txt|All Files (*.*)|*.*|유난님 파일(.yunhwan)|*.yunhwan|RFT Files(*.rtf)|*.rtf";
            // text files(.txt)|*.txt < 이건, 위 설명 부분에 나오는 게 "text files(.txt)" 이거고
            // 아래에 *.txt 라고 나오는 게 아래 실제 파일 읽어들이는 곳에 붙는 설명.
            oFD.InitialDirectory = "C:\\";
            oFD.FilterIndex = 1;

            if (oFD.ShowDialog() == DialogResult.OK)
            {
                Stream strm = oFD.OpenFile();
                StreamReader reader = new StreamReader(strm);
                //텍스트 내용 읽어오기
                richTextBox1.Text = reader.ReadToEnd();
                //스트림 닫기
                reader.Close();
                strm.Close();
                this.Text = oFD.FileName;
            }
        }
    }
}
redraw.zip

by 李君 | 2009/06/16 19:59 | 즐거운 것 | 트랙백 | 덧글(0)

트랙백 주소 : http://haken.egloos.com/tb/4167035
☞ 내 이글루에 이 글과 관련된 글 쓰기 (트랙백 보내기) [도움말]

:         :

:

비공개 덧글

◀ 이전 페이지          다음 페이지 ▶