728x90
반응형
728x170
■ RichTextBox 클래스를 사용하는 기본적인 방법을 보여준다.
▶ MainWindow.cs
using Microsoft.Win32;
using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
namespace TestProject
{
/// <summary>
/// 메인 윈도우
/// </summary>
public class MainWindow : Window
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Field
////////////////////////////////////////////////////////////////////////////////////////// Private
#region Field
/// <summary>
/// 리치 텍스트 상자
/// </summary>
private RichTextBox richTextBox;
/// <summary>
/// 필터
/// </summary>
private string filter = "Document Files(*.xaml)|*.xaml|All files (*.*)|*.*";
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - MainWindow()
/// <summary>
/// 생성자
/// </summary>
public MainWindow()
{
Width = 800;
Height = 600;
Title = "RichTextBox 클래스 사용하기";
FontFamily = new FontFamily("나눔고딕코딩");
FontSize = 16;
this.richTextBox = new RichTextBox();
this.richTextBox.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
Content = this.richTextBox;
this.richTextBox.Focus();
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Static
//////////////////////////////////////////////////////////////////////////////// Public
#region 프로그램 시작하기 - Main()
/// <summary>
/// 프로그램 시작하기
/// </summary>
[STAThread]
public static void Main()
{
Application application = new Application();
application.Run(new MainWindow());
}
#endregion
////////////////////////////////////////////////////////////////////////////////////////// Instance
//////////////////////////////////////////////////////////////////////////////// Protected
#region PREVIEW 텍스트 입력시 처리하기 - OnPreviewTextInput(e)
/// <summary>
/// PREVIEW 텍스트 입력시 처리하기
/// </summary>
/// <param name="e">이벤트 인자</param>
protected override void OnPreviewTextInput(TextCompositionEventArgs e)
{
#region CTRL + O 키를 누른 경우 처리한다.
if(e.ControlText.Length > 0 && e.ControlText[0] == '\x0F')
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.CheckFileExists = true;
openFileDialog.Filter = filter;
if((bool)openFileDialog.ShowDialog(this))
{
FlowDocument flowDocument = richTextBox.Document;
TextRange textRange = new TextRange(flowDocument.ContentStart, flowDocument.ContentEnd);
Stream stream = null;
try
{
stream = new FileStream(openFileDialog.FileName, FileMode.Open);
textRange.Load(stream, DataFormats.Xaml);
}
catch(Exception exception)
{
MessageBox.Show(exception.Message, Title);
}
finally
{
if(stream != null)
{
stream.Close();
}
}
}
e.Handled = true;
}
#endregion
#region CTRL + S 키를 누른 경우 처리한다.
if(e.ControlText.Length > 0 && e.ControlText[0] == '\x13')
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = filter;
if((bool)saveFileDialog.ShowDialog(this))
{
FlowDocument flowDocument = richTextBox.Document;
TextRange textRange = new TextRange(flowDocument.ContentStart, flowDocument.ContentEnd);
Stream stream = null;
try
{
stream = new FileStream(saveFileDialog.FileName, FileMode.Create);
textRange.Save(stream, DataFormats.Xaml);
}
catch(Exception exception)
{
MessageBox.Show(exception.Message, Title);
}
finally
{
if(stream != null)
{
stream.Close();
}
}
}
e.Handled = true;
}
#endregion
base.OnPreviewTextInput(e);
}
#endregion
}
}
728x90
반응형
그리드형(광고전용)
'C# > WPF' 카테고리의 다른 글
[C#/WPF] Jeu de Tacquin (숫자 퍼즐 게임) (0) | 2018.03.18 |
---|---|
[C#/WPF] Button 클래스 : 버튼 장식하기 (0) | 2018.03.18 |
[C#/WPF] 색상 선택하기 (0) | 2018.03.18 |
[C#/WPF] 날짜 계산하기 (0) | 2018.03.18 |
[C#/WPF] 디렉토리 탐색하기 (0) | 2018.03.18 |
[C#/WPF] ToggleButton 클래스 사용하기 (0) | 2018.03.10 |
[C#/WPF] CommandBinding 클래스 사용하기 (0) | 2018.03.10 |
[C#/WPF] FrameworkElement 클래스 : 커스텀 엘리먼트 사용하기 (0) | 2018.03.10 |
[C#/WPF] RadialGradientBrush 클래스 사용하기 (0) | 2018.03.10 |
[C#/WPF] LinearGradientBrush 클래스 : MappingMode 속성 사용하기 (0) | 2018.03.10 |
댓글을 달아 주세요