我运行的应用程序的控件的ClientSize计算错误。
在正常模式下,我得到了错误的值:
var x = linePanel.ClientSize.Width;
>>> x = 40
linePanel
是System.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
}
}
}