- 最大化缩放
//界面初始化
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);
}