本文介绍了相同文字上有2种字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在尝试为我的公司申请。

我想知道是否有可能在同一个按钮上有两种类型的文字,例如1粗体中的单词和正常的其他单词。

Hello, i'm trying to make an application for my company.
And i want to know if it is possible to have two types of text on the same button, like 1 word in bold and other word in normal.

我正在试图找出它,但找不到任何相关的信息。我希望得到你的帮助。



最好的问候,



Pedro Costa

I'm trying to figure it out, but can't find any information about that. I would aprecciate your help.

Best Regards,

Pedro Costa

推荐答案

感谢您发布此处。

基于您的描述,您希望在同一个按钮上有两种类型的文本。

Based on your description, you want to have two types of text on the same button.

正如Castorix31所说,您可以尝试覆盖OnPaint。

As Castorix31 said, you could try override OnPaint.

public class XButton : Button
    {
        public XButton()
        {
            UseVisualStyleBackColor = false;
            TextImageRelation = TextImageRelation.ImageAboveText;
        }
        public override string Text
        {
            get { return ""; }
            set { base.Text = value; }
        }
        public string LeftText { get; set; }
        public string RightText { get; set; }
        protected override void OnPaint(PaintEventArgs pevent)
        {
            base.OnPaint(pevent);
            Rectangle rect = ClientRectangle;
            rect.Inflate(-5, -5);
            using (StringFormat sf = new StringFormat() { Alignment = StringAlignment.Near, LineAlignment = StringAlignment.Far })
            {
                using (Brush brush = new SolidBrush(ForeColor))
                {
                    Font font= new Font(Font,FontStyle.Bold);
                    pevent.Graphics.DrawString(LeftText, font, brush, rect, sf);
                    sf.Alignment = StringAlignment.Far;
                    pevent.Graphics.DrawString(RightText, Font, brush, rect, sf);
                }
            }
        }
    }

 private void Form1_Load(object sender, EventArgs e)
        {
            XButton button = new XButton();
            button.LeftText = "hello";
            button.RightText = "test";
            this.Controls.Add(button);
        }

结果:

最好的问候,

杰克


这篇关于相同文字上有2种字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 00:14