(Go: >> BACK << -|- >> HOME <<)

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi-line Button text not centered for the 2nd line #21696

Open
kriskazmar opened this issue Aug 16, 2023 · 5 comments
Open

Multi-line Button text not centered for the 2nd line #21696

kriskazmar opened this issue Aug 16, 2023 · 5 comments

Comments

@kriskazmar
Copy link

I have a cross-platform (Windows and Linux) WinForms application. For buttons where the button text wraps to a second line, the first line is always centered, but the second line is not. It works fine natively on Windows, but on Linux using Mono, it does not. Is there a fix for this? I've tried "MyButton.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;"

image

@kriskazmar
Copy link
Author

PrjApc - 2023-09-09.zip
The attached zip file contains a very small test program that demonstrates the problem. Ignore the Japanese character issue also being reported (issue #21697). #21697

Thank you for your help with this! Mono is an awesome software package. I hope it will remain available and supported for quite some time for Ubuntu and Yocto!

image

Though source code is not greatly required for this issue, the main source code is:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace Apc
{
#region Class FormMain
public class FormMain : System.Windows.Forms.Form
{
private Button ExitButton;
private Button ToggleLangButton;
private Font DefaultProgramFont;

static void Main()
{
	Application.Run(new FormMain());
}

public FormMain()
{
	// Component initialization - required for Windows Form Designer
	//		support
	InitializeComponent();

	int Width = 240;
	int Height = 120;

	this.Size = new Size(Width, Height);
	this.FormBorderStyle = FormBorderStyle.FixedSingle;
	this.MinimizeBox = true;
	this.Font = new Font("Microsoft Sans Serif", 8.25F,
		FontStyle.Regular);
	DefaultProgramFont = this.Font;

	ExitButton = new Button();
	ExitButton.Size = new Size(80, 32);
	ExitButton.BackColor = SystemColors.Control;
	ExitButton.ForeColor = Color.Black;
	ExitButton.Font = this.Font;
	ExitButton.Text = "Exit the Program";
	ExitButton.Location = new Point(Width - ExitButton.Width - 8,
	Height - ExitButton.Height - 8);
	ExitButton.Click += new System.EventHandler(this.ExitButton_Click);
	Controls.Add(ExitButton);

	ToggleLangButton = new Button();
	ToggleLangButton.Size = new Size(80, 32);
	ToggleLangButton.BackColor = SystemColors.Control;
	ToggleLangButton.ForeColor = Color.Black;
	ToggleLangButton.Font = this.Font;
	ToggleLangButton.Text = "Toggle Language";
	ToggleLangButton.Location = new Point(8,
		Height - ToggleLangButton.Height - 8);
	ToggleLangButton.Click += new System.EventHandler(
		this.ToggleLangButton_Click);
	Controls.Add(ToggleLangButton);
}

private void FormMain_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
    // Draw the strings
    Graphics FormGraphicsRef = this.CreateGraphics();
    Brush StrBrush = new SolidBrush(Color.Black);
    Font StrFont = new Font("MS Gothic", 12, FontStyle.Regular);

    string Str = "\u91CD\u91CF\u8A08";
    FormGraphicsRef.DrawString(Str, StrFont, StrBrush, 16, 16);
    Console.WriteLine(Str);

    Str = "\u91CD\u91CF\u8A08\u91CD\u91CF";
    FormGraphicsRef.DrawString(Str, StrFont, StrBrush, 16, 40);
    Console.WriteLine(Str);
}

private void ToggleLangButton_Click(object sender, System.EventArgs e)
{
    string JapaneseStr = "日本語";
    string EnglishStr = "Toggle Language";

    if (ToggleLangButton.Text != JapaneseStr)
    {
        ToggleLangButton.Font = new Font("MS Gothic", 9, FontStyle.Regular);
        ToggleLangButton.Text = JapaneseStr;
    }
    else
    {
        ToggleLangButton.Font = DefaultProgramFont;
        ToggleLangButton.Text = EnglishStr;
    }
}

private void ExitButton_Click(object sender, System.EventArgs e)
{
    Close();
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(FormMain));
    // 
    // FormMain
    // 
    this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
    this.BackColor = System.Drawing.Color.White;
    this.ClientSize = new System.Drawing.Size(640, 480);
    this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
    this.MaximizeBox = true;
    this.MinimizeBox = true;
    this.Text = "Test Program";
    this.Paint += new System.Windows.Forms.PaintEventHandler(this.FormMain_Paint);
}
#endregion

}
#endregion
}

@dr3loy
Copy link
dr3loy commented Oct 5, 2023

Im not a mono developer but as far as I can see, this bug falls to libgdiplus to function GDIPlus.GdipDrawString
Have you tried to build libgdiplus from sources? Maybe it helps. It also may help with japanese if you rebuild libgdiplus with pango support

@dr3loy
Copy link
dr3loy commented Oct 5, 2023

@kriskazmar
Copy link
Author
kriskazmar commented Oct 5, 2023 via email

@kriskazmar
Copy link
Author

The issue "Multi-line Button text not centered for the 2nd line" is fixed by:

mono/libgdiplus@main...kriskazmar:libgdiplus:patch-1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants