近期做項目時,遇到開發的winform在自己電腦上可以正常顯示,共享到其他電腦就事兒不能顯示了:
1.當兩個電腦分辨率相同時,無法顯示完全,請檢查form的autoscalemode屬性是否為none,並設為none
2.分辨率不同時,可直接在form的構造函數中調用初始化函數之後, 加上一句AutoReSizeForm. SetFormSize(this);(對於自定義控件usercontrol也適用)
public class AutoReSizeForm
{
static float SH
{
get
{
return (float)Screen.PrimaryScreen.Bounds.Height / Properties.Settings.Default.Y;
}
}
static float SW
{
get
{
return (float)Screen.PrimaryScreen.Bounds.Width / Properties.Settings.Default.X;
}
}
public static void SetFormSize(Control fm)
{
fm.Location = new Point((int)(fm.Location.X * SW), (int)(fm.Location.Y * SH));
fm.Size = new Size((int)(fm.Size.Width * SW), (int)(fm.Size.Height * SH));
fm.Font = new Font(fm.Font.Name, fm.Font.Size * SH,fm.Font.Style,fm.Font.Unit,fm.Font.GdiCharSet,fm.Font.GdiVerticalFont);
if (fm.Controls.Count!=0)
{
SetControlSize(fm);
}
}
private static void SetControlSize(Control InitC)
{
foreach (Control c in InitC.Controls)
{
c.Location = new Point((int)(c.Location.X * SW), (int)(c.Location.Y * SH));
c.Size = new Size((int)(c.Size.Width * SW), (int)(c.Size.Height * SH));
c.Font = new Font(c.Font.Name, c.Font.Size * SH, c.Font.Style, c.Font.Unit, c.Font.GdiCharSet, c.Font.GdiVerticalFont);
if (c.Controls.Count != 0)
{
SetControlSize(c);
}
}
}
}