惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
The Blog of Author Tim Ferriss
S
Securelist
D
Docker
The Register - Security
The Register - Security
GbyAI
GbyAI
Recorded Future
Recorded Future
Engineering at Meta
Engineering at Meta
Stack Overflow Blog
Stack Overflow Blog
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
罗磊的独立博客
博客园 - 【当耐特】
F
Full Disclosure
WordPress大学
WordPress大学
腾讯CDC
小众软件
小众软件
大猫的无限游戏
大猫的无限游戏
D
DataBreaches.Net
SecWiki News
SecWiki News
L
Lohrmann on Cybersecurity
I
InfoQ
MyScale Blog
MyScale Blog
量子位
Cyberwarzone
Cyberwarzone
博客园 - 三生石上(FineUI控件)
The Hacker News
The Hacker News
F
Fortinet All Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Jina AI
Jina AI
博客园_首页
H
Help Net Security
K
Kaspersky official blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Webroot Blog
Webroot Blog
Blog — PlanetScale
Blog — PlanetScale
V
Vulnerabilities – Threatpost
Y
Y Combinator Blog
The Cloudflare Blog
P
Proofpoint News Feed
V
Visual Studio Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Tailwind CSS Blog
爱范儿
爱范儿
P
Privacy International News Feed
Security Archives - TechRepublic
Security Archives - TechRepublic
The GitHub Blog
The GitHub Blog
C
Cybersecurity and Infrastructure Security Agency CISA
B
Blog RSS Feed

博客园 - 在天空飞翔

asp net core 跨平台初体验 获取图片的主色调 U盘启动安装 window server 2003 简单的中文姓名生成器 WebBrowser - 想说爱你不容易 表白 天涯宝盒-天涯看贴脚本-只看楼主-自动翻页 使用 asp.net 编写的一些大中型的网站 PrecompiledApp.config 的惨剧 [音乐] the dream catcher csv 文件的读取 扩展DataGridView 的功能(四) 三八节快乐 将MP3文件嵌入到exe中并播放 [音乐] 下个路口见 雷人的面试 发现不明飞行物 扩展 DataGridView 的功能(三) 扩展 DataGridView 的功能(二)
扩展 DataGridView 的功能(五)
在天空飞翔 · 2012-08-29 · via 博客园 - 在天空飞翔

文章最后有代码下载

DataGridView 的功能还是很强大的,每个cell都可以设置单独的style,可以打造出非常漂亮的效果。

但是如何让同一个 cell 里的文字设置不同的 style 呢, 比如像下图这样

 

有了前面的基础,相信扩展这个功能还是比较简单的, 总体来说就是重写 Cell 的 Paint 方法,想怎么画就怎么画

但是如何表示这种富文本的结构呢?想了一会,还是觉得用 html 标签来表示就行了。

上图中得文本用 html 标签来表示就变成了

string text = "<font color=\"red\" name=\"楷体\" size=\"14\">少年</font><font color=\"blue\" name=\"微软雅黑\" size=\"16\">张</font>三丰";

然后写一个简单的 html parser 解析它就行了。

主要代码如下 

void PaintText(Graphics g, Rectangle rect, string text, bool selected, DataGridViewCellStyle cell_style)
        {
            Document = parser.parse_text(text);
            Point pt = rect.Location;
            foreach (var node in Document.Nodes)
            {
                Font font = get_node_font(node, cell_style);
                Color clr = get_node_color(node, selected, cell_style);
                Size size = TextRenderer.MeasureText(node.InnerText, font);
                pt.Y = rect.Top + (rect.Height - size.Height) / 2;
                using (Brush bru = new SolidBrush(clr))
                {
                    g.DrawString(node.InnerText, font, bru, pt);
                }
                pt.X += size.Width;
            }
        }

        Font get_node_font(DOMNode node, DataGridViewCellStyle cell_style)
        {
            Font font = cell_style.Font;
            if (node.Name == "font" && node.Attributes.ContainsKey("name") && node.Attributes.ContainsKey("size"))
            {
                float font_size = cell_style.Font.Size;
                float.TryParse(node.Attributes["size"], out font_size);
                font = new Font(node.Attributes["name"], font_size);
            }
            return font;
        }

        Color get_node_color(DOMNode node, bool selected, DataGridViewCellStyle cell_style)
        {
            Color clr = selected ? cell_style.SelectionForeColor : cell_style.ForeColor;
            if (node.Name == "font" && node.Attributes.ContainsKey("color"))
            {
                clr = ColorTranslator.FromHtml(node.Attributes["color"]);
                if (selected)
                    clr = Color.FromArgb(~(clr.ToArgb()&0x00FFFFFF));
            }
            return clr;

        } 

代码下载