最大化缩放
//界面初始化
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);
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。