📄 xlinklabel.cs
字号:
namespace Imps.Client.Pc.Controls
{
using Microsoft.Win32;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Windows.Forms;
[DefaultEvent("LinkClicked")]
public class XLinkLabel : XLabel1
{
private Color _ieDefaultColor = Color.Empty;
private Color _ieHoverForeColor = Color.Empty;
private System.Windows.Forms.LinkBehavior _ieLinkBehavior;
private System.Windows.Forms.LinkBehavior _linkBehavior = System.Windows.Forms.LinkBehavior.AlwaysUnderline;
private Color _linkColor = Color.Blue;
public event LinkLabelLinkClickedEventHandler LinkClicked;
public XLinkLabel()
{
this.Cursor = Cursors.Hand;
}
private void EnsureFontAndForeColor()
{
this.ForeColor = this.GetForeColor();
this.Font = this.GetFont();
}
private Font GetFont()
{
switch (this.GetLinkBehavior())
{
case System.Windows.Forms.LinkBehavior.HoverUnderline:
if (!base.IsMouseOver)
{
return new Font(this.Font.FontFamily.Name, this.Font.Size, FontStyle.Regular);
}
return new Font(this.Font.FontFamily.Name, this.Font.Size, this.Font.Style | FontStyle.Underline);
case System.Windows.Forms.LinkBehavior.NeverUnderline:
return new Font(this.Font.FontFamily.Name, this.Font.Size, FontStyle.Regular);
}
return new Font(this.Font.FontFamily.Name, this.Font.Size, this.Font.Style | FontStyle.Underline);
}
private Color GetForeColor()
{
if (this.LinkBehavior == System.Windows.Forms.LinkBehavior.SystemDefault)
{
if (base.IsMouseOver)
{
if (this._ieHoverForeColor == Color.Empty)
{
this._ieHoverForeColor = this.GetIEColor("Anchor Color Hover");
}
return this._ieHoverForeColor;
}
if (this._ieDefaultColor == Color.Empty)
{
this._ieDefaultColor = this.GetIEColor("Anchor Color");
}
}
return this._linkColor;
}
private Color GetIEColor(string name)
{
Color foreColor = this.ForeColor;
RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Internet Explorer\Settings");
if (key != null)
{
string text = (string) key.GetValue(name);
if (text != null)
{
string[] textArray = text.Split(new char[] { ',' });
int[] numArray = new int[3];
int num = Math.Min(numArray.Length, textArray.Length);
for (int i = 0; i < num; i++)
{
int.TryParse(textArray[i], ref numArray[i]);
}
return Color.FromArgb(numArray[0], numArray[1], numArray[2]);
}
}
if (string.Equals(name, "Anchor Color", 5))
{
return Color.Blue;
}
if (string.Equals(name, "Anchor Color Visited", 5))
{
return Color.Purple;
}
if (!string.Equals(name, "Anchor Color Hover", 5))
{
}
return Color.Red;
}
private System.Windows.Forms.LinkBehavior GetIELinkBehavior()
{
if (this._ieLinkBehavior != System.Windows.Forms.LinkBehavior.SystemDefault)
{
return this._ieLinkBehavior;
}
RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Internet Explorer\Main");
if (key != null)
{
string strA = (string) key.GetValue("Anchor Underline");
if ((strA != null) && (string.Compare(strA, "no", true, CultureInfo.InvariantCulture) == 0))
{
return System.Windows.Forms.LinkBehavior.NeverUnderline;
}
if ((strA != null) && (string.Compare(strA, "hover", true, CultureInfo.InvariantCulture) == 0))
{
return System.Windows.Forms.LinkBehavior.HoverUnderline;
}
}
return System.Windows.Forms.LinkBehavior.AlwaysUnderline;
}
private System.Windows.Forms.LinkBehavior GetLinkBehavior()
{
if (this.LinkBehavior == System.Windows.Forms.LinkBehavior.SystemDefault)
{
return this.GetIELinkBehavior();
}
return this.LinkBehavior;
}
protected override void OnClick(EventArgs e)
{
base.OnClick(e);
if ((this.ForeColor != this.GetForeColor()) || (this.Font != this.GetFont()))
{
base.Invalidate();
}
}
protected virtual void OnLinkClicked(LinkLabelLinkClickedEventArgs e)
{
if (this.LinkClicked != null)
{
this.LinkClicked(this, e);
}
}
protected override void OnMouseClick(MouseEventArgs e)
{
base.OnMouseClick(e);
LinkLabel.Link link = new LinkLabel.Link();
link.set_Name(base.Name);
link.set_Description(this.Text);
this.OnLinkClicked(new LinkLabelLinkClickedEventArgs(link, e.Button));
}
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
if ((this.ForeColor != this.GetForeColor()) || (this.Font.Style != this.GetFont().Style))
{
base.Invalidate();
}
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if ((this.ForeColor != this.GetForeColor()) || (this.Font.Style != this.GetFont().Style))
{
base.Invalidate();
}
}
protected override void OnPaintControl(PaintEventArgs e)
{
this.EnsureFontAndForeColor();
base.OnPaintControl(e);
}
[Description("超链接的下划线行为"), Category("设计")]
public System.Windows.Forms.LinkBehavior LinkBehavior
{
get
{
return this._linkBehavior;
}
set
{
if (this._linkBehavior != value)
{
this._linkBehavior = value;
base.Invalidate();
}
}
}
[Category("设计"), Description("超链接默认字体的颜色")]
public Color LinkColor
{
get
{
return this._linkColor;
}
set
{
if (this._linkColor != value)
{
this._linkColor = value;
base.Invalidate();
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -