using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using System.Collections;
using System.Drawing;
using System.IO;
using CnasSynchronousCommon;
using System.Xml;
namespace CNAS_DBSync
{
struct FieldLine
{
Label label;
Control editor;
Button button;
int size;
public FieldLine(Label label, Control editor, Button button = null)
{
this.label = label;
this.editor = editor;
this.button = button;
int count = 0;
if (label != null) count++;
if (editor != null) count++;
if (button != null) count++;
size = count;
}
public Label Label
{
get
{
return label;
}
}
public Control Editor
{
get
{
return editor;
}
}
public Button Button
{
get
{
return button;
}
}
}
///
/// 设置容器内编辑字段位置的公共算法
///
class FormSizeHelper
{
private static readonly string strLocalConfigPath = FileHelper.getBasePath() + @"\Data\";
private static readonly string FormSizeXml = "FormSize.xml";
private static readonly FormSizeHelper instance;
private Hashtable formSizes;
// 静态构造函数确保线程安全且延迟加载。
static FormSizeHelper() {
instance = new FormSizeHelper();
}
// 防止外部实例化。
private FormSizeHelper() {
Init();
}
public static FormSizeHelper Instance => instance; // 提供全局访问点。
public static void Arrange(Panel container, FieldLine[] fields, int span1 = 1, int span2 = 1)
{
int colLabel = 0;
int colEditor = 0;
int colButton = 0;
int lbLeft = -1;
for (int i = 0; i < fields.Length; i++)
{
if (lbLeft == -1)
{
if (fields[i].Label != null)
lbLeft = fields[i].Label.Left;
}
else if (fields[i].Label != null && fields[i].Label.Left >= 0 && fields[i].Label.Left < lbLeft)
{
lbLeft = fields[i].Label.Left;
}
}
int width = lbLeft < 0 ? (container.Width * 80 / 100) : (container.Width-lbLeft*2);
int marginLeft = (container.Width - width) / 2;
for (int i=0;i< fields.Length; i++)
{
if(fields[i].Label != null && fields[i].Label.Width > colLabel)
{
colLabel = fields[i].Label.Width;
}
if (fields[i].Button != null && fields[i].Button.Width > colButton)
{
colButton = fields[i].Button.Width;
}
}
colEditor = width - (colLabel + span1 + span2 + colButton);
for (int i = 0; i < fields.Length; i++)
{
//Label不动
//改变Editor
if (fields[i].Editor != null)
{
if(fields[i].Editor is TextBox)
{
TextBox tb = (TextBox)fields[i].Editor;
//tb.Left = marginLeft + colLabel + span1;
tb.Width = colEditor;
if(fields[i].Button != null)
{
fields[i].Button.Left = tb.Left + colEditor + span2;
}
}
if(fields[i].Editor is CheckBox)
{
//不动
}
if(fields[i].Editor is ComboBox)
{
ComboBox tb = (ComboBox)fields[i].Editor;
//tb.Left = marginLeft + colLabel + span1;
tb.Width = colEditor;
if (fields[i].Button != null)
{
fields[i].Button.Left = tb.Left + colEditor + span2;
}
}
}
}
}
private static bool ValidBound(Form form, int x, int y, int w, int h)
{
if (x>=0 && y >= 0 && w>0 && h > 0)
{
Screen screen = Screen.FromControl(form);
Rectangle r = screen.Bounds;
return (x + w < r.Width && y + h < r.Height);
}
return false;
}
private void Init()
{
formSizes = new Hashtable(10);
string strUrl = strLocalConfigPath + FormSizeXml;
try
{
if (!File.Exists(strUrl)) return;
XmlDocument doc = new XmlDocument();
doc.Load(strUrl);
XmlNode root = doc.DocumentElement;
foreach (XmlNode node in root.ChildNodes)
{
string formname = node.Name;
string value = node.InnerText;
formSizes.Add(formname, value);
}
}
catch (Exception ex)
{
AppLog.Error(ex.Message);
}
}
public List LoadDefaultSize(Form form, string key, out bool changed)
{
changed = false;
if (formSizes.ContainsKey(key))
{
string value = formSizes[key] as string;
string[] values = value.Split(',');
if (values.Length >= 4)
{
int x, y, w, h;
if (int.TryParse(values[0], out x) && int.TryParse(values[1], out y) &&
int.TryParse(values[2], out w) && int.TryParse(values[3], out h))
{
if (ValidBound(form, x, y, w, h))
{
form.Left = x;
form.Top = y;
form.Width = w;
form.Height = h;
changed = true;
}
}
if (changed && values.Length > 4)
{
List list = new List(values.Length - 4);
for (int i = 4; i < values.Length; i++)
{
list.Add(values[i]);
}
return list;
}
}
}
return null;
}
public bool SaveDefaultSize(string key, string value)
{
if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(value))
{
formSizes[key] = value;
StringBuilder sb = new StringBuilder();
sb.Append("\n\n");
foreach(object k in formSizes.Keys)
{
string line = string.Format("<{0}>{1}{0}>", k, formSizes[k]);
sb.Append(line).Append("\n");
}
sb.Append("");
try
{
bool bIfSuccess = FileHelper.SaveLocalFile(strLocalConfigPath, FormSizeXml, sb.ToString());
return bIfSuccess;
}
catch (Exception ex)
{
AppLog.Error(ex.Message);
}
}
return true;
}
}
}