【问题标题】:Winform C# Application, how do I align text inside a button?Winform C# 应用程序,如何在按钮内对齐文本?
【发布时间】:2021-11-20 20:42:48
【问题描述】:

我想创建一个带有加号的按钮,但是当我使用文本创建加号时,它没有从按钮顶部对齐,如下图所示:

当我尝试增加字体大小时会出现问题。在文本的默认大小中,加号居中。我该怎么做才能将它放在我的案例中?

TextAlign (TopLeft) 和 UseCompatibleTextRendering 不起作用。 (Winforms C# Visual Studio 2019)

注意:文本对齐属性不起作用,因为我的字体大小大于默认值,即 36+。

【问题讨论】:

  • 您能告诉我如何改善这个问题吗?
  • 在 Visual Studio 的“属性”窗格中,Button 控件上应该有一个 ImageAlign 属性。它存在于 VB.NET WinForms 中,所以我假设它也存在于 C# WinForms 中。
  • @HardCode 我没有对齐图像,我正在对齐文本。
  • 那么TextAlign 属性呢?
  • @HardCode 阅读我的问题,我尝试了 textAlign 它不起作用。您是否因此建议关闭问题?

标签: c# winforms button


【解决方案1】:

您可以从资源中使用 + 的 png,而不是直接设置按钮的 text 属性。

示例

this.button1.Image = NameSpace1.Properties.Resources.Image2.png;

【讨论】:

  • 是的,我知道,但我找不到透明背景的“+”号 PNG :)。我的按钮也是 30x30 尺寸
  • 等一下,我会为你做一个高ppi的,以避免模糊
  • 使用 Photoshop 或任何照片编辑应用程序
  • 此链接ibb.co/gWvJKnH 中的图像为每英寸 9000 像素,我认为到目前为止还没有屏幕能超过这个数字。你也可以使用任何你想制作图片的字体
【解决方案2】:

一种方法是将按钮的FlatStyle (https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.buttonbase.flatstyle?view=net-5.0) 设置为System (System.Windows.Forms.FlatStyle.System),如果您可以更改FlatStyle

【讨论】:

    【解决方案3】:

    我创建了一个测试应用程序,字体“Microsoft Sans Serif”(大小为 36pt)与按钮大小为 32x32px 和 FlatStyle = FlatStyle.System(正如 elimad 已经提到的)的组合对我来说效果很好。

    namespace ControlTest
    {
        public class MainForm : Form
        {
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.Run(new MainForm());
            }
    
            private Button button1;
    
            public MainForm()
            {
                InitializeComponent();
            }
    
            private void InitializeComponent()
            {
                this.button1 = new System.Windows.Forms.Button();
                this.SuspendLayout();
                // 
                // button1
                // 
                this.button1.FlatStyle = System.Windows.Forms.FlatStyle.System;
                this.button1.Font = new System.Drawing.Font("Microsoft Sans Serif", 36F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.button1.Location = new System.Drawing.Point(16, 16);
                this.button1.Name = "button1";
                this.button1.Size = new System.Drawing.Size(32, 32);
                this.button1.TabIndex = 0;
                this.button1.Text = "+";
                // 
                // MainForm
                // 
                this.ClientSize = new System.Drawing.Size(157, 92);
                this.Controls.Add(this.button1);
                this.Name = "MainForm";
                this.Text = "ControlTest";
                this.ResumeLayout(false);
    
            }
        }
    }
    

    【讨论】:

      【解决方案4】:

      您可以创建自定义控件,然后自己渲染文本

      https://www.c-sharpcorner.com/UploadFile/f5a10c/creating-custom-controls-in-C-Sharp/

      编译后它应该只显示在工具箱中

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-12-03
        • 2012-10-06
        • 1970-01-01
        • 2021-06-01
        • 1970-01-01
        • 2017-08-28
        相关资源
        最近更新 更多