首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Windows窗体中控件的位置和大小在Visual Studio设计器和编程实例化之间有所不同

Windows窗体中控件的位置和大小在Visual Studio设计器和编程实例化之间有所不同
EN

Stack Overflow用户
提问于 2020-08-12 19:08:14
回答 1查看 205关注 0票数 1

当我使用Visual Studio (Community 2019)和设计器工具创建Windows窗体应用程序时,控件似乎没有正确的大小和位置。例如,将尺寸为(100,22)的TextBox定位在(100,80)处实际上并未定位在期望的位置。此外,大小的x维实际上是75而不是100。如果我以编程方式使用相同的值(只是在y位置上移动)实例化一个类似的TextBox,它被放置在更右侧,并且与另一个相比它的大小更大,但这次使用正确的值。

请看附件中的图片。

我已经创建了一个小的测试应用程序来演示这一点。

Program.cs

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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();
        }
    }
}

这里我漏掉了什么?如何通过设计器实现控件具有所需的大小和位置(如第二个TextBox),反之亦然?

提前感谢您的帮助!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-12 19:29:05

几个月前我就遇到过这样的问题。它是你的显示器(或者从技术上讲,你是在显示分辨率和缩放)。我猜你的DPI监控器很高。您可以看到在VS中有一个关于缩放的小通知- VS窗口窗体设计器似乎不能很好地处理缩放。

我修复了这个问题,首先丢弃了我对表单所做的任何更改,然后关闭VS。然后将我的显示器分辨率改回标准高清(因为没有缩放的情况下,我的4k显示器会让东西变得太小--你可以试一下标准的100%缩放),然后打开我的VS,做我的更改,一切都很好。

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63375256

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档