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

博客介绍了一种快速实现保存窗体上控件位置的方法。先创建类型化的DataSet及表,包含控件名称、位置等字段,接着给出具体代码实现保存和加载功能,最后说明调用方式,可使用Save和Load方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

然后可使用如下的代码:
None.gifusing System;
None.gif
using System.Collections;
None.gif
using System.Windows.Forms;
None.gif
using System.Reflection;
None.gif
using System.Data;
None.gif
using System.IO;
None.gif
using System.Runtime.Serialization;
None.gif
using System.Runtime.Serialization.Formatters;
None.gif
using System.Runtime.Serialization.Formatters.Binary;
None.gif
using System.Drawing;
None.gif
None.gif
namespace Persistent
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Refactor 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class Refactor
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        FormPersistent fp 
=new FormPersistent();
InBlock.gif
InBlock.gif        
//保存指定的窗体的控件布局
InBlock.gif
        public void Save(Form obj)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.RecordChildControl(obj.Controls);
InBlock.gif            fp.WriteXml(obj.Name
+".xml");
ExpandedSubBlockEnd.gif        }

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

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 记录所有的控件的指定属性值控件
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="controls"></param>

InBlock.gif        private void RecordChildControl(System.Windows.Forms.Control.ControlCollection controls)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
foreach (Control control in controls)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (control.Controls.Count>0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    RecordChildControl(control.Controls);
ExpandedSubBlockEnd.gif                }

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

ExpandedSubBlockEnd.gif            }
            
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private int rowIndex = 0;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 加载所有的控件的指定属性值
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="controls"></param>

InBlock.gif        private void LoadRecordChildControl(System.Windows.Forms.Control.ControlCollection controls)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
foreach (Control control in controls)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (control.Controls.Count>0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    LoadRecordChildControl(control.Controls);
ExpandedSubBlockEnd.gif                }

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

ExpandedSubBlockEnd.gif            }

InBlock.gif            rowIndex 
= 0;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


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

ps:
不要问我为什么把该类取名为Refactor
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值