using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace WebSolus.Windows.Controls
{
class KeyWordRichTextBox:RichTextBox
{
#region 속성
/// <summary>
/// 검색할 KeyWord를 가져오거나 설정합니다.
/// </summary>
public List<string> KeyWords {get;set; }
#endregion
#region 생성자
/// <summary>
/// 생성자
/// </summary>
public KeyWordRichTextBox()
{
InitializeComponent();
}
#endregion
#region InitializeComponent
/// <summary>
/// InitializeComponent
/// </summary>
private void InitializeComponent()
{
this.SuspendLayout();
this.ResumeLayout(false);
this.KeyWords = new List<string>();
this.KeyUp += new KeyEventHandler(KeyWordRichTextBox_KeyUp);
}
#endregion
#region Api Function
/// <summary>
/// Caret의 위치를 가져옵니다.
/// </summary>
/// <param name="pt">Point 개체</param>
/// <returns>true 성공,false 실패</returns>
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool GetCaretPos(out System.Drawing.Point pt);
#endregion
#region Event
/// <summary>
/// 키를 놓을 때 발생합니다.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void KeyWordRichTextBox_KeyUp(object sender, KeyEventArgs e)
{
for (int i = 0; i < this.KeyWords.Count; i++)
{
string word = this.KeyWords[i];
if (this.SelectionStart >= word.Length)
{
int result = this.Find(word, this.SelectionStart - word.Length, this.SelectionStart, RichTextBoxFinds.MatchCase);
if (result != -1)
{
System.Drawing.Point nearPoint = new System.Drawing.Point();
GetCaretPos(out nearPoint);
int index = this.GetCharIndexFromPosition(nearPoint);
this.SelectionColor = System.Drawing.Color.Blue;
if (index + 1 == this.TextLength)
{
this.SelectionStart = index + 1;
}
else
{
this.SelectionStart = index;
}
this.SelectionColor = System.Drawing.Color.Black;
this.DeselectAll();
}
}
}
}
#endregion
}
}
결과 이미지
프레임워크 버젼 : 닷넷 프레임워크 2.0
'.Net > Winform' 카테고리의 다른 글
C# Keyboard, Mouse Hooking (0) | 2012.06.28 |
---|---|
C# 지정한 폴더 감시하기 (0) | 2012.06.27 |
RichTextBox 원하는 단어들 원하는 색으로 변경 (0) | 2012.05.11 |
C# WndProc이용, USB인식 및 해제 감시 (0) | 2011.12.07 |
C# FileSystemWatcher (3) | 2011.12.07 |