標籤:.net c#
最大化縮放
//介面初始化
private void frmTemp_Load(object sender, EventArgs e)
{
//全螢幕顯示
Rectangle ScreenArea = System.Windows.Forms.Screen.GetWorkingArea(this);
//設定水平伸縮比例
dbScaleX = ScreenArea.Width /Convert.ToDouble(this.Width);
//設定垂直伸縮比例
dbScaleY = ScreenArea.Height /Convert.ToDouble(this.Height);
this.Width = ScreenArea.Width;
this.Height = ScreenArea.Height;
//表單的位置由 Location 屬性確定。
this.StartPosition = FormStartPosition.Manual;
this.Location = new Point(0, 0);
this.MaximizeBox = false;
this.MinimizeBox = false;
controlsDfsToResize(frmMain.Instance.Controls,dbScaleX,dbScaleY);
}
/// <summary>
/// 對form中的控制項進行深度優先遍曆,
/// 隨著Form的大小調整子控制項的大小及位置
/// </summary>
/// <param name="controls"></param>
private static void controlsDfsToResize(Control.ControlCollection controls, double dbScaleX,double dbScaleY)
{
foreach (Control ctrl in controls)
{
ctrl.Left = (int)(Convert.ToDouble(ctrl.Left) * dbScaleX);
ctrl.Top = (int)(Convert.ToDouble(ctrl.Top) * dbScaleY);
ctrl.Width = (int)(Convert.ToDouble(ctrl.Width) * dbScaleX);
ctrl.Height = (int)(Convert.ToDouble(ctrl.Height) * dbScaleY);
//如果控制項是Datagridview,則縮放列的行寬
if (ctrl.GetType().Equals(typeof(DataGridView)))
{
DataGridView dgv = (DataGridView)ctrl;
foreach (DataGridViewColumn col in dgv.Columns)
{
col.Width = Convert.ToInt32(col.Width * dbScaleX);
}
}
if (ctrl.Controls.Count > 0)
{
controlsDfsToResize(ctrl.Controls, dbScaleX, dbScaleY);
}
}
}
2.不可移動
/// <summary>
/// 欺騙表單,讓表單認為點擊的為非表單地區,從而實現不可移動(有BUG,所以需要下面的函數)
/// </summary>
/// <param name="m"></param>
protected override void WndProc(ref Message m)
{
if(m.Msg == 0x00A1 && m.WParam.ToInt32() == 2)
{
m.Msg = 0x0201;
m.LParam = IntPtr.Zero;
}
base.WndProc(ref m);
}
/// <summary>
/// 不讓表單移動
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void frmMain_Move(object sender, EventArgs e)
{
this.Location = new Point(0,0);
}
Winform 最大化縮放、不可移動