winForm画面缩放的代码,实现控件缩放,字体缩放,labe右对齐,datagridview字体缩放

这是一段用于winForm画面缩放的代码,能实现控件缩放,字体缩放,labe右对齐,datagridview字体缩放。写得很臃肿,有好办法,欢迎共享。

使用方法:在画面的虚构函数中调用autoMaxSize(this)就可以了。
其中的代码可根据具体情况删改,我的画面是必须这样复杂,无奈。。。


#region 最大化の時、コントロールの大きさを変更する。

/// <summary>
/// 画面ロードの時、使用される。
/// </summary>
/// <param name="frm">画面</param>
private void autoMaxSize(Form frm)
{
frm.Tag = frm.Width.ToString() + "," + frm.Height.ToString();
//因为是事件触发器的添加必须发生在from_load之前,故此方法应在虚构函数中调用。
frm.SizeChanged += new EventHandler(frm_SizeChanged);


}

/// <summary>
/// 画面の大きさを変更されている時の事件処理関数
/// </summary>
/// <param name="sender">事件の送信者</param>
/// <param name="e">事件処理引数</param>
private void frm_SizeChanged(object sender, EventArgs e)
{
if (this.WindowState != FormWindowState.Minimized)
{
string[] tmp = ((Form)sender).Tag.ToString().Split(',');
float width = (float)((Form)sender).Width / float.Parse(tmp[0]);
float heigth = (float)((Form)sender).Height / float.Parse(tmp[1]);
((Form)sender).Tag = ((Form)sender).Width.ToString() + "," + ((Form)sender).Height;
foreach (Control control in ((Form)sender).Controls)
{
control.Scale(new SizeF(width, heigth));
//这个字体缩放主要是针对datagridview的字体的。
control.Font = new Font(control.Font.FontFamily, 0.008875f * this.Width);
//实现groupbox中文本框的高度和字体改变。
if (control.GetType().Name.Equals("GroupBox"))
{
foreach (Control con in ((GroupBox)control).Controls)
{
if (con.GetType().Name.Contains("TextBox"))
{
con.AutoSize = false;
((TextBox)con).Height = 21 * this.Height / 670;
((TextBox)con).Font = new Font(((TextBox)con).Font.FontFamily, 0.011875f * this.Width);
}
}
}
}

//实现label的右对齐。
int intLocation = 0;
intLocation = label2.Location.X + label2.Width;
label4.Location = new Point((intLocation - label4.Width), label4.Location.Y);
label5.Location = new Point((intLocation - label5.Width), label5.Location.Y);
label6.Location = new Point((intLocation - label6.Width), label6.Location.Y);
label8.Location = new Point((intLocation - label8.Width), label8.Location.Y);
label10.Location = new Point((intLocation - label10.Width), label10.Location.Y);
label11.Location = new Point((intLocation - label11.Width), label11.Location.Y);

intLocation = label3.Location.X + label3.Width;
label7.Location = new Point((intLocation - label7.Width), label7.Location.Y);
label9.Location = new Point((intLocation - label9.Width), label9.Location.Y);

intLocation = label14.Location.X + label14.Width;
label17.Location = new Point((intLocation - label17.Width), label17.Location.Y);

intLocation = label15.Location.X + label15.Width;
label18.Location = new Point((intLocation - label18.Width), label18.Location.Y);

this.lblTitle.Font = new Font(this.lblTitle.Font.FontFamily, 0.011875f * this.Width);
//实现字体缩放,注意要是显示指定了label的字体的话,这段代码就不会起作用。
//请删去显示指定,窗体控件的字体默认与窗体字体相同。
this.groupBox1.Font = new Font(this.groupBox1.Font.FontFamily,0.011875f * this.Width );
this.groupBox2.Font = new Font(this.groupBox2.Font.FontFamily, 0.011875f * this.Width);

}

}

#endregion

 

 

转自 http://www.xa-ccb.com/forum/posts/list/28.page

posted @ 2009-06-22 18:32  何翔华  阅读(1997)  评论(0编辑  收藏  举报