C#ClientSize计算错误

时间:2019-03-30 07:40:10

标签: c#

我运行的应用程序的控件的ClientSize计算错误。

当我在调试模式下执行以下步骤时,我发现: enter image description here

在正常模式下,我得到了错误的值:

var x = linePanel.ClientSize.Width;
>>> x = 40

linePanelSystem.Windows.Forms中带有BorderStyle.Fixed3D的普通面板。这就是ClientSize.Width应该为 36 的原因。我不知道为什么我的应用程序会出现这种行为,而解决方法却很糟糕,因为我必须在代码中用力编写正确的值。

感谢您的任何答复。

编辑:控制台应用程序(.Net Framework 4.6.1)的最小示例:

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

namespace Tools
{
    //BASE IS A CONSOLE-APP (.NET FRAMEWORK 4.6.1)
    class Program
    {
        static void Main(string[] args)
        {
            //MAIN
            Run(new Action(() => UI()));
        }

        public static void Run(Action xAction)
        {
            //RUN AS STA (SINGLETHREAD-APARTMENT)
            Thread thread = new Thread(new ThreadStart(() => { AutoResetEvent are = new AutoResetEvent(false); xAction(); are.Set(); are.WaitOne(); }));
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
        }

        public static void UI()
        {
            //UI
            Form f = new Form();
            f.FormBorderStyle = FormBorderStyle.Fixed3D;
            Size s = f.ClientSize;
            Console.WriteLine("Wrong ClientSize: " + s); //WRONG CLIENT SIZE IS 284, 261
            Console.WriteLine("Correct ClientSize: (280, 257)"); //CORRECT CLIENT SIZE AFTER OPEN DETAILS
        }
    }
}

0 个答案:

没有答案