C#適應不同分辨率

窗體的代碼:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
           
            WindowsApplication1.Instance.InitInstance(this);
        }
        
    }

}



實現方法的類:

using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsApplication1
{
  public  class Instance
    {
      public static void InitInstance(System.Windows.Forms.Form form)
      {
          form.Size = new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
          Screen sc = Screen.PrimaryScreen;
          float w = sc.Bounds.Width;
          float h = sc.Bounds.Height;
          foreach (Control c in form.Controls)
          {

              // 1024   768  爲當前窗體設計的尺寸
              c.Size = new Size((int)(c.Width * w / 1024),(int) (c.Height * h / 768));
             c.Location = new Point((int)(c.Left * w / 1024),(int)( c.Top * h / 768));
             Single size = Convert.ToSingle(c.Font.Size *h/768);

             c.Font = new Font(c.Font.Name,size,c.Font.Style,c.Font.Unit);
          
          }
      }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章