繁体   English   中英

Windows Forms 中控件的位置和大小在 Visual Studio Designer 和编程实例化之间有所不同

[英]Positions and sizes of controls in Windows Forms differ between Visual Studio Designer and programmatic instantiation

当我使用 Visual Studio(Community 2019)和 Designer 工具创建 Windows Forms 应用程序时,似乎控件的大小和位置不正确。 例如,将大小为 (100, 22) 的 TextBox 定位在 (100, 80) 实际上并未定位在所需的位置。 此外,大小的 x 维度实际上是 75 而不是 100。如果我用相同的值(只是在 y 位置移动)以编程方式实例化一个类似的 TextBox,它会更靠右放置,并且与另一个,但这次使用正确的值。

见附图。

通过 Designer 设置第一个 TextBox 的属性 实际输出

为了演示这一点,我创建了一个小型测试应用程序。

程序.cs

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

namespace WindowsFormsTest
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

Form1.Designer.cs

namespace WindowsFormsTest
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #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()
        {
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(100, 80);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(100, 22);
            this.textBox1.TabIndex = 0;
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(12, 9);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(46, 17);
            this.label1.TabIndex = 1;
            this.label1.Text = "label1";
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(800, 450);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.textBox1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.Label label1;
    }
}

Form1.cs

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

namespace WindowsFormsTest
{
    public partial class Form1 : Form
    {
        private TextBox textBox2;

        public Form1()
        {
            InitializeComponent();
            TestBox();
            this.MouseMove += new MouseEventHandler(MouseMoved);
        }


        private void TestBox()
        {
            textBox2 = new TextBox() {
                Location = new System.Drawing.Point(100, 104),
                Name = "textBox2",
                Size = new System.Drawing.Size(100, 22),
                TabIndex = 1
            };

            this.Controls.Add(textBox2);
        }

        private void MouseMoved(object sender, MouseEventArgs evt)
        {
            label1.Text = "X = " + evt.X.ToString() + ", Y = " + evt.Y.ToString();
        }
    }
}

我在这里想念什么? 如何通过设计器实现控件具有所需的大小和 position(如第二个文本框),反之亦然?

提前感谢您的帮助!

几个月前我遇到了这个确切的问题。 这是您的显示器(或者从技术上讲,您是显示分辨率和缩放比例)。 我猜你有一个高 DPI 显示器。 您可以在 VS 中看到一些关于缩放的通知 - VS windows forms 设计器似乎不能很好地处理缩放。

我通过首先丢弃我对表单所做的任何更改并关闭 VS 来解决此问题。 然后将我的显示器分辨率改回标准高清(因为不缩放我的 4k 显示器会使事情变得太小 - 你可以尝试标准 100% 缩放)然后打开我的 VS 并进行更改,一切正常。

这个答案https://stackoverflow.com/a/12406133/6915929似乎描述了一种在表单上设置东西以处理 DPI 更改的方法,但我自己没有尝试过

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM