RaUI/Source/RyPrint/Mod/PrintModEdit.cs

398 lines
16 KiB
C#
Raw Normal View History

2020-11-28 07:03:28 +00:00
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ryPrint.Mod;
using ryPrint;
namespace RyPrint.Mod
{
public partial class PrintModEdit : UserControl
{
public PrintModEdit()
{
InitializeComponent();
}
public bool IsDataChanged = false;
#region
public myLabel AddControl(string name, ClsProperty.En_Type _type)
{
if (GetControl(name) != null)
{
MessageBox.Show("该项已经存在", "提醒", MessageBoxButtons.OK, MessageBoxIcon.Information);
return null;
}
else
{
return AddControl(name, Enum.GetName(typeof(ClsProperty.En_Type), _type), _type, "", "");
}
}
public myLabel AddControl(string name, string title, ClsProperty.En_Type sType, string format, string value)
{
return AddControl(name, title, sType, format, value, "");
}
public myLabel AddControl(string name, string title, ClsProperty.En_Type sType, string format, string value, string DataSource)
{
IsDataChanged = true;
myLabel ct = new myLabel()
{
Size = new Size(100, 25),
BackColor = Color.Transparent,
Text = title,
Name = name,
Parent = picPreview,
Cursor = Cursors.Arrow,
Font = new Font("黑体", 10, FontStyle.Regular)
};
//ct.MouseDown += new MouseEventHandler(ct_MouseDown);
ct.KeyUp += Ct_KeyUp;
ct.KeyDown += new KeyEventHandler(Ct_KeyDown);
//ct.MouseMove += new MouseEventHandler(ct_MouseMove);
//ct.MouseUp += new MouseEventHandler(ct_MouseUp);
ct.SizeChanged += new EventHandler(Ct_SizeChanged);
ct.Click += new EventHandler(Ct_Click);
//ct.sType = sType;
//ct.title = title;
//ct.format = format;
//ct.value = value;
ct.Property.DataSource = DataSource;
ct.Property.Type = sType;
ct.Property.Title = title;
ct.Property.CustomFormat = format;
ct.Property.Value = value;
ct.Property.FontSize = ct.Font.Size;
ct.Property.Size = new SizeF(RySet.Pixeltomm(ct.Size.Width), RySet.Pixeltomm(ct.Size.Height));
ct.Property.Postion = new PointF(RySet.Pixeltomm(ct.Location.X), RySet.Pixeltomm(ct.Location.Y));
new DragControl.MoveControl(ct);
picPreview.Controls.Add(ct);
return ct;
}
private void Ct_KeyUp(object sender, KeyEventArgs e)
{
//if (e.KeyCode == Keys.Up)
//{
// isDataChanged = true;
// myLabel ct = (myLabel)sender;
// ct.Top--;
//}
//else if (e.KeyCode == Keys.Down)
//{
// isDataChanged = true;
// myLabel ct = (myLabel)sender;
// ct.Top++;
//}
//else if (e.KeyCode == Keys.Left)
//{
// isDataChanged = true;
// myLabel ct = (myLabel)sender;
// ct.Left--;
//}
//else if (e.KeyCode == Keys.Right)
//{
// isDataChanged = true;
// myLabel ct = (myLabel)sender;
// ct.Left++;
//}
}
void Ct_SizeChanged(object sender, EventArgs e)
{
IsDataChanged = true;
myLabel ct = (myLabel)sender;
LoadProperty(ct);
}
void Ct_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete)
{
IsDataChanged = true;
myLabel ct = (myLabel)sender;
picPreview.Controls.Remove(ct);
propertyGrid1.SelectedObject = null;
}
}
private myLabel GetControl(string name)
{
foreach (Control ct in picPreview.Controls)
{
if (ct.Name == name)
{
if (ct is myLabel)
{
return (myLabel)ct;
}
else
{
return null;
}
}
}
return null;
}
#endregion
private void LoadProperty(myLabel ct)
{
propertyGrid1.SelectedObject = ct.Property;
}
private void Ct_Click(object sender, EventArgs e)
{
myLabel ct = (myLabel)sender;
LoadProperty(ct);
}
#region
//private Point position;
private void PicPreview_DragEnter(object sender, DragEventArgs e)
{
//当Button被拖拽到WinForm上时候鼠标效果出现
if ((e.Data.GetDataPresent(typeof(TextBox))) || (e.Data.GetDataPresent(typeof(myLabel))))
{
e.Effect = DragDropEffects.Move;
}
}
#endregion
/// <summary>
/// 重设控件位置
/// </summary>
/// <returns></returns>
public string ResetLocationControl()
{
ryCommon.Storage tStor = new ryCommon.Storage();
foreach (Control ct1 in picPreview.Controls)
{
switch(ct1)
{
case myLabel ct:
ct.Size = new Size(RySet.MMtopixel(ct.Property.Size.Width), RySet.MMtopixel(ct.Property.Size.Height));
ct.Location = new Point(RySet.MMtopixel(ct.Property.Postion.X), RySet.MMtopixel(ct.Property.Postion.Y));
break;
}
}
return tStor.GetXMLText();
}
/// <summary>
/// 获取或设置配置信息
/// </summary>
public string Config
{
get {return SaveControl(); }
set { LoadControl(value); }
}
/// <summary>
/// 保存布局配置信息到字符串
/// </summary>
/// <returns></returns>
public string SaveControl()
{
ryCommon.Storage tStor = new ryCommon.Storage();
foreach (Control ct1 in picPreview.Controls)
{
switch (ct1)
{
case myLabel ct:
tStor.AddNode2("Name", ct.Name);
tStor.SetAttrValue("Title", ct.Property.Title);
tStor.SetAttrValue("Width", RySet.Pixeltomm(ct.Size.Width));
tStor.SetAttrValue("Height", RySet.Pixeltomm(ct.Size.Height));
tStor.SetAttrValue("FontSize", ct.Font.Size);
tStor.SetAttrValue("Left", RySet.Pixeltomm(ct.Location.X));
tStor.SetAttrValue("Top", RySet.Pixeltomm(ct.Location.Y));
//tStor.SetAttrValue("Left", ct.Location.X>picExpress.Image.Width?0:ct.Location.X);
//tStor.SetAttrValue("Top", ct.Location.Y>picExpress.Image.Height?0:ct.Location.Y);
tStor.SetAttrValue("Multiline", ct.Property.Multiline);
tStor.SetAttrValue("align", ct.AlignStr);
tStor.SetAttrValue("format", ct.Property.CustomFormat);
tStor.SetAttrValue("value", ct.Property.Value);
tStor.SetAttrValue("sType", (int)ct.Property.Type);
tStor.SetAttrValue("LineSpace", ct.Property.LineSpace);
tStor.SetAttrValue("PageCount", ct.Property.PageCount);
tStor.SetAttrValue("DataSource", ct.Property.DataSource);
tStor.SetAttrValue("NoPrint", ct.Property.NoPrint);
break;
}
}
ryCommon.Storage tStor_set = new ryCommon.Storage();
tStor_set.SelectNode2("id", "set");
tStor_set.SetAttrValue("Width",numWidth.Value);
tStor_set.SetAttrValue("Height", numHeight.Value);
tStor_set.SetAttrValue("PreviewPicPath",txtPicPath.Text);
tStor_set.SelectNode2("id", "print_xml");
tStor_set.SetAttrValue("Value", tStor.GetXMLText());
return tStor_set.GetXMLText();
}
/// <summary>
/// 保存配置到文件
/// </summary>
/// <param name="path">要保存的路径</param>
public void SaveToFile(string path)
{
if(!System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(path)))
{
System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(path));
}
System.IO.File.WriteAllText(path, SaveControl(), Encoding.UTF8);
}
/// <summary>
/// 从xml文本中加载配置
/// </summary>
/// <param name="xml"></param>
public void LoadControl(string xml)
{
ryCommon.Storage tStor_set = new ryCommon.Storage(xml);
tStor_set.SelectNode2("id", "set");
numWidth.Value = tStor_set.GetAttrValue("Width",0);
numHeight.Value = tStor_set.GetAttrValue("Height", 0);
txtPicPath.Text = tStor_set.GetAttrValue("PreviewPicPath", "");
tStor_set.SelectNode2("id","print_xml");
ryCommon.Storage tStor = new ryCommon.Storage(tStor_set.GetAttrValue("Value"));
System.Xml.XmlNodeList list = tStor.GetList();
for (int i = 0; i < list.Count; i++)
{
tStor.SelectNode3(list[i]);
myLabel ct = new myLabel()
{
Font = new Font("黑体", tStor.GetAttrValueByInt("FontSize", 10), FontStyle.Regular),
Size = new Size(RySet.MMtopixel(tStor.GetAttrValue("Width", 50f)), RySet.MMtopixel(tStor.GetAttrValue("Height", 10f)))
};
ct.Property.Multiline = tStor.GetAttrValueByBool("Multiline", false);
if (ct.Size.Height == 0) { ct.Height = 21; }
if (ct.Size.Width == 0) { ct.Width = 100; }
ct.Property.Size = new SizeF((float)tStor.GetAttrValue("Width", 50f), (float)tStor.GetAttrValue("Height", 10f));
ct.Location = new Point(RySet.MMtopixel(tStor.GetAttrValue("Left", 0f)), RySet.MMtopixel(tStor.GetAttrValue("Top", 0f)));
ct.Property.Postion = new PointF((float)tStor.GetAttrValue("Left", 0f), (float)tStor.GetAttrValue("Top", 0f));
if (ct.Location.Y < 0) { ct.Top = 0; }
if (ct.Location.X < 0) { ct.Left = 0; }
//ct.Size = new Size(tStor.GetAttrValue("Width", 50), tStor.GetAttrValue("Height", 10));
//if (ct.Size.Height == 0) { ct.Height = 21; }
//if (ct.Size.Width == 0) { ct.Width = 100; }
//ct.Property.Size = new SizeF((float)pixelTomm_Width(tStor.GetAttrValue("Width", 50)), (float)pixelTomm_Height(tStor.GetAttrValue("Height", 10)));
////ct.Location = new Point((int)pixelTomm_Width(tStor.GetAttrValue("Left", 0)), (int)pixelTomm_Height(tStor.GetAttrValue("Top", 0)));
//ct.Property.Postion = new PointF((float)pixelTomm_Width(tStor.GetAttrValue("Left", 0)), (float)pixelTomm_Height(tStor.GetAttrValue("Top", 0)));
//ct.Size = new Size(tStor.GetAttrValue("Width", 50),tStor.GetAttrValue("Height", 10));
//ct.Location = new Point(tStor.GetAttrValue("Left", 0),tStor.GetAttrValue("Top", 0));
//
ClsProperty.En_Type _type = ClsProperty.En_Type.;
try
{
_type = (ClsProperty.En_Type)tStor.GetAttrValue("sType", 0);
}
catch { }
ct.Property.Type = _type;
ct.Property.Title = tStor.GetAttrValue("Title");
ct.Property.LineSpace = tStor.GetAttrValue("LineSpace", 0d);
ct.Property.PageCount = tStor.GetAttrValue("PageCount", 0);
ct.Name = tStor.GetAttrValue("Name");
ct.Parent = picPreview;
//ct.ReadOnly = true;
ct.BackColor = Color.Transparent;
ct.Cursor = Cursors.Arrow;
ct.SetAlign(tStor.GetAttrValue("align"));
ct.Property.Value = tStor.GetAttrValue("value");
ct.Property.CustomFormat = tStor.GetAttrValue("format");
ct.Property.DataSource = tStor.GetAttrValue("DataSource");
ct.Property.NoPrint = tStor.GetAttrValue("NoPrint", false);
ct.Property.Type = _type;
ct.Refresh();
//ct.MouseDown += new MouseEventHandler(ct_MouseDown);
//ct.MouseMove += new MouseEventHandler(ct_MouseMove);
//ct.MouseUp += new MouseEventHandler(ct_MouseUp);
new DragControl.MoveControl(ct);
ct.Click += new EventHandler(Ct_Click);
ct.KeyUp += Ct_KeyUp;
ct.KeyDown += new KeyEventHandler(Ct_KeyDown);
ct.SizeChanged += new EventHandler(Ct_SizeChanged);
picPreview.Controls.Add(ct);
}
}
/// <summary>
/// 从文件中加载配置。
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public int LoadFromFile(string path)
{
if(System.IO.File.Exists(path))
{
LoadControl(System.IO.File.ReadAllText(path, ryCommon.TxtFileEncoder.GetEncoding(path)));
return 1;
}
return 0;
}
private void PropertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
{
IsDataChanged = true;
ClsProperty item = (ClsProperty)propertyGrid1.SelectedObject;
myLabel ct = GetControl(item.Name);
if (ct != null)
{
ct.Property = item;
ct.Refresh();
}
}
private void BtnBrowser_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
txtPicPath.Text = openFileDialog1.FileName.Replace(Application.StartupPath, "<app>");
}
}
private void txtPicPath_TextChanged(object sender, EventArgs e)
{
string picPath = RySet.GetTruePath(txtPicPath.Text);
if (System.IO.File.Exists(picPath))
{
Image image = Image.FromFile(picPath);
picPreview.Image = image;
numWidth.Value = Convert.ToDecimal(RySet.InchesTomm(image.Width / Convert.ToDecimal(image.HorizontalResolution)));
numHeight.Value = Convert.ToDecimal(RySet.InchesTomm(image.Height / Convert.ToDecimal(image.VerticalResolution)));
picPreview.Width = RySet.MMtopixel((float)numWidth.Value);
picPreview.Height = RySet.MMtopixel((float)numHeight.Value);
ResetLocationControl();
//picExpress.
}
else { picPreview.Image = null; }
}
/// <summary>
/// 获取新的控件名
/// </summary>
/// <returns></returns>
private string GetNewName()
{
int i = 1;
while (true)
{
if (GetControl("custom" + i.ToString()) == null)
{
return "custom" + i.ToString();
}
i++;
}
}
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
string name = GetNewName();
AddControl(name, "自定义" + name.Replace("custom", ""), ClsProperty.En_Type., "", "");
}
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
string name = GetNewName();
AddControl(name, "数据字段" + name.Replace("custom", ""), ClsProperty.En_Type., "", "");
}
private void BtnAddLabel_Click(object sender, EventArgs e)
{
Button ct = (Button)sender;
Point p = new Point(0, ct.Height);
contextMenuStrip1.Show(ct, p);
}
}
}