首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

创设了一个Form增大缩小类封装,但是这个封装却没有任何效果,希望各位指点下

2013-11-05 
创建了一个Form增大缩小类封装,但是这个封装却没有任何效果,希望各位指点下。下面是封装的缩小放大类,这个

创建了一个Form增大缩小类封装,但是这个封装却没有任何效果,希望各位指点下。
下面是封装的缩小放大类,这个类直接在form1中调用是正确的。

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

namespace 放大和缩小界面
{
    class resize : Form
    {
        public void firstDemo(Form it)
        {
            try
            {
                int count = it.Controls.Count * 2 + 2;
                float[] factor = new float[count];
                int i = 0;
                factor[i++] = Size.Width;
                factor[i++] = Size.Height;
                foreach (Control ctrl in it.Controls)
                {
                    factor[i++] = ctrl.Location.X / (float)Size.Width;
                    factor[i++] = ctrl.Location.Y / (float)Size.Height;
                    ctrl.Tag = ctrl.Size;
                }
                Tag = factor;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        public void resizeStatus(Form it)
        {
            try
            {
                float[] scale = (float[])Tag;
                int i = 2;
                foreach (Control ctrl in it.Controls)
                {
                    ctrl.Left = (int)(Size.Width * scale[i++]);
                    ctrl.Top = (int)(Size.Height * scale[i++]);
                    ctrl.Width = (int)(Size.Width / (float)scale[0] * ((Size)ctrl.Tag).Width);
                    ctrl.Height = (int)(Size.Height / (float)scale[1] * ((Size)ctrl.Tag).Height);
                    //每次使用的都是最初始的控件大小,保证准确无误。
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

    }
}

这个下面是form1中的调用情况
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 放大和缩小界面
{
    public partial class Form1 : Form
    {
        resize res = new resize();


        public Form1()
        {
            InitializeComponent();
            this.Resize += new EventHandler(Form1_Resize);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            res.firstDemo(this);
        }
        private void Form1_Resize(object sender, EventArgs e)
        {
            res.resizeStatus(this);
        }
    }
}


[解决办法]
firstDemo函数中漏了2个地方。
actor[i++] = ctrl.Location.X / (float)it.Size.Width;
factor[i++] = ctrl.Location.Y / (float)it.Size.Height;

热点排行