代码分享,WinForm中LinkLabel实现关键字高亮

threenewbee threenewbee 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
效果:


使用说明:

IgnoreCase 是否匹配大小写,默认false,严格匹配大小写

Keyword 关键字

KeywordColor 关键字的颜色,默认红色

BUG列表:

部分颜色属性不工作,比如访问后颜色,自定义背景色等等

不支持多行文本

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace WindowsFormsApplication1
{
class SuperLinkLabel : LinkLabel
{
public bool IgnoreCase { get; set; }

private string keyWord = "";
public string Keyword
{
get { return keyWord; }
set { keyWord = value; this.Invalidate(); }
}

public Color KeywordColor { get; set; }

public SuperLinkLabel()
{
Keyword = "";
KeywordColor = Color.Red;
}

protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.FillRectangle(new SolidBrush(SystemColors.Control), e.ClipRectangle);

StringFormat stringFormat = new StringFormat();
stringFormat.SetMeasurableCharacterRanges(GetRanges());
stringFormat.FormatFlags = StringFormatFlags.MeasureTrailingSpaces;
Region[] regions = e.Graphics.MeasureCharacterRanges(Text, Font, new RectangleF(e.ClipRectangle.X, e.ClipRectangle.Y, e.ClipRectangle.Width + 200, e.ClipRectangle.Height), stringFormat);

int i = 0;
foreach (var item in GetStringRgn().ToList())
{
bool isKeyword = GetStringRgn().Single(x => x.Item1 == item.Item1).Item3;
e.Graphics.DrawString(Text.Substring(item.Item1, item.Item2 - item.Item1 + 1), Font, isKeyword ? new SolidBrush(KeywordColor) : new SolidBrush(LinkColor), new PointF(regions[i].GetBounds(e.Graphics).X, regions[i].GetBounds(e.Graphics).Y), stringFormat);
i++;
}
}

private CharacterRange[] GetRanges()
{
return GetStringRgn().ToArray().Select(x => new CharacterRange(x.Item1, x.Item2 - x.Item1 + 1)).ToArray();
}

private IEnumerable<Tuple<int, int, bool>> GetStringRgn()
{
if (Keyword == "" || Text == "")
{
if (Text == "") yield break;
yield return new Tuple<int, int, bool>(0, Text.Length - 1, false);
yield break;
}
int pre = 0;
int i = 0;
while (i <= Text.Length - Keyword.Length)
{
if (Text.Substring(i, Keyword.Length).ToUpper() == Keyword.ToUpper())
{
if (IgnoreCase || Text.Substring(i, Keyword.Length) == Keyword)
{
if (pre != i) yield return new Tuple<int, int, bool>(pre, i - 1, false);
yield return new Tuple<int, int, bool>(i, i + Keyword.Length - 1, true);
i += Keyword.Length;
pre = i;
}
else
{
i++;
}
}
else
{
i++;
}
}
if (pre <= Text.Length - 1) yield return new Tuple<int, int, bool>(pre, Text.Length - 1, false);
}
}
}



最后show下VS2013
...全文

相关推荐

C# winform控件使用例程

C# winform 控件使用代码,包括checkbox、combobox、linklabel、menustrip、notifyicon等二十多种控件的使用方法

yadan00

WinForm超链接实现[项目源码]

本文详细介绍了在WinForm窗体上实现超链接跳转的几种方法。首先,通过使用LinkLabel控件,在LinkClicked事件中调用System.Diagnostics.Process.Start方法即可打开指定网页。其次,针对需要在新IE窗口中打开链接的需求,只需将代码调整为启动iexplore.exe并传入目标URL。此外,还讲解了如何在RichTextBox中设置可点击的文本链接,通过处理LinkClicked事件来实现点击文本跳转的功能。这些方法简单实用,适用于各种WinForm开发场景。

rainy

C# WinForm控件实战[代码]

本文详细介绍了C# WinForm开发中五种常用控件的实战应用,包括Checkbox、ComboBox、LinkLabel、MenuStrip和NotifyIcon。内容涵盖控件的基本属性设置、事件处理机制以及在项目中的集成方式,特别针对Checkbox的三态模式、ComboBox的数据绑定、LinkLabel的多链接实现、MenuStrip的层级化设计以及NotifyIcon的系统托盘功能进行了深入解析。通过具体代码示例演示了权限配置模块、联动筛选机制、帮助文档导航系统等典型场景的实现方案,为初学者提供从基础到进阶的系统性学习路径,帮助开发者快速掌握WinForm界面交互设计的核心技能。

放屁带闪电

Winform开机启动 托盘显示 打开超链接

近段时间忙于写一个winform小程序,对于擅长写web程序的我谈不上是在分享,这里所写的相当于我的备忘录,呵呵。。

丸子妈妈

C#的winform控件命名规范

主要介绍了C#的winform控件命名规范,对各种常用控件的命名规范做了较为详细的讲解,需要的朋友可以参考下

weixin_38616120
评论 66
收藏 已收藏
分享
返回顶部
threenewbee
C# 111,130 成员 642,541 内容