How to change font size and color in second line of text on a Winforms button?

懵懂的女人 提交于 2019-12-10 14:21:56

问题


 this.Controls.Add(button);
 button.Font = new Font("Arial", 8);
 button.Name = "btn" + idDanych;
 button.Width = 100;
 button.Height = 100;
 button.Location = new Point(0, 0);
 button.Text = "…" + Environment.NewLine + Environment.NewLine + "…";
 button.ForeColor = Color.Black;

How can I change the font size and color in the button's second line of text?


回答1:


It's not possible using the .Text property...

...but you can create a dynamic Bitmap to take the place of the Text, allowing you to format it however you want:

        Button button = new Button();
        button.Name = "btn" + idDanych;
        button.Width = 100;
        button.Height = 100;
        button.Location = new Point(0, 0);

        button.Text = "";
        Bitmap bmp = new Bitmap(button.ClientRectangle.Width, button.ClientRectangle.Height);
        using (Graphics G = Graphics.FromImage(bmp))
        {
            G.Clear(button.BackColor);

            string line1 = "( " + Wieszak + " ) " + Haczyk;
            string line2 = KodEAN;

            StringFormat SF = new StringFormat();
            SF.Alignment = StringAlignment.Center;
            SF.LineAlignment = StringAlignment.Near;
            using (Font arial = new Font("Arial", 12))
            {
                Rectangle RC = button.ClientRectangle;
                RC.Inflate(-5, -5);
                G.DrawString(line1, arial, Brushes.Black, RC, SF);
            }

            using (Font courier = new Font("MS Courier", 24))
            {
                SF.LineAlignment = StringAlignment.Center;
                G.DrawString(line2, courier, Brushes.Red, button1.ClientRectangle, SF);
            }
        }
        button.Image = bmp;
        button.ImageAlign = ContentAlignment.MiddleCenter;

        this.Controls.Add(button);

You'll have to figure out the best combination of font sizes, StringFormat layouts, and/or positioning to make it look as desired. There are other DrawString() overloads to render the text in different ways.

Note that there will be a difference in how the control highlights, however. On my system, the entire area of the standard button highlights when the mouse enters. With this approach only the border will highlight since the entire middle of the button is a static image.




回答2:


What you want to do is not possible with the standard System.Windows.Forms.Button class (of which, I assume, button is an instance): The font size and color apply to all of the text; you cannot change these properties for just one part of the text.

(By the way, the second line of text in your button is an empty line, so you would probably not notice a different font or color anyway. Did you mean the third line of text?)



来源:https://stackoverflow.com/questions/28203383/how-to-change-font-size-and-color-in-second-line-of-text-on-a-winforms-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!