formatlabelcontrol.cs
来自「Fireball.CodeEditor is an source code ed」· CS 代码 · 共 1,329 行 · 第 1/3 页
CS
1,329 行
}
//}
//catch (Exception x)
//{
// Console.WriteLine(x.Message);
//}
}
}
protected override void OnPaint(PaintEventArgs e)
{
this.SetAutoSize();
//base.OnPaint (e);
if (_HasImageError)
CreateAll();
if (_bufferSurface != null)
_bufferSurface.RenderToControl(0, 0);
}
private FormatLabelElement[] CreateElements()
{
string text = this.Text.Replace("\n", "");
text = text.Replace("\r", "");
string[] parts = text.Split('<');
ArrayList Elements = new ArrayList();
int i = 0;
foreach (string part in parts)
{
FormatLabelElement cmd = new FormatLabelElement();
if (i == 0)
{
cmd.Text = part;
}
else
{
string[] TagTextPair = part.Split('>');
cmd.Tag = TagTextPair[0].ToLower();
if (cmd.Tag.IndexOfAny(" \t".ToCharArray()) >= 0)
{
int ws = cmd.Tag.IndexOfAny(" \t".ToCharArray());
string s1 = TagTextPair[0].Substring(0, ws).ToLower();
string s2 = TagTextPair[0].Substring(ws + 1);
cmd.Tag = s1 + " " + s2;
}
cmd.Text = TagTextPair[1];
if (cmd.TagName == "img")
{
FormatLabelElement img = new FormatLabelElement();
img.Tag = cmd.Tag;
Elements.Add(img);
cmd.Tag = "";
// Elements.Add (cmd);
}
//
// if (cmd.TagName == "hr")
// {
// Element hr=new Element();
// hr.Tag = cmd.Tag;
// Elements.Add (hr);
// cmd.Tag ="";
// cmd.Text ="a";
// // Elements.Add (cmd);
// }
cmd.Text = cmd.Text.Replace("\t", " ");
cmd.Text = cmd.Text.Replace("‘", "'");
cmd.Text = cmd.Text.Replace("’", "'");
cmd.Text = cmd.Text.Replace(" ", ((char) 1).ToString());
cmd.Text = HttpUtility.HtmlDecode(cmd.Text);
// cmd.Text =cmd.Text.Replace (" ","*");
cmd.Text = cmd.Text.Replace(((char) 1).ToString(), " ");
}
Elements.Add(cmd);
i++;
}
FormatLabelElement[] res = new FormatLabelElement[Elements.Count];
Elements.CopyTo(res);
return res;
}
private string GetAttrib(string attrib, string tag)
{
try
{
if (tag.IndexOf(attrib) < 0)
return "";
//tag=tag.Replace("\"","");
tag = tag.Replace("\t", " ");
int start = tag.IndexOf(attrib);
int end = start + attrib.Length;
int valuestart = tag.IndexOf("=", end);
if (valuestart < 0)
return "";
valuestart++;
string value = tag.Substring(valuestart);
while (value.StartsWith(" "))
value = value.Substring(1);
//int pos=0;
if (value.StartsWith("\""))
{
// = "value"
value = value.Substring(1);
int valueend = value.IndexOf("\"");
value = value.Substring(0, valueend);
return value;
}
else
{
// = value
int valueend = value.IndexOf(" ");
if (valueend < 0)
valueend = value.Length;
value = value.Substring(0, valueend);
return value;
}
//return "";
}
catch
{
return "";
}
}
private void ApplyFormat(FormatLabelElement[] Elements)
{
Stack bold = new Stack();
Stack italic = new Stack();
Stack underline = new Stack();
Stack forecolor = new Stack();
Stack backcolor = new Stack();
Stack fontsize = new Stack();
Stack fontname = new Stack();
Stack link = new Stack();
Stack effectcolor = new Stack();
Stack effect = new Stack();
bold.Push(this.Font.Bold);
italic.Push(this.Font.Italic);
underline.Push(this.Font.Underline);
forecolor.Push(this.ForeColor);
backcolor.Push(Color.Transparent);
fontsize.Push((int) (this.Font.Size*1.3));
fontname.Push(this.Font.Name);
effect.Push(TextEffect.None);
effectcolor.Push(Color.Black);
link.Push(null);
foreach (FormatLabelElement Element in Elements)
{
switch (Element.TagName)
{
case "b":
{
bold.Push(true);
break;
}
case "a":
{
//underline.Push (true);
//forecolor.Push (_l);
link.Push(Element);
break;
}
case "i":
case "em":
{
italic.Push(true);
break;
}
case "u":
{
underline.Push(true);
break;
}
case "font":
{
string _fontname = GetAttrib("face", Element.Tag);
string _size = GetAttrib("size", Element.Tag);
string _color = GetAttrib("color", Element.Tag);
string _effectcolor = GetAttrib("effectcolor", Element.Tag);
string _effect = GetAttrib("effect", Element.Tag);
if (_size == "")
fontsize.Push(fontsize.Peek());
else
fontsize.Push(int.Parse(_size));
if (_fontname == "")
fontname.Push(fontname.Peek());
else
fontname.Push(_fontname);
if (_color == "")
forecolor.Push(forecolor.Peek());
else
forecolor.Push(Color.FromName(_color));
if (_effectcolor == "")
effectcolor.Push(effectcolor.Peek());
else
effectcolor.Push(Color.FromName(_effectcolor));
if (_effect == "")
effect.Push(effect.Peek());
else
effect.Push(Enum.Parse(typeof (TextEffect), _effect, true));
break;
}
case "br":
{
Element.NewLine = true;
break;
}
case "hr":
{
Element.NewLine = true;
break;
}
case "h3":
{
fontsize.Push((int) (this.Font.Size*1.4));
bold.Push(true);
Element.NewLine = true;
break;
}
case "h4":
{
fontsize.Push((int) (this.Font.Size*1.2));
bold.Push(true);
Element.NewLine = true;
break;
}
case "/b":
{
bold.Pop();
break;
}
case "/a":
{
//underline.Pop ();
//forecolor.Pop ();
link.Pop();
break;
}
case "/i":
case "/em":
{
italic.Pop();
break;
}
case "/u":
{
underline.Pop();
break;
}
case "/font":
{
fontname.Pop();
fontsize.Pop();
forecolor.Pop();
effect.Pop();
effectcolor.Pop();
break;
}
case "/h3":
{
fontsize.Pop();
bold.Pop();
Element.NewLine = true;
break;
}
case "/h4":
{
fontsize.Pop();
bold.Pop();
Element.NewLine = true;
break;
}
default:
{
break;
}
}
//---------------------------------------------------------------------
bool Bold = (bool) bold.Peek();
bool Italic = (bool) italic.Peek();
bool Underline = (bool) underline.Peek();
FormatLabelElement Link = (FormatLabelElement) link.Peek();
string FontName = (string) fontname.Peek();
int FontSize = (int) fontsize.Peek();
Color BackColor = (Color) backcolor.Peek();
Color ForeColor = (Color) forecolor.Peek();
TextEffect Effect = (TextEffect) effect.Peek();
Color EffectColor = (Color) effectcolor.Peek();
FontStyle fs = 0;
if (Bold) fs |= FontStyle.Bold;
if (Italic) fs |= FontStyle.Italic;
if (Underline) fs |= FontStyle.Underline;
Font font = new Font(FontName, FontSize, fs);
Element.Font = font;
Element.BackColor = BackColor;
Element.ForeColor = ForeColor;
Element.Link = Link;
Element.Effect = Effect;
Element.EffectColor = EffectColor;
}
}
private bool IsIndex(string SRC)
{
try
{
int i = int.Parse(SRC);
return true;
}
catch
{
return false;
}
}
private void CreateWords(FormatLabelElement[] Elements)
{
GDISurface bbuff = new GDISurface(1, 1, this, false);
_HasImageError = false;
foreach (FormatLabelElement Element in Elements)
{
if (Element.TagName == "img")
{
Element.words = new FormatLabelWord[1];
Element.words[0] = new FormatLabelWord();
Image img = null;
try
{
string SRC = GetAttrib("img", Element.Tag).ToLower();
if (IsIndex(SRC))
{
int index = int.Parse(SRC);
img = this.ImageList.Images[index];
}
else if (SRC.StartsWith("http://")) //from url
{
}
else if (SRC.StartsWith("file://")) // from file
{
img = Image.FromFile(SRC.Substring(7));
}
else //from file
{
img = Image.FromFile(SRC);
}
}
catch
{
img = new Bitmap(20, 20);
_HasImageError = true;
}
Element.words[0].Image = img;
Element.words[0].Element = Element;
if (img != null)
{
Element.words[0].Height = img.Height;
Element.words[0].Width = img.Width;
Element.words[0].ScreenArea = new Rectangle(Element.words[0].ScreenArea.Location,
new Size(img.Width, img.Height));
}
}
else
{
string[] words = Element.Text.Split(' ');
Element.words = new FormatLabelWord[words.Length];
int i = 0;
foreach (string word in words)
{
Element.words[i] = new FormatLabelWord();
string tmp = "";
Element.words[i].Element = Element;
if (i == words.Length - 1)
{
Element.words[i].Text = word;
tmp = word;
}
else
{
Element.words[i].Text = word + " ";
tmp = word + " "; //last space cant be measured , lets measure an "," instead
}
//SizeF size=g.MeasureString (tmp,Element.Font);
bbuff.Font = GetFont(Element.Font);
Size s = bbuff.MeasureTabbedString(tmp, 0);
Element.words[i].Height = s.Height;
Element.words[i].Width = s.Width - 0;
Element.words[i].ScreenArea = new Rectangle(Element.words[i].ScreenArea.Location,
new Size(Element.words[i].Width, Element.words[i].Height));
// Element.words[i].Link =Element.Link ;
i++;
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?