您好,登錄后才能下訂單哦!
最大化縮放
//界面初始化
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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。