public static string TransferXml(string str)
{
if (string.IsNullOrEmpty(str))
{
return str;
}
return str.Replace("&", "&")
.Replace(">", ">")
.Replace("<", "<")
.Replace("'", "'")
.Replace("\"", """);
}
static List<Row> GetFormRows(Form form, out List<VListTable> tables)
{
List<Row> rows = new List<Row>();
tables = new List<VListTable>();
Dictionary<ColumnHeader, string> cols = GetFormTypes<ColumnHeader>(form);
foreach (Control c in form.Controls)
{
IControl ctrl = null;
Label label = c as Label;
if (label != null)
{
VLabel lbl = new VLabel();
lbl.id = c.Name;
lbl.width = AdjustWidth(Pt2Px(c.Width));
lbl.text = c.Text;
lbl.visible = label.Visible;
lbl.height = c.Height;
lbl.align = label.TextAlign.ToString().ToLower().Substring(3);
if (c.Font != null)
{
if (c.Font.Size > 9)
{
lbl.fontSize = Convert.ToInt32(Pt2Px(c.Font.Size));
}
}
if (c.ForeColor != System.Drawing.SystemColors.ControlText)
{
lbl.color = string.Format("#{0:x2}{1:x2}{2:x2}", c.ForeColor.R, c.ForeColor.G, c.ForeColor.B);
}
ctrl = lbl;
}
TextBox txt = c as TextBox;
if (txt != null)
{
VInput input = new VInput();
input.id = c.Name;
input.text = c.Text;
input.width = AdjustWidth(Pt2Px(c.Width));
input.visible = txt.Visible;
input.type = "text";
input.enabled = c.Enabled;
ctrl = input;
}
NumericUpDown num = c as NumericUpDown;
if (num != null)
{
VInput input = new VInput();
input.id = c.Name;
input.text = c.Text;
input.width = AdjustWidth(Pt2Px(c.Width));
input.visible = num.Visible;
input.type = "number";
input.props = new Dictionary<string, string> {
{ "max", num.Maximum.ToString() },
{ "min", num.Minimum.ToString() }
};
input.enabled = c.Enabled;
ctrl = input;
}
ComboBox drop = c as ComboBox;
#region Dropdown
if (drop != null)
{
VSelect select = new VSelect();
select.enabled = drop.Enabled;
select.id = drop.Name;
select.width = AdjustWidth(Pt2Px(c.Width));
select.items = new Dictionary<string, string>();
select.visible = drop.Visible;
if (drop.Items != null && drop.Items.Count>0)
{
Tuple<PropertyInfo,PropertyInfo> props = null;
if (!string.IsNullOrEmpty(drop.ValueMember)
&& !string.IsNullOrEmpty(drop.DisplayMember))
{
props = GetItemProp(drop.Items[0], drop.ValueMember, drop.DisplayMember);
}
foreach (object item in drop.Items)
{
if (item == null)
{
continue;
}
if (props == null)
{
string key = item.ToString();
if (!select.items.ContainsKey(key))
{
select.items.Add(key, key);
}
}
else
{
object obj = props.Item1.GetValue(item,null);
if (obj == null) continue;
string key = obj.ToString();
if (!select.items.ContainsKey(key))
{
obj = props.Item2.GetValue(item, null);
select.items.Add(key, obj == null?"":obj.ToString());
}
}
}
}
ctrl = select;
}
#endregion
Button btn = c as Button;
if (btn != null)
{
VButton button = new VButton();
button.id = c.Name;
button.text = c.Text;
button.enabled = c.Enabled;
button.width = AdjustWidth(Pt2Px(c.Width));
button.visible = btn.Visible;
ctrl = button;
}
CheckBox cbx = c as CheckBox;
if (cbx != null)
{
VCheckBox checkbox = new VCheckBox();
checkbox.id = c.Name;
checkbox.width = AdjustWidth(Pt2Px(c.Width));
checkbox.visible = cbx.Visible;
checkbox.enabled = c.Enabled;
checkbox.selected = cbx.Checked;
checkbox.text = cbx.Text;
ctrl = checkbox;
}
RadioButton rbtn = c as RadioButton;
if (rbtn != null)
{
VCheckBox checkbox = new VCheckBox();
checkbox.id = c.Name;
checkbox.width = AdjustWidth(Pt2Px(c.Width));
checkbox.visible = rbtn.Visible;
checkbox.enabled = c.Enabled;
checkbox.selected = rbtn.Checked;
checkbox.text = rbtn.Text;
ctrl = checkbox;
}
ListView view = c as ListView;
if (view != null)
{
VListTable table = new VListTable();
table.id = c.Name;
table.height = c.Height;
table.width = AdjustWidth(Pt2Px(c.Width));
table.visible = view.Visible;
table.Columns = new List<VListTableColumn>();
#region Column
if (view.Columns != null)
{
foreach (ColumnHeader col in view.Columns)
{
if (cols.ContainsKey(col))
{
table.Columns.Add(new VListTableColumn()
{
text = col.Text,
align = col.TextAlign.ToString().ToLower(),
width = AdjustWidth(Pt2Px(col.Width)),
id = cols[col],
visible = col.Width > 0
});
}
}
}
#endregion
ctrl = table;
tables.Add(table);
}
if (ctrl != null)
{
Point location = GetAbsoluteLocation(c);
ctrl.x1 = location.X;
ctrl.x2 = location.X + c.Width;
ctrl.y1 = location.Y;
ctrl.y2 = location.Y + c.Height;
Row row = GetRow(rows, ctrl);
if (!row.hasTable && ctrl as VListTable != null)
{
row.hasTable = true;
}
row.controls.Add(ctrl);
}
}
rows.RemoveAll(r => r.controls.Count == 1 && (r.controls[0].id == "label_TitleLine" || ((r.controls[0] as VLabel) != null && (r.controls[0] as VLabel).height == 1)));
rows.Sort(RowComparer.Instance);
return rows;
}
static float AdjustWidth(float width)
{
if (width < 100)
{
int a = Convert.ToInt32(width / 10);
return a * 10 + (width - a > 5 ? 10 : 5);
}
else
{
return Convert.ToInt32(Math.Ceiling(width));
}
}
static string GetFormPrefix(string name)
{
if (string.IsNullOrEmpty(name)) return string.Empty;
StringBuilder result = new StringBuilder();
if (name.IndexOf('_') > -1)
{
foreach (string str in name.Split('_'))
{
result.Append(GetFormPrefix(str));
}
}
else
{
result.Append(char.ToUpper(name[0]));
for (int i = 1, j = name.Length; i < j; i++)
{
if (char.IsUpper(name[i]))
{
result.Append(name[i]);
}
}
}
return result.ToString();
}
/// <summary>
/// 磅转像素
/// </summary>
/// <param name="pt">磅</param>
/// <returns></returns>
static float Pt2Px(float pt)
{
//px * 96/72
return pt * 4 / 3;
}
static Dictionary<T,string> GetFormTypes<T>(Form form)
{
Type dstType = typeof(T);
return form.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly)
.Where(f => f.FieldType == dstType)
.ToDictionary(f => (T)f.GetValue(form), f => f.Name);
}
static Tuple<PropertyInfo, PropertyInfo> GetItemProp(object item,
string keyName,
string valueName)
{
if (item == null || string.IsNullOrEmpty(keyName)
|| string.IsNullOrEmpty(valueName))
{
return null;
}
Type type = item.GetType();
if (type == typeof(string))
{
return null;
}
PropertyInfo keyProp = type.GetProperty(keyName, BindingFlags.Public | BindingFlags.Instance);
PropertyInfo valueProp = type.GetProperty(valueName, BindingFlags.Public | BindingFlags.Instance);
if (keyProp != null && keyProp.CanRead && valueProp != null && valueProp.CanRead)
{
return Tuple<PropertyInfo, PropertyInfo>.Create(keyProp, valueProp);
}
return null;
}
static Row GetRow(List<Row> rows,IControl control)
{
Row result = null;
foreach (Row row in rows)
{
float center = (float)(row.y1 + row.y2) / 2;
if ((control.y1 < center && control.y2 > center)
|| (control.y1 >= row.y1 && control.y2 <= row.y2))
{
result = row;
break;
}
}
if (result == null)
{
result = new Row();
result.y1 = control.y1;
result.y2 = control.y2;
result.no = rows.Count + 1;
rows.Add(result);
}
else
{
if (result.y1 > control.y1)
{
result.y1 = control.y1;
}
if (result.y2 < control.y2)
{
result.y2 = control.y2;
}
}
return result;
}
static Point GetAbsoluteLocation(Control control)
{
int x = control.Location.X;
int y = control.Location.Y;
Control parent = control.Parent;
while (parent != null && (parent as Form) == null)
{
x += parent.Location.X;
y += parent.Location.Y;
parent = parent.Parent;
}
return new Point(x,y);
}
05-26
2887
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交