如何在标签中为c#中的文本使用两种颜色 [英] how to use two colors in label for text in c#

查看:108
本文介绍了如何在标签中为c#中的文本使用两种颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带文字的标签:项目 .....在本文中我需要一种颜色的专业版和另一种颜色的专业版

I have one Label with Text : Project .....In this text i need pro in one color and ject in another color

推荐答案

标签中只能有一种颜色。

作为一种简单的解决方法,您可以使用两个标签(使用不同的字体颜色)来完成此操作。



更复杂的解决方案是建立自己的用户控件。
You can only have one color in a label.
As an easy workaround, you can use two labels (with different font colors) to do this.

A more complex solution would be to build your own user control.


一旦我必须创建一个Gradient标签......这就是代码(适用于Winforms)



Once I had to create a "Gradient" Label... This is the code (works in Winforms)

public partial class GradientLabel : Label {

public LabelEx() {
    InitializeComponent();
}

protected override void OnPaint(PaintEventArgs e) {
    Font font = new Font("Tahoma", 48f, FontStyle.Bold);
    LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, Width, Height + 5), Color.Gold, Color.Black, LinearGradientMode.Vertical);
    e.Graphics.DrawString(Text, font, brush, 0, 0);
}

}





这不是两种颜色,但可能会引导你解决方案。



This is not exactly "two color", but may lead you to a solution.


解决方法。使用一些文本创建一个底部按钮,然后用包含不同颜色和透明背景的一些字符的按钮覆盖它,覆盖底部的按钮。我使用按钮,因为它们都是虚拟按钮形状的对象。只有顶级项目是一个实际的按钮,这实际上相当于它有一个onclick事件。



顺便说一下,这是实用的,因为它用于某些类型的网络钓鱼诈骗 - 用真实的网站数据覆盖接受输入的透明控件,将其发送回诈骗者,然后报告错误(转到真实页面进行再入)或甚至输入文本真实屏幕作为隐形层消失。
A work-around. Create a bottom "button" with some of the text, then keep overlaying it with buttons containing some of the characters in different colors and transparent backgrounds, overlaying the bottom "button". I use "button" because all of them are dummy button-shaped objects. Only the top level item is an actual 'button', which really amounts to it having an onclick event.

This is practical enough, by the way, in that it is used in certain types of phishing scams - overlaying the real website data with transparent controls that accept the input, send it back to the scammers, and then either report an error (going to the real page for reentry) or even entering the text into the real screen as the invisible layer goes away.


这篇关于如何在标签中为c#中的文本使用两种颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆