快速保存与加载窗体上控件的位置

这里的快速是指写起来快,不是指它运行起来快,但也不算慢了,此处功能仅可以实现保存窗体上的控件位置。

首先,创建一个类型化的DataSet,命名为FormPersistent,并在其中创建一张表叫“Controls”,元素(字段)包括ControlName,LocationX,LoctaionY,分别是string,int,int

然后可使用如下的代码:
using System;
using System.Collections;
using System.Windows.Forms;
using System.Reflection;
using System.Data;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
using System.Drawing;

namespace Persistent
{
    
/// <summary>
    
/// Refactor 的摘要说明。
    
/// </summary>

    public class Refactor
    
{
        FormPersistent fp 
=new FormPersistent();

        
//保存指定的窗体的控件布局
        public void Save(Form obj)
        
{
            
this.RecordChildControl(obj.Controls);
            fp.WriteXml(obj.Name
+".xml");
        }


        
//保存指定的窗体的控件布局
        public void Load(Form obj)
        
{
            
if (File.Exists(obj.Name+".xml"))
            
{
                fp.ReadXml(obj.Name
+".xml");
                LoadRecordChildControl(obj.Controls);
            }

        }


        
/// <summary>
        
/// 记录所有的控件的指定属性值控件
        
/// </summary>
        
/// <param name="controls"></param>

        private void RecordChildControl(System.Windows.Forms.Control.ControlCollection controls)
        
{
            
foreach (Control control in controls)
            
{
                
if (control.Controls.Count>0)
                
{
                    RecordChildControl(control.Controls);
                }

                
else
                
{
                    FormPersistent.ControlsRow row 
= (FormPersistent.ControlsRow )fp.Controls.NewRow();
                    row.ControlName 
= control.Name;//名称
                    row.LocationX = control.Location.X;//位置X
                    row.LocationY = control.Location.Y;//位置Y
                    fp.Controls.Rows.Add(row);
                }

            }
            
        }


        
private int rowIndex = 0;

        
/// <summary>
        
/// 加载所有的控件的指定属性值
        
/// </summary>
        
/// <param name="controls"></param>

        private void LoadRecordChildControl(System.Windows.Forms.Control.ControlCollection controls)
        
{
            
foreach (Control control in controls)
            
{
                
if (control.Controls.Count>0)
                
{
                    LoadRecordChildControl(control.Controls);
                }

                
else
                
{    
                     FormPersistent.ControlsRow row 
= (FormPersistent.ControlsRow)fp.Controls.Rows[rowIndex];
                    control.Name 
= row.ControlName;
                    control.Location 
= new System.Drawing.Point(row.LocationX,row.LocationY);
                    rowIndex
++;
                }

            }

            rowIndex 
= 0;
        }

    }

}



调用时
Persistent.Refactor refactor = new Persistent.Refactor()后,可以用refactor.Save()或refactor.Load()

ps:
不要问我为什么把该类取名为Refactor
原文地址:https://www.cnblogs.com/William_Fire/p/171648.html