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

转载于:https://www.cnblogs.com/thornfield_he/archive/2009/06/22/1508637.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对不起,我之前的回答有误。hWindowControl件是HALCON图像库中的件,用于显示和处理图像,并不直接支持自适应图片大小的功能。如果您需要在WinForms中实现自适应图片大小的功能,可以使用PictureBox件来显示图片并设置SizeMode属性为Zoom。以下是相应的代码示例: ```csharp private void AdjustImageSize() { // 获取PictureBox件的尺寸 int controlWidth = pictureBox.Width; int controlHeight = pictureBox.Height; // 加载图片并调整大小 Image originalImage = Image.FromFile("image.jpg"); // 替换为您实际使用的图片路径 // 计算比例 float scaleWidth = (float)controlWidth / originalImage.Width; float scaleHeight = (float)controlHeight / originalImage.Height; float scale = Math.Min(scaleWidth, scaleHeight); // 计算调整后的图片尺寸 int adjustedWidth = (int)(originalImage.Width * scale); int adjustedHeight = (int)(originalImage.Height * scale); // 创建调整后的图片 Image adjustedImage = new Bitmap(originalImage, adjustedWidth, adjustedHeight); // 在PictureBox件中显示调整后的图片 pictureBox.Image = adjustedImage; pictureBox.SizeMode = PictureBoxSizeMode.Zoom; } ``` 在上述代码中,我们通过计算比例,调整原始图片的大小,并创建调整后的图片对象。然后,将调整后的图片显示在PictureBox件中,并将SizeMode属性设置为Zoom以实现自适应图片大小的效果。 请确保已经在窗体设计器中添加了一个PictureBox件,并将其命名为pictureBox。 希望这次能够解决您的问题。如果您有任何其他疑问,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值