목표 |
richTextBox 사용법 및 FontDialog 사용법 등을 배워보자.
컴포넌트 설명 |
RichTextBox : 글꼴 등을 적용 시킬 수 있는 텍스트박스
FontDialog : 글꼴을 선택할 수 있는 컴포넌트
ColorDialog : 색상을 선택할 수 있는 컴포넌트
MenuStrip : 메뉴바를 만들수 있다
StatusStrip : 상태를 표시할 수 있는 바
폼만들기 |
1. MenuStrip을 폼에 추가 후 메뉴 생성
2. RichTextBox 를 폼에 추가후 Dock 를 Fill 로 설정하여 폼을 채움
소스코드 구현 |
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace NotePad
{
public partial class Form1 : Form
{
string OpenFileName;
bool TextChange;
private PageSettings pageSettings = new PageSettings(); // 수정할 페이지 설정을 나타내는 값을 가져오거나 설정
private PrinterSettings printerSettings = new PrinterSettings();
public Form1()
{
InitializeComponent();
OpenFileName = "";
TextChange = false; ///TextChang가 변경 된것이 없다.
}
private void 새로만들기ToolStripMenuItem_Click(object sender, EventArgs e)
{
if(TextChange) /// 텍스트가 변경이 되었다면
{
if(MessageBox.Show("변경 내용을 저장하시겠습니까","저장",MessageBoxButtons.YesNo)==DialogResult.Yes)
{
if(OpenFileName=="")
{
saveFileDialog1.Filter = "텍스트파일|*.txt";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
}
}
else richTextBox1.SaveFile(OpenFileName,RichTextBoxStreamType.PlainText);
}
}
richTextBox1.Text = "";
OpenFileName = "";
TextChange = false;
}
private void 열기ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (TextChange) /// 텍스트가 변경이 되었다면
{
DialogResult rs = MessageBox.Show("변경 내용을 저장하시겠습니까", "저장", MessageBoxButtons.YesNoCancel);
if (rs == DialogResult.Yes)
{
if (OpenFileName == "")
{
saveFileDialog1.Filter = "텍스트파일|*.txt";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.RichText);
}
}
else richTextBox1.SaveFile(OpenFileName, RichTextBoxStreamType.RichText);
}
else if (rs == DialogResult.Cancel) return;
}
openFileDialog1.Filter = "텍스트파일|*.txt";
if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
OpenFileName = openFileDialog1.FileName;
richTextBox1.LoadFile(OpenFileName, RichTextBoxStreamType.RichText);
}
TextChange = false;
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
TextChange = true; ///텍스트 박스의 값이 변경이 되면 나중에 저장 여부를 물어 본다.
}
private void 저장ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (OpenFileName == "")
{
saveFileDialog1.Filter = "텍스트파일|*.txt";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
OpenFileName = saveFileDialog1.FileName;
richTextBox1.SaveFile(OpenFileName, RichTextBoxStreamType.RichText);
}
}
else richTextBox1.SaveFile(OpenFileName, RichTextBoxStreamType.RichText);
TextChange = false; ///저장했으면 TextChang를 False로 변경
}
private void 다른이름으로저장ToolStripMenuItem_Click(object sender, EventArgs e)
{
saveFileDialog1.Filter = "텍스트파일|*.txt";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
OpenFileName = saveFileDialog1.FileName;
richTextBox1.SaveFile(OpenFileName, RichTextBoxStreamType.RichText);
}
TextChange = false;
}
private void 페이지설정ToolStripMenuItem_Click(object sender, EventArgs e)
{
PageSetupDialog ps = new PageSetupDialog();
ps.PageSettings = pageSettings;
ps.PrinterSettings = printerSettings;
ps.AllowPrinter = true; //프린터 버튼 사용
ps.AllowOrientation = true; // 대화장자의 방향
ps.ShowDialog();
}
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
String text = richTextBox1.Text;
Font textFont = new Font(richTextBox1.Font.Name, richTextBox1.Font.Size); //폰트를 메모장 폰트로 설정
int leftMargin = e.MarginBounds.Left;
int topMargin = e.MarginBounds.Top;
e.Graphics.DrawString(text, textFont, Brushes.Black, leftMargin, topMargin);
}
private void 인쇄ToolStripMenuItem_Click(object sender, EventArgs e)
{
printDocument1.PrinterSettings = printerSettings;
printDocument1.DefaultPageSettings = pageSettings;
PrintDialog pd = new PrintDialog();
pd.Document = printDocument1;
if(pd.ShowDialog()==DialogResult.OK)
{
printDocument1.Print(); //인쇄 시작
}
}
private void 끝내기ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (TextChange) /// 텍스트가 변경이 되었다면
{
DialogResult rs = MessageBox.Show("변경 내용을 저장하시겠습니까", "저장", MessageBoxButtons.YesNoCancel);
if (rs == DialogResult.Yes)
{
if (OpenFileName == "")
{
saveFileDialog1.Filter = "텍스트파일|*.txt";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.RichText);
}
}
else richTextBox1.SaveFile(OpenFileName, RichTextBoxStreamType.RichText);
}
else if (rs == DialogResult.Cancel) return;
}
Application.Exit();
}
private void 잘라내기ToolStripMenuItem1_Click(object sender, EventArgs e)
{
richTextBox1.Cut();
}
private void 복사ToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Copy();
}
private void 붙여넣기ToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Paste();
}
private void 글꼴ToolStripMenuItem1_Click(object sender, EventArgs e)
{
FontDialog fd = new FontDialog();
fd.Font = richTextBox1.SelectionFont;
if(fd.ShowDialog()==DialogResult.OK)
{
richTextBox1.SelectionFont = fd.Font;
}
}
private void 색상ToolStripMenuItem1_Click(object sender, EventArgs e)
{
ColorDialog cd = new ColorDialog();
cd.Color = richTextBox1.SelectionColor;
if (cd.ShowDialog() == DialogResult.OK)
{
richTextBox1.SelectionColor = cd.Color;
}
}
}
}
저장 타입을 RichText 타입으로 저장시 색상이 모두 저장 되지만 일반 메모장에서는 다른 속성값과 같이 조회 된다.
활용 |
워드와 같은 글꼴 속성을 변경할 수 있는 텍스트 박스를 사용할때 RichText를 활용하여 다양한 용도로 사용이 가능하다.
사업자 정보 표시
원당컴퓨터학원 | 기희경 | 인천 서구 당하동 1028-2 장원프라자 502호 | 사업자 등록번호 : 301-96-83080 | TEL : 032-565-5497 | Mail : icon001@naver.com | 통신판매신고번호 : 호 | 사이버몰의 이용약관 바로가기
'강의자료 > C#' 카테고리의 다른 글
[C#] WebClient를 이용하여 웹다운로드 기능 구현 (0) | 2020.12.16 |
---|---|
[C#] WebBrowser를 이용해서 간단한 웹브라우저를 만들어 보자 (0) | 2020.12.16 |
[C#] 트레이 아이콘을 사용하여 프로그램을 숨겨보자 (0) | 2020.12.11 |
[C#] 프로그래스바를 활용한 진행 상태 표현 (0) | 2020.12.11 |
[C#]PicturBox,ImageList 를 이용한 앨범 구현 (0) | 2020.12.10 |