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

 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 to change the font size and color in the line of the second line of text?

+4
source share
2 answers

Cannot use property .Text...

... but you can create a dynamic bitmap to replace the text, allowing you to format it, but you want:

Button with Custom String Rendering via an Image

        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 will need to find the best combination of font sizes, StringFormat layouts and / or positioning to make them look as desired. There are other DrawString () overloads for rendering text in different ways.

, , . . border, .

+2

, , System.Windows.Forms.Button class ( , , button - ): ; .

(, - , , , . ?)

0

All Articles