using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
using System.ComponentModel;
namespace System.Windows.Forms
{
public class HlbDataGridViewW : DataGridView
{
private GetOrSetIniKeyValue _inikey;
/// <summary>
/// Ini文件操作类
/// </summary>
[Browsable(false)]
public GetOrSetIniKeyValue inikey
{
get
{
if (_inikey == null)
{
string head = GetBastPatint(this).ToString().Replace(",", "_").Replace(":", "_");
_inikey = new GetOrSetIniKeyValue(_IniName, head);
}
return _inikey;
}
set
{
_inikey = value;
}
}
private bool _AutoSaveWidth = true;
/// <summary>
/// 是否自动保存宽度信息
/// </summary>
[DefaultValue(true)]
[Description("是否自动保存生成的列信息")]
public bool AutoSaveWidth
{
get { return _AutoSaveWidth; }
set { _AutoSaveWidth = value; }
}
/// <summary>
/// 获取最父级窗体
/// </summary>
/// <param name="ctl">传入一个控件</param>
public Control GetBastPatint(Control ctl)
{
if (ctl.Parent == null) return ctl;
return GetBastPatint(ctl.Parent);
}
private string _IniName = "GridVidewWidth.dll";
/// <summary>
/// 保存的配置文件名称
/// </summary>
[DefaultValue("GridVidewWidth.dll")]
[Description("要保存的宽度的配置信息")]
public string IniName
{
get { return _IniName; }
set
{
_IniName = value;
_inikey = null;
}
}
protected override void OnColumnAdded(DataGridViewColumnEventArgs e)
{
if (_AutoSaveWidth)
GetWidth(e.Column);
base.OnColumnAdded(e);
}
protected override void OnColumnWidthChanged(DataGridViewColumnEventArgs e)
{
if (_AutoSaveWidth)
SetWidth(e.Column.Name, e.Column.Width);
base.OnColumnWidthChanged(e);
}
private void GetWidth(DataGridViewColumn columns)
{
int width = 0;
string value = inikey[columns.Name];
if (value == "" || value == "0" || !int.TryParse(value, out width) || width == 0) return;
columns.Width = width;
}
private void SetWidth(string name, int width)
{
inikey[name] = width.ToString();
}
}
/// <summary>
/// INI设置文件帮助类
/// </summary>
public class GetOrSetIniKeyValue
{
public INIFile iniServer;
string Head;
public GetOrSetIniKeyValue(string FileName, string head)
{
iniServer = new INIFile(FileName);
iniServer.CreateFile();
this.Head = head;
}
public GetOrSetIniKeyValue(INIFile ini, string head)
{
iniServer = ini;
this.Head = head;
}
/// <summary>
///读取或设置指定头的值
/// </summary>
/// <param name="key">当前对应的键</param>
/// <returns>指定键的值</returns>
public string this[string key]
{
get { return iniServer.IniReadValue(Head, key); }
set { iniServer.IniWriteValue(Head, key, value); }
}
}
/// <summary>
/// INI文件读写类。
/// HDL
/// 2009.09.26
/// </summary>
public class INIFile
{
/// <summary>
/// 文件路径
/// </summary>
public string path;
/// <summary>
/// INI文件读写类
/// </summary>
/// <param name="INIPath">INI文件路径</param>
public INIFile(string INIPath)
{
path = Path.GetFullPath(INIPath);
}
/// <summary>
/// 文件是否存在
/// </summary>
public bool Ispath
{
get { return File.Exists(path); }
}
/// <summary>
/// 文件不存在则创建
/// </summary>
/// <returns>一个 System.IO.FileStream,它提供对 path 中指定的文件的读/写访问</returns>
public void CreateFile()
{
if (!Ispath)
{
try
{
using (File.Create(path)) { }
}
catch { }
}
}
private int _SizeWord = 1000;
/// <summary>
/// 读取或设置字节大小,默认是255字节
/// </summary>
public int SizeWord
{
get { return _SizeWord; }
set
{
if (value > 1)
_SizeWord = value;
}
}
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string defVal, Byte[] retVal, int size, string filePath);
/// <summary>
/// 写INI文件
/// </summary>
/// <param name="Section"></param>
/// <param name="Key"></param>
/// <param name="Value"></param>
public void IniWriteValue(string Section, string Key, string Value)
{
WritePrivateProfileString(Section, Key, Value, this.path);
}
/// <summary>
/// 读取INI文件
/// </summary>
/// <param name="Section"></param>
/// <param name="Key"></param>
/// <returns></returns>
public string IniReadValue(string Section, string Key)
{
StringBuilder temp = new StringBuilder(_SizeWord);
int j = GetPrivateProfileString(Section, Key, "", temp, _SizeWord, this.path);
return temp.ToString();
}
public byte[] IniReadValues(string section, string key)
{
byte[] temp = new byte[_SizeWord];
int i = GetPrivateProfileString(section, key, "", temp, _SizeWord, this.path);
return temp;
}
public List<string> ReadKeys(string sectionName)
{
byte[] buffer = new byte[5120];
int rel = GetPrivateProfileString(sectionName, null, "", buffer, buffer.GetUpperBound(0), this.path);
int iCnt, iPos;
List<string> arrayList = new List<string>();
string tmp;
if (rel > 0)
{
iCnt = 0; iPos = 0;
for (iCnt = 0; iCnt < rel; iCnt++)
{
if (buffer[iCnt] == 0x00)
{
tmp = System.Text.ASCIIEncoding.Default.GetString(buffer, iPos, iCnt - iPos).Trim();
iPos = iCnt + 1;
if (tmp != "")
arrayList.Add(tmp);
}
}
}
return arrayList;
}
/// <summary>
/// 删除ini文件下所有段落
/// </summary>
public void ClearAllSection()
{
IniWriteValue(null, null, null);
}
/// <summary>
/// 删除ini文件下personal段落下的所有键
/// </summary>
/// <param name="Section"></param>
public void ClearSection(string Section)
{
IniWriteValue(Section, null, null);
}
}
}
下载源码