formatlabelcontrol.cs

来自「Fireball.CodeEditor is an source code ed」· CS 代码 · 共 1,329 行 · 第 1/3 页

CS
1,329
字号
//ORIGINAL LGPL SOURCE CODE FINDED ON COMPONA LGPL SOURCE CODE
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Web;
using System.Windows.Forms;
using Fireball.Drawing.GDI;

namespace Fireball.Windows.Forms
{
	/// <summary>
	/// 
	/// </summary>
	public class FormatLabelControl : Widget
	{
		private string _Text = "format <b>label</b>";
		private FormatLabelElement[] _Elements = null;
		private ArrayList _Rows = null;
		private Hashtable _Fonts = new Hashtable();
		private Hashtable _Images = new Hashtable();
		private bool _WordWrap = true;
		private PictureBox Filler;
		private VScrollBar vScroll;
		private HScrollBar hScroll;
		private bool _AutoSizeHorizontal = false;
		private bool _AutoSizeVertical = false;
		private ScrollBars _ScrollBars = 0;
		private FormatLabelElement _ActiveElement = null;
		private Color _Link_Color = Color.Blue;
		private Color _Link_Color_Hover = Color.Blue;
		private bool _Link_UnderLine = false;
		private bool _Link_UnderLine_Hover = true;
		private bool _HasImageError = false;

		private ImageList _ImageList = null;

		public event ClickLinkEventHandler ClickLink = null;

		protected void OnClickLink(string Link)
		{
			if (ClickLink != null)
				ClickLink(this, new ClickLinkEventArgs(Link));
		}

		#region PUBLIC PROPERTY MARGIN

		private int _Margin = 0;

		public int LabelMargin
		{
			get { return _Margin; }
			set
			{
				_Margin = value;
				CreateRows();
				this.Invalidate();
			}
		}

		#endregion

		public ImageList ImageList
		{
			get { return _ImageList; }
			set
			{
				_ImageList = value;
				this.Invalidate();
				//this.Text = this.Text;
			}
		}

		public Color Link_Color
		{
			get { return _Link_Color; }
			set
			{
				_Link_Color = value;
				this.Invalidate();
			}
		}

        protected override void OnInvalidated(InvalidateEventArgs e)
        {
            if (this.IsHandleCreated)
                RedrawBuffer();
            base.OnInvalidated(e);
        }



		public Color Link_Color_Hover
		{
			get { return _Link_Color_Hover; }
			set
			{
				_Link_Color_Hover = value;
				this.Invalidate();
			}
		}

		public bool Link_UnderLine
		{
			get { return _Link_UnderLine; }
			set
			{
				_Link_UnderLine = value;
				this.Invalidate();
			}
		}

		public bool Link_UnderLine_Hover
		{
			get { return _Link_UnderLine_Hover; }
			set
			{
				_Link_UnderLine_Hover = value;
				this.Invalidate();
			}
		}


		public bool AutoSizeHorizontal
		{
			get { return _AutoSizeHorizontal; }
			set { _AutoSizeHorizontal = value; }

		}

		public bool AutoSizeVertical
		{
			get { return _AutoSizeVertical; }
			set { _AutoSizeVertical = value; }

		}

		#region Defaults

		private Container components = null;

		public FormatLabelControl()
		{
            _Rows = new ArrayList();

			InitializeComponent();

            SetStyle(ControlStyles.ResizeRedraw, true);
            SetStyle(ControlStyles.Opaque, true);

			this.Text = this.Text;
            InitScrollbars(); 
		}

		protected override void Dispose(bool disposing)
		{
			if (disposing)
			{
				foreach (GDIObject o in this._Fonts.Values)
					o.Dispose();

				if (components != null)
					components.Dispose();

                if (_bufferSurface != null)
                    _bufferSurface.Dispose();
			}
			base.Dispose(disposing);
		}

		#region Component Designer generated code

		/// <summary>
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.Filler = new System.Windows.Forms.PictureBox();
			this.vScroll = new System.Windows.Forms.VScrollBar();
			this.hScroll = new System.Windows.Forms.HScrollBar();
			this.SuspendLayout();
			// 
			// Filler
			// 
			this.Filler.BackColor = System.Drawing.SystemColors.Control;
			this.Filler.Cursor = System.Windows.Forms.Cursors.Default;
			this.Filler.Location = new System.Drawing.Point(136, 112);
			this.Filler.Name = "Filler";
			this.Filler.Size = new System.Drawing.Size(16, 16);
			this.Filler.TabIndex = 5;
			this.Filler.TabStop = false;
			// 
			// vScroll
			// 
			this.vScroll.Cursor = System.Windows.Forms.Cursors.Default;
			this.vScroll.LargeChange = 2;
			this.vScroll.Location = new System.Drawing.Point(136, -8);
			this.vScroll.Name = "vScroll";
			this.vScroll.Size = new System.Drawing.Size(16, 112);
			this.vScroll.TabIndex = 4;
			this.vScroll.Scroll += new System.Windows.Forms.ScrollEventHandler(this.vScroll_Scroll);
			// 
			// hScroll
			// 
			this.hScroll.Cursor = System.Windows.Forms.Cursors.Default;
			this.hScroll.LargeChange = 1;
			this.hScroll.Location = new System.Drawing.Point(0, 112);
			this.hScroll.Maximum = 600;
			this.hScroll.Name = "hScroll";
			this.hScroll.Size = new System.Drawing.Size(128, 16);
			this.hScroll.TabIndex = 3;
			// 
			// FormatLabelControl
			// 
			this.BackColor = System.Drawing.SystemColors.Window;
			this.Controls.AddRange(new System.Windows.Forms.Control[]
				{
					this.Filler,
					this.vScroll,
					this.hScroll
				});
			this.Name = "FormatLabelControl";
			this.Size = new System.Drawing.Size(160, 136);
			this.ResumeLayout(false);

		}

		[Browsable(true)]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		public override string Text
		{
			get { return _Text; }
			set
			{
				try
				{
					//Text=value;
					_Text = value;

					CreateAll();
					this.Invalidate();
				}
				catch (Exception x)
				{
					Console.WriteLine(x.Message);
					System.Diagnostics.Debugger.Break();
				}

                RedrawBuffer();
			}

		}

		private void CreateAll()
		{
			_Elements = CreateElements();
			ClearFonts();


			ApplyFormat(_Elements);
			CreateWords(_Elements);
			CreateRows();
			SetAutoSize();
		}

		private void ClearFonts()
		{
			foreach (GDIFont gf in _Fonts.Values)
			{
				gf.Dispose();
			}
			_Fonts.Clear();
		}

		#endregion

		#endregion

		private void SetAutoSize()
		{
			if (this.AutoSizeHorizontal)
				this.Width = this.GetWidth();

			if (this.AutoSizeVertical)
				this.Height = this.GetHeight();

		}

		public bool WordWrap
		{
			get { return _WordWrap; }
			set
			{
				_WordWrap = value;
				CreateRows();
				this.Invalidate();
			}


		}


		[Browsable(true), EditorBrowsable(EditorBrowsableState.Always), Obsolete("", false)]
		public override Image BackgroundImage
		{
			get { return base.BackgroundImage; }
			set { base.BackgroundImage = value; }
		}

        GDISurface _bufferSurface = null;

        private void RedrawBuffer()
        {
            if (_bufferSurface != null)
                _bufferSurface.Dispose();

            _bufferSurface = new GDISurface(this.Width, this.Height, this, true);

            using (Graphics gfx = Graphics.FromHdc(_bufferSurface.hDC))
            {
                //try
                //{
                _bufferSurface.FontTransparent = true;

                if (this.BackgroundImage != null)
                {
                    gfx.DrawImage(this.BackgroundImage, 0, 0, this.Width, this.Height);
                }
                else
                {
                    _bufferSurface.Clear(this.BackColor);
                }
                int x = LabelMargin;
                int y = LabelMargin;
                for (int i = vScroll.Value; i < _Rows.Count; i++)
                {
                    FormatLabelRow r = (FormatLabelRow)_Rows[i];
                    x = LabelMargin;
                    r.Visible = true;
                    r.Top = y;
                    if (r.RenderSeparator)
                    {
                        Color c1 = Color.FromArgb(120, 0, 0, 0);
                        Brush b1 = new SolidBrush(c1);
                        gfx.FillRectangle(b1, 0, y, this.Width, 1);

                        Color c2 = Color.FromArgb(120, 255, 255, 255);
                        Brush b2 = new SolidBrush(c2);
                        gfx.FillRectangle(b2, 0, y + 1, this.Width, 1);

                        b1.Dispose();
                        b2.Dispose();


                        //bbuff.DrawLine (this.ForeColor,new Point (0,y),new Point (this.Width,y));
                    }

                    foreach (FormatLabelWord w in r.Words)
                    {
                        int ypos = r.Height - w.Height + y;

                        if (w.Image != null)
                        {
                            gfx.DrawImage(w.Image, x, y);
                            //bbuff.FillRect (Color.Red ,x,ypos,w.Width ,w.Height);
                        }
                        else
                        {
                            GDIFont gf = null;
                            if (w.Element.Link != null)
                            {
                                Font f = null;

                                FontStyle fs = w.Element.Font.Style;
                                if (w.Element.Link == _ActiveElement)
                                {
                                    if (_Link_UnderLine_Hover)
                                        fs |= FontStyle.Underline;

                                    f = new Font(w.Element.Font, fs);
                                }
                                else
                                {
                                    if (_Link_UnderLine)
                                        fs |= FontStyle.Underline;

                                    f = new Font(w.Element.Font, fs);
                                }

                                gf = GetFont(f);
                            }
                            else
                            {
                                gf = GetFont(w.Element.Font);
                            }

                            _bufferSurface.Font = gf;
                            if (w.Element.Effect != TextEffect.None)
                            {
                                _bufferSurface.TextForeColor = w.Element.EffectColor;

                                if (w.Element.Effect == TextEffect.Outline)
                                {
                                    for (int xx = -1; xx <= 1; xx++)
                                        for (int yy = -1; yy <= 1; yy++)
                                            _bufferSurface.DrawTabbedString(w.Text, x + xx, ypos + yy, 0, 0);


                                }
                                else if (w.Element.Effect != TextEffect.None)
                                {
                                    _bufferSurface.DrawTabbedString(w.Text, x + 1, ypos + 1, 0, 0);
                                }
                            }


                            if (w.Element.Link != null)
                            {
                                if (w.Element.Link == _ActiveElement)
                                {
                                    _bufferSurface.TextForeColor = Link_Color_Hover;
                                }
                                else
                                {
                                    _bufferSurface.TextForeColor = Link_Color;
                                }
                            }
                            else
                                _bufferSurface.TextForeColor = w.Element.ForeColor;

                            _bufferSurface.TextBackColor = w.Element.BackColor;
                            _bufferSurface.DrawTabbedString(w.Text, x, ypos, 0, 0);
                        }

                        w.ScreenArea =new Rectangle(new Point(x, ypos), w.ScreenArea.Size);
                        //w.ScreenArea.Y = ypos;
                        x += w.Width;
                    }

                    y += r.Height + r.BottomPadd;
                    if (y > this.Height)
                        break;

⌨️ 快捷键说明

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