新文本框 class 的大小 属性 乘以 1,5

Size property of new TextBox class multiplies by 1,5

我已经为我自己的文本框类型创建了一个名为 TextBoxUnderline 的 class。 class 继承了 System.Windows.Forms.TextBox。但是,当我设置 TextBoxUnderline 的大小时,它会调整为我在设计器视图中添加它时大小的 1.5 倍。例如,如果我设置 Size = new Size(100,100);并将其添加到 designerview 中,TextBoxUnderline 的大小为 150,150。

不过,如果我设置 MaximumSize = Size(100,100);它运作良好。 TextBoxUnderline 的大小为 100,100。但是,我不想将 MaximumSize 默认为 true。

这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;

namespace XYZ
{
    class TextBoxUnderline : TextBox
    {
        public TextBoxUnderline()
        {
            this.BorderStyle = BorderStyle.None;
            this.AutoSize = false;
            this.BackColor = SystemColors.Control;
            this.Size = new Size(200, 200);
            //this.MaximumSize = new Size(200, 200);


            this.MouseEnter += TextBoxUnderline_MouseEnter;
            this.MouseLeave += TextBoxUnderline_MouseLeave;
            this.GotFocus += TextBoxUnderline_GotFocus;
            this.LostFocus += TextBoxUnderline_LostFocus;

            this.Controls.Add(new Label()
            { Height = 1, Dock = DockStyle.Bottom, BackColor = Color.Black });
        }

        void TextBoxUnderline_MouseLeave(object sender, EventArgs e)
        {
            if (((TextBoxUnderline)sender).Focused == false)
            {
                ((TextBoxUnderline)sender).BackColor = SystemColors.Control;
            }
        }

        void TextBoxUnderline_MouseEnter(object sender, EventArgs e)
        {
            ((TextBoxUnderline)sender).BackColor = Color.White;
        }

        private void TextBoxUnderline_LostFocus(object sender, EventArgs e)
        {
            ((TextBoxUnderline)sender).BackColor = SystemColors.Control;
        }

        private void TextBoxUnderline_GotFocus(object sender, EventArgs e)
        {
            ((TextBoxUnderline)sender).BackColor = Color.White;
        }
    }
}

听起来您的显示缩放比例在 Windows 设置中设置为 150%。如果您希望屏幕上的像素与您在代码中设置的大小相匹配,则需要将其设置为 100%。