我设计了一个Windows窗体对话框,应该可以在其他应用程序,WPF和Windows窗体中重用.当我在Windows窗体应用程序中使用它时,这可以正常工作,但在WPF应用程序中调用时会导致一些布局问题.从屏幕上的像素,WinForms API所说的以及Spy ++中测量的尺寸和大小是不一致的.窗口宽10像素,在没有调试器的情况下运行比Spy ++说的更高,并且比我说它应该是.这有什么问题?我找不到任何东西,只是说它是一个严重破坏的.NET Framework.
这是Form类代码:
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++);
}
}
}
Run Code Online (Sandbox Code Playgroud)
只需将此文件放在一个空的WPF应用程序项目中,然后从应用程序构造函数中调用它:
public MainWindow()
{
InitializeComponent();
new MyDialog().ShowDialog();
Application.Current.Shutdown();
}
Run Code Online (Sandbox Code Playgroud)
以下是调试器的外观:
没有:
额外的粉红色边框是不应该在那里的10个像素.绿色标签设置为填满所有空间.
即使没有TableLayoutPanel、Label和 using System.Windows.Forms.Application.Run(new MyDialog()),问题仍然会发生。导致问题的行是FormBorderStyle = FormBorderStyle.FixedDialog;
似乎与此处描述的问题相同:Form Tells error size on Windows 8 \xe2\x80\x94 how to get real size?
\n\n解决方法:
\n\n mainLayout.SizeChanged += delegate {\n label.MaximumSize = new Size(mainLayout.Width, 0);\n //MessageBox.Show("hi"); // called when not ran in debugger\n };\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
78 次 |
| 最近记录: |