WinForm的自动缩放(跟随系统字体大小的改变)

当我们把系统的字体改为大字号的话,如果我们的 Form上的text和control不跟着变大的话,那会使我们的界面很难看。但是很幸运的是在WinForm中已经提供了这个功能,使用也很方便,只需要在Form的Load事件中从新设置Form的字体为系统的字体。

二  请看效果如下:( 均为系统字体为特大号时)
       修改后:

      修改前:
三  相关的代码1:
using  System;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Data;
using  System.Drawing;
using  System.Text;
using  System.Windows.Forms;

namespace  WinFormAutoScale
this.style.display='none'; Codehighlighter1_193_455_Open_Text.style.display='none'; Codehighlighter1_193_455_Closed_Image.style.display='inline'; Codehighlighter1_193_455_Closed_Text.style.display='inline';" src="https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top>ContractedBlock.gif
{
    
public partial class Form1 : Form
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
public Form1()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            InitializeComponent();
        }


        
private void Form1_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
this.Font = SystemFonts.IconTitleFont;
        }

    }

}
相关代码2:
namespace  WinFormAutoScale
ExpandedBlockStart.gifContractedBlock.gif
{
    partial 
class Form1
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// Required designer variable.
        
/// </summary>

        private System.ComponentModel.IContainer components = null;

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// Clean up any resources being used.
        
/// </summary>
        
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>

        protected override void Dispose(bool disposing)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if (disposing && (components != null))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                components.Dispose();
            }

            
base.Dispose(disposing);
        }


ContractedSubBlock.gifExpandedSubBlockStart.gif        
Windows Form Designer generated code#region Windows Form Designer generated code

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// Required method for Designer support - do not modify
        
/// the contents of this method with the code editor.
        
/// </summary>

        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            System.ComponentModel.ComponentResourceManager resources 
= new System.ComponentModel.ComponentResourceManager(typeof(Form1));
            
this.label1 = new System.Windows.Forms.Label();
            
this.button= new System.Windows.Forms.Button();
            
this.SuspendLayout();
            
// 
            
// label1
            
// 
            resources.ApplyResources(this.label1, "label1");
            
this.label1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            
this.label1.Name = "label1";
            
// 
            
// button1
            
// 
            resources.ApplyResources(this.button1, "button1");
            
this.button1.Name = "button1";
            
this.button1.UseVisualStyleBackColor = true;
            
// 
            
// Form1
            
// 
            resources.ApplyResources(this"$this");
            
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            
this.Controls.Add(this.button1);
            
this.Controls.Add(this.label1);
            
this.Name = "Form1";
            
this.Load += new System.EventHandler(this.Form1_Load);
            
this.ResumeLayout(false);
            
this.PerformLayout();

        }


        
#endregion


        
private System.Windows.Forms.Label label1;
        
private System.Windows.Forms.Button button1;
    }

}



注意上面代码中的Form的AutoScaleMode属性的设置


四  求助:在MFC中的Dialog并没有提供类似的功能,要实现Dialog的自动的缩放,要使Dialog上texts和controls都随着系统的字体大小缩放,而且还不仅跟字体有关。那位有好的办法,谢谢!


五  参考:
         http://www.cppblog.com/mzty/archive/2006/11/13/15131.html

         http://msdn2.microsoft.com/en-us/library/ms229605.aspx

转载于:https://www.cnblogs.com/peijian708/archive/2009/03/02/1401605.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值