readonlyrichtextbox.cs

来自「自定义控件」· CS 代码 · 共 67 行

CS
67
字号
using System;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;

namespace ListViewEmbeddedControls
{
	/// <summary>
	/// Zusammenfassung f黵 ReadOnlyRichTextBox.
	/// </summary>
	public class ReadOnlyRichTextBox : RichTextBox
	{
		public ReadOnlyRichTextBox()
		{
			ReadOnly=true;
			TabStop=false;

			SetStyle(ControlStyles.Selectable, false);
		}

		// Font is overridden because assigning the RichTextBox to a new parent sets
		// its Font to the parent's Font and thus loses all formatting of existing RTF content.
		protected Font _font = new Font("Arial", 10);
		public override Font Font
		{
			get 
			{
				return _font;
			}
			set 
			{
				_font = value;
			}
		}

		[ Browsable(false) ]
		public new bool ReadOnly
		{
			get { return true; }
			set { }
		}
		
		[ Browsable(false) ]
		public new bool TabStop
		{
			get { return false; }
			set {  }
		}

		const int WM_SETFOCUS = 0x0007;
		protected override void WndProc(ref Message m)
		{
			switch (m.Msg)
			{
				case WM_SETFOCUS:
					// We don't want the RichTextBox to be able to receive focus, so just
					// pass the focus back to the control it came from.
					IntPtr prevCtl = m.WParam;
					Control c = Control.FromHandle(prevCtl);
					c.Select();
					return;
			}
			base.WndProc (ref m);
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?