WinForm 根据屏幕分辨率自适应_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > WinForm 根据屏幕分辨率自适应

WinForm 根据屏幕分辨率自适应

 2013/11/16 12:33:35  刘老财  博客园  我要评论(0)
  • 摘要:方法来自百度,不算太好,但目前能满足需求。(窗口在LOAD的时候记录每个控件的坐标,每次窗口重绘的时候引时SizeChange事件,根据比率重新设置坐标)以下是代码AutoSizeFormClass类usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespaceSalesClient.Sys
  • 标签:for winform

方法来自百度, 不算太好,但目前能满足需求。(窗口在LOAD的时候记录每个控件的坐标,每次窗口重绘的时候引时SizeChange事件,根据比率重新设置坐标)

以下是代码  AutoSizeFormClass类

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SalesClient.Sys
{
   public class AutoSizeFormClass
    {
        public struct controlRect
        {
            public int Left;
            public int Top;
            public int Width;
            public int Height;
        }

        private bool _Flag;
        public bool Flag
        {
            get { return _Flag; }
            set { _Flag = value; }
        }

        private int _Number;
        public int Number
        {
            get { return _Number; }
            set { _Number = value; }
        }

        private List<controlRect> oldCtrl;

        public void Initialize(Form mForm)
        {
            oldCtrl = new List<controlRect>();
            controlRect cR;

            cR.Left = mForm.Left;
            cR.Top = mForm.Top;
            cR.Width = mForm.Width;
            cR.Height = mForm.Height;

            oldCtrl.Add(cR);

            foreach (Control c in mForm.Controls)
            {
                controlRect objCtrl;
                objCtrl.Left = c.Left;
                objCtrl.Top = c.Top;
                objCtrl.Width = c.Width;
                objCtrl.Height = c.Height;
                oldCtrl.Add(objCtrl);
            }
            Flag = true;
            Number = mForm.Controls.Count;
        }

        public void ReSize(Form mForm)
        {
            if (!Flag) return;

            float wScale = (float)mForm.Width / (float)oldCtrl[0].Width;
            float hScale = (float)mForm.Height / (float)oldCtrl[0].Height;

            int ctrLeft0, ctrTop0, ctrWidth0, ctrHeight0;
            int ctrlNo = 1;

            try
            {
                if (mForm.Controls.Count != Number) return;
                foreach (Control c in mForm.Controls)
                {
                    ctrLeft0 = oldCtrl[ctrlNo].Left;
                    ctrTop0 = oldCtrl[ctrlNo].Top;
                    ctrWidth0 = oldCtrl[ctrlNo].Width;
                    ctrHeight0 = oldCtrl[ctrlNo].Height;

                    c.Left = (int)(ctrLeft0 * wScale);
                    c.Top = (int)(ctrTop0 * hScale);
                    c.Width = (int)(ctrWidth0 * wScale);
                    c.Height = (int)(ctrHeight0 * hScale);
                    ctrlNo += 1;
                }
            }
            catch
            {
                return;
            }
        }
    }
}

 

在要设置的窗体里

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SalesClient.HT
{
    public partial class test : Form
    {
        public test()
        {
            InitializeComponent();
        }
        SalesClient.Sys.AutoSizeFormClass asc = new SalesClient.Sys.AutoSizeFormClass();

        private void test_Load(object sender, EventArgs e)
        {
            asc.Initialize(this);
        }

        private void test_SizeChanged(object sender, EventArgs e)
        {
            asc.ReSize(this);
        }
    }
}

 

效果图

原来

image

 

放大后

 

image

 

这样如果屏幕分辨率改变,就可以根据缩放比率进行调整 。

发表评论
用户名: 匿名