using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace ryControls.Controls
{
///
/// 增强富文本控件
///
public partial class RichTextBox2 : RichTextBox
{
///
/// 开始更新
///
public void BeginUpdate()
{
SendMessage(this.Handle, WM_SETREDRAW, (IntPtr)0, IntPtr.Zero);
}
///
/// 结束更新
///
public void EndUpdate()
{
SendMessage(this.Handle, WM_SETREDRAW, (IntPtr)1, IntPtr.Zero);
this.Invalidate();
}
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
private const int WM_SETREDRAW = 0x0b;
///
/// RichTextBox
///
public RichTextBox2()
{
InitializeComponent();
this.DetectUrls = false;
this.AutoWordSelection = false;
contextMenuStripRichText1.Opening += ContextMenuStripRichText1_Opening;
}
///
///
///
///
protected override void OnHScroll(EventArgs e)
{
base.Invalidate();
base.OnHScroll(e);
}
///
///
///
///
protected override void OnVScroll(EventArgs e)
{
base.Invalidate();
base.OnVScroll(e);
}
///
/// 在自带菜单弹出前激发
///
[Description("在自带菜单弹出前激发")]
public event CancelEventHandler OnMenuOpening;
private void ContextMenuStripRichText1_Opening(object sender, CancelEventArgs e)
{
OnMenuOpening?.Invoke(sender,e);
}
///
/// 根据标签获得菜单项
///
///
///
public ToolStripItem GetMenuItem(string tag)
{
for (int i = 0; i < contextMenuStripRichText1.Items.Count; i++)
{
var item = contextMenuStripRichText1.Items[i];
if (item.Tag == null) { continue; }
if(tag== item.Tag.ToString())
{
return item;
}
}
return null;
}
///
/// 只允许输入文本(只对设置之后的人工输入有效)
///
[Description("只允许输入文本(只对设置之后的人工输入有效)")]
public bool OnlyInputText { get; set; } = false;
///
/// 按下按键
///
///
protected override void OnKeyDown(KeyEventArgs e)
{
if (OnlyInputText && e.Control && e.KeyCode == Keys.V)
{
e.SuppressKeyPress = true;
this.Paste(DataFormats.GetFormat(DataFormats.Text));
return;
}
base.OnKeyDown(e);
}
///
///
///
///
protected override void OnLostFocus(EventArgs e)
{
base.Invalidate();
base.OnLostFocus(e);
}
///
///
///
///
protected override void OnEnter(EventArgs e)
{
if (base.Text.Length == 0)
{
base.Invalidate();
}
base.OnEnter(e);
}
private bool _Selecting = false;
private int _StartPosition = 0;
///
///
///
///
protected override void OnMouseMove(System.Windows.Forms.MouseEventArgs e)
{
if (_Selecting)
{
int charPosition = base.GetCharIndexFromPosition(new System.Drawing.Point(e.X, e.Y));
int length = 0;
if (charPosition >= _StartPosition)
{
length = charPosition - _StartPosition + 1;
this.Select(_StartPosition, length);
}
else
{
length = _StartPosition - charPosition;
this.Select(charPosition, length);
}
}
base.OnMouseMove(e);
}
///
///
///
///
protected override void OnMouseLeave(EventArgs e)
{
if (base.Text.Length == 0)
{
base.Invalidate();
}
base.OnMouseLeave(e);
}
///
/// 鼠标按下
///
///
protected override void OnMouseDown(MouseEventArgs e)
{
this._Selecting = true;
_StartPosition = base.GetCharIndexFromPosition(new System.Drawing.Point(e.X, e.Y));
if (e.Button==MouseButtons.Right && this.ContextMenuStrip==null)
{
contextMenuStripRichText1.Show(this, e.Location);
}
base.OnMouseDown(e);
}
///
///
///
///
protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
{
this._Selecting = false;
base.OnMouseUp(e);
}
///
///
///
///
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == 0xf || m.Msg == 0x133)
{
if (this.BorderStyle == BorderStyle.None)
{
System.Drawing.Pen pen = new Pen(SkinHelp.DefalutBorderColor, 1);
Graphics g = Graphics.FromHwnd(m.HWnd);
g.DrawRectangle(pen, 0, 0, this.Width - 1, this.Height - 1);
pen.Dispose();
return;
}
if(!base.Focused && this.Text.Length==0)
{
Graphics g = Graphics.FromHwnd(m.HWnd);
Brush brush = new SolidBrush(Color.FromArgb(175, 185, 200));
g.DrawString(EmptyText,Font, brush, 3, 3);
brush.Dispose();
}
//返回结果
m.Result = IntPtr.Zero;
}
}
///
/// 当文本框为空时,显示的内容。
///
[Description("当文本框为空时,显示的内容。")]
public string EmptyText
{
get { return _EmptyText; }
set
{
_EmptyText = value;
base.Invalidate();
}
}
private string _EmptyText = "";
///
/// 添加菜单分隔线
///
///
public ToolStripSeparator AddSeparatorMenu()
{
return contextMenuStripRichText1.AddSeparatorMenu();
}
///
/// 添加菜单
///
///
///
///
public ToolStripMenuItem AddMenu(string name, string tag)
{
return contextMenuStripRichText1.AddMenu(name, tag);
}
}
}