I've designed a Windows Forms dialog that should be reusable in other applications, WPF and Windows Forms. This works fine when I use it in a Windows Forms application, but it causes some layout trouble when called in a WPF application. Dimensions and sizes are inconsistent when measured from the pixels on the screen, from what WinForms API says it is, and from Spy++. The window is 10 pixels wider and taller when run without a debugger than Spy++ says it is, and than I say it should be. What's the problem here? I can't find anything but say it's a badly broken .NET Framework.
Here's the Form class code:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace DialogTestApp
{
internal class MyDialog : Form
{
public MyDialog()
{
Text = "Title";
Width = 500; // -> actually 510 (Spy++ says 500)
Height = 300; // -> actually 310 (Spy++ says 300)
Font = SystemFonts.MessageBoxFont;
FormBorderStyle = FormBorderStyle.FixedDialog;
MaximizeBox = false;
MinimizeBox = false;
ShowInTaskbar = false;
StartPosition = FormStartPosition.CenterScreen;
TableLayoutPanel mainLayout = new TableLayoutPanel();
mainLayout.BackColor = Color.FromArgb(255, 171, 255); // pink
mainLayout.Dock = DockStyle.Fill;
mainLayout.Margin = Padding.Empty;
mainLayout.Padding = Padding.Empty;
mainLayout.RowStyles.Add(new RowStyle(SizeType.AutoSize)); // Only use minimum required space
mainLayout.RowStyles.Add(new RowStyle(SizeType.Percent, 100));
Controls.Add(mainLayout);
int row = 0;
Label label = new Label();
label.Text = "Hello world. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque suscipit vestibulum gravida.";
label.Font = new Font(Font.FontFamily, 12);
label.MaximumSize = new Size(mainLayout.Width, 0);
label.AutoSize = true;
label.Dock = DockStyle.Fill;
label.Margin = Padding.Empty;
label.BackColor = Color.FromArgb(58, 171, 58); // green
label.ForeColor = Color.White;
mainLayout.Controls.Add(label, 0, row++);
TextBox textBox = new TextBox();
textBox.Dock = DockStyle.Fill;
textBox.Margin = Padding.Empty;
textBox.Multiline = true;
textBox.ScrollBars = ScrollBars.Both;
mainLayout.Controls.Add(textBox, 0, row++);
}
}
}
Just put this file in an empty WPF application project and call it from the application constructor:
public MainWindow()
{
InitializeComponent();
new MyDialog().ShowDialog();
Application.Current.Shutdown();
}
Here's what it looks with a debugger:

And without:

The additional pink border is the 10 pixels that are not supposed to be there. The green label is set to fill up all space.
Even without the TableLayoutPanel, Label, and using System.Windows.Forms.Application.Run(new MyDialog()), the problem still happens. The line that is causing the issue is FormBorderStyle = FormBorderStyle.FixedDialog;
Seems to be the same issue as described here: Form tells wrong size on Windows 8 — how to get real size?
Workaround:
mainLayout.SizeChanged += delegate {
label.MaximumSize = new Size(mainLayout.Width, 0);
//MessageBox.Show("hi"); // called when not ran in debugger
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With