old_flsystem/应用/TBAppraisalTools/Forms/MainForm.cs

450 lines
19 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using UI.Framework.Forms;
using TBAppraisalTools.Properties;
using TBAppraisalTools.;
using System.IO;
using Api.Framework.Tools;
using Newtonsoft.Json;
using CsharpHttpHelper;
using System.Text.RegularExpressions;
using UI.Framework.Tools;
using static TBAppraisalTools.Enums;
using Newtonsoft.Json.Linq;
namespace TBAppraisalTools.Forms
{
public partial class MainForm : BaseForm
{
//拾取框对象
private PickBox pickBox = new PickBox();
public MainForm()
{
InitializeComponent();
this.Text = Resources.MainFormTitle;
}
/// <summary>
/// 关闭窗口
/// </summary>
internal void CloseForm()
{
try
{
if (!this.IsDisposed)
{
this.Invoke(new Action(delegate ()
{
this.Close();
this.Dispose();
}));
}
}
catch (Exception ex)
{
}
}
/// <summary>
/// 窗体加载
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MainForm_Load(object sender, EventArgs e)
{
try
{
settingControl1.Bind(Class1.Config);
TemplateLoad();
}
catch (Exception ex)
{
ShowError(ex.Message);
}
}
//优惠券设计样板加载
private void TemplateLoad()
{
myPanel.Controls.Clear();
try
{
if (!File.Exists(TbTools.TBCONFIGJSON))
{
File.WriteAllText(TbTools.TBCONFIGJSON, "{\"panel1\":{\"C_Name\":\"panel1\",\"C_Point\":\"11, 12\",\"C_Size\":\"387, 555\",\"C_Tag\":{\"ImagePath\":\"D:\\\\1\\\\2\\\\3\\\\File\\\\评价工具缓存\\\\优惠券背景.png\",\"Shape\":true,\"Varible\":\"#背景\"},\"T_Text\":null,\"T_Font\":null,\"T_ForeColor\":\"\"},\"picbox2\":{\"C_Name\":\"picbox0\",\"C_Point\":\"13, 316\",\"C_Size\":\"121, 148\",\"C_Tag\":{\"ImagePath\":\"D:\\\\1\\\\2\\\\3\\\\File\\\\评价工具缓存\\\\商品主图.png\",\"Shape\":true,\"Varible\":\"#商品主图\"},\"T_Text\":null,\"T_Font\":null,\"T_ForeColor\":\"\"},\"picbox3\":{\"C_Name\":\"picbox1\",\"C_Point\":\"159, 55\",\"C_Size\":\"80, 80\",\"C_Tag\":{\"ImagePath\":\"D:\\\\1\\\\2\\\\3\\\\File\\\\评价工具缓存\\\\店铺头像.png\",\"Shape\":false,\"Varible\":\"#店铺头像\"},\"T_Text\":null,\"T_Font\":null,\"T_ForeColor\":\"\"},\"label4\":{\"C_Name\":\"label0\",\"C_Point\":\"248, 412\",\"C_Size\":\"62, 15\",\"C_Tag\":\"\",\"T_Text\":\"#券后特价\",\"T_Font\":\"Microsoft Sans Serif, 9pt\",\"T_ForeColor\":\"64, 64, 64\"},\"label5\":{\"C_Name\":\"label1\",\"C_Point\":\"220, 384\",\"C_Size\":\"62, 15\",\"C_Tag\":\"\",\"T_Text\":\"#商品现价\",\"T_Font\":\"Microsoft Sans Serif, 9pt\",\"T_ForeColor\":\"64, 64, 64\"},\"label6\":{\"C_Name\":\"label2\",\"C_Point\":\"248, 356\",\"C_Size\":\"62, 15\",\"C_Tag\":\"\",\"T_Text\":\"#月销售量\",\"T_Font\":\"Microsoft Sans Serif, 9pt\",\"T_ForeColor\":\"64, 64, 64\"},\"label7\":{\"C_Name\":\"label3\",\"C_Point\":\"156, 306\",\"C_Size\":\"62, 15\",\"C_Tag\":\"\",\"T_Text\":\"#商品标题\",\"T_Font\":\"Microsoft Sans Serif, 9pt\",\"T_ForeColor\":\"64, 64, 64\"},\"label8\":{\"C_Name\":\"label4\",\"C_Point\":\"80, 251\",\"C_Size\":\"127, 15\",\"C_Tag\":\"\",\"T_Text\":\"#开始时间 - #结束时间\",\"T_Font\":\"Microsoft Sans Serif, 9pt\",\"T_ForeColor\":\"64, 64, 64\"},\"label9\":{\"C_Name\":\"label5\",\"C_Point\":\"136, 199\",\"C_Size\":\"62, 15\",\"C_Tag\":\"\",\"T_Text\":\"#优惠券价\",\"T_Font\":\"Microsoft Sans Serif, 9pt\",\"T_ForeColor\":\"64, 64, 64\"},\"label10\":{\"C_Name\":\"label6\",\"C_Point\":\"166, 141\",\"C_Size\":\"62, 15\",\"C_Tag\":\"\",\"T_Text\":\"#店铺名称\",\"T_Font\":\"Microsoft Sans Serif, 9pt\",\"T_ForeColor\":\"64, 64, 64\"}}");
}
if (File.Exists(TbTools.TBCONFIGJSON))//存在优惠券模板的情况
{
var objDic = JsonConvert.DeserializeObject<Dictionary<string, TbConfig>>(File.ReadAllText(TbTools.TBCONFIGJSON));
if (objDic != null)
{
var panel = objDic.FirstOrDefault(f => f.Key.Contains("panel"));
if (!default(KeyValuePair<string, object>).Equals(panel))
{
TbConfig tbConfig = objDic[panel.Key] as TbConfig;
if (!File.Exists(TbTools.BACKICO))
{
using (var bitmap = Resources.)
{
bitmap.Save(TbTools.BACKICO);
}
}
if (!File.Exists(TbTools.ITEMICO))
{
using (var bitmap = Resources.)
{
bitmap.Save(TbTools.ITEMICO);
}
}
if (!File.Exists(TbTools.SHOPICO))
{
using (var bitmap = Resources.)
{
bitmap.Save(TbTools.SHOPICO);
}
}
myPanel.Name = "panel1";
myPanel.Location = tbConfig.C_Point;
myPanel.Size = tbConfig.C_Size;
myPanel.TabIndex = 1;
myPanel.BackgroundImage = Tools.ReadImageFile(Tools.RefreshFileAddress(TbTools.BACKICO, TbTools.BASEPATH + @"\", Util.MapPath(TbTools.BASEPATH)));
myPanel.BackgroundImageLayout = ImageLayout.Stretch;
myPanel.Tag = tbConfig.C_Tag;
}
var picboxs = objDic.Where(f => f.Key.Contains("picbox")).ToList();
int i = 0;
foreach (var item in picboxs)
{
AddImage(item.Value, i);
i++;
}
int j = 0;
var labels = objDic.Where(f => f.Key.Contains("label")).ToList();
foreach (var item in labels)
{
AddText(item.Value, j);
j++;
}
}
}
else
{
throw new Exception("对不起不存在优惠券模板");
}
}
catch (Exception ex)
{
ShowError(ex);
}
}
#region ,
/// <summary>
/// 增加图片控件
/// </summary>
/// <param name="config">控件的基础信息</param>
/// <param name="i">第几个图片控件(用于名字使用)</param>
private void AddImage(TbConfig config, int i)
{
try
{
var pic = new MyPictureBox();
ImageInfo imageInfo = FindImageInfoByTag(config.C_Tag);
var backImage = imageInfo.ImagePath;
if (imageInfo.Varible.Contains(EditImageType..ToString()))
backImage = Tools.RefreshFileAddress(backImage, TbTools.OTHERSICOPATH + @"\", Util.MapPath(TbTools.OTHERSICOPATH));
else
backImage = Tools.RefreshFileAddress(backImage, TbTools.BASEPATH + @"\", Util.MapPath(TbTools.BASEPATH));
var bitmapImage = Tools.ReadImageFile(backImage);
pic.BackgroundImage = imageInfo.Shape ? bitmapImage : Tools.CutEllipse(bitmapImage, new Rectangle(0, 0, bitmapImage.Width, bitmapImage.Height), new Size(500, 500));
pic.Location = config.C_Point;
pic.Size = config.C_Size;
pic.Tag = config.C_Tag;
pic.Name = "picbox" + i;
pic.BackColor = Color.Transparent;
pic.SizeMode = PictureBoxSizeMode.Zoom;
pic.BackgroundImageLayout = ImageLayout.Stretch;
pic.TabStop = false;
pic.WaitOnLoad = true;
pic.MouseEnter += MouseEnter;
pic.ContextMenuStrip = this.menuStrip_Photo;
myPanel.Controls.Add(pic);
pickBox.WireControl(pic);
}
catch (Exception ex)
{
ShowError(ex);
}
}
/// <summary>
/// 增加文字控件
/// </summary>
private void AddText(TbConfig config, int i)
{
try
{
var label = new Label();
label.Font = config.T_Font;
label.ForeColor = config.T_ForeColor;
label.Location = config.C_Point;
label.AutoSize = false;
label.TextAlign = ContentAlignment.MiddleCenter;
label.Size = config.C_Size;
label.Name = "label" + i;
label.BackColor = Color.Transparent;
label.AutoSize = true;
label.Text = config.T_Text;
label.Tag = config.C_Tag ?? string.Empty;
label.ContextMenuStrip = this.menuStrip_Text;
label.MouseEnter += MouseEnter;
myPanel.Controls.Add(label);
pickBox.WireControl(label);
}
catch (Exception ex)
{
ShowError(ex);
}
}
#endregion
private void button1_Click(object sender, EventArgs e)
{
try
{
int seed = 1;
var tbConfigDic = new Dictionary<string, TbConfig>();//将优惠券模板控件的信息保存至字典中
var myPanelSize = myPanel.Size;//获取背景图的大小
tbConfigDic.Add($"panel{seed}", new TbConfig() { C_Name = myPanel.Name, C_Size = myPanel.Size, C_Point = myPanel.Location, C_Tag = myPanel.Tag });//保存底图信息至字典中
#region
foreach (Control item in myPanel.Controls)
{
seed++;
var tbConfig = new TbConfig();
#region
tbConfig.C_Name = item.Name;
tbConfig.C_Size = item.Size;//(图片|文字)的大小
tbConfig.C_Point = item.Location;//(图片|文字)的坐标
tbConfig.C_Tag = item.Tag;
#endregion
if (item.GetType() == typeof(Label))//文字信息
{
var label = item as Label;
tbConfig.T_Text = label.Text;
tbConfig.T_ForeColor = label.ForeColor;
tbConfig.T_Font = label.Font;
tbConfigDic.Add($"label{seed}", tbConfig);
}
else if (item.GetType() == typeof(MyPictureBox))//图片信息
tbConfigDic.Add($"picbox{seed}", tbConfig);
}
#endregion
//File.WriteAllText(TbTools.TBCONFIGJSON, CsharpHttpHelper.HttpHelper.ObjectToJson(tbConfigDic));//将字典保存为json,再将json保存至文件中
File.WriteAllText(TbTools.TBCONFIGJSON, JsonConvert.SerializeObject(tbConfigDic));//将字典保存为json,再将json保存至文件中
ShowSuccess("保存成功!");
}
catch (Exception ex)
{
ShowError(ex);
}
}
/// <summary>
/// 登录淘宝按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
try
{
var tbLoginListForm = new TbLoginListForm();
tbLoginListForm.ShowDialog();
}
catch (Exception ex)
{
ShowError(ex);
}
}
#region ,
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
var photoForm = new EditPhotoForm();
if (photoForm.ShowDialog() == DialogResult.OK)
{
var tbConfig = photoForm.tbConfig;
AddImage(tbConfig, myPanel.Controls.Count + 1);
}
}
catch (Exception ex)
{
ShowError(ex);
}
}
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
var textForm = new EditTextForm();
if (textForm.ShowDialog() == DialogResult.OK)
{
var tbConfig = textForm.tbConfig;
AddText(tbConfig, myPanel.Controls.Count + 1);
}
}
catch (Exception ex)
{
ShowError(ex);
}
}
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
OpenFileDialog fdlg = new OpenFileDialog();
fdlg.Title = "选择要上传的图片";
fdlg.InitialDirectory = @"c:\";
fdlg.Filter = "jpg,png|*.jpg;*.png;";
fdlg.FilterIndex = 1;
fdlg.RestoreDirectory = true;
if (fdlg.ShowDialog() == DialogResult.OK)
{
string oldImagePath = TbTools.BACKICO;
if (!string.IsNullOrWhiteSpace(oldImagePath))
File.Delete(oldImagePath);
File.Copy(fdlg.FileName, TbTools.BACKICO);
myPanel.BackgroundImage = Tools.ReadImageFile(TbTools.BACKICO);
myPanel.Tag = new ImageInfo() { ImagePath = TbTools.BACKICO, Shape = true, Varible = "#底图" };
}
fdlg.Dispose();
}
catch (Exception ex)
{
ShowError(ex);
}
}
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
var pic = (MyPictureBox)_sender;
var photoForm = new EditPhotoForm(new TbConfig() { C_Tag = FindImageInfoByTag(pic.Tag), C_Size = pic.Size, C_Point = pic.Location });
if (photoForm.ShowDialog() == DialogResult.OK)
{
AddImage(photoForm.tbConfig, myPanel.Controls.Count + 1);
pickBox.RemoveSelect();
myPanel.Controls.Remove(pic);
}
}
catch (Exception ex)
{
ShowError(ex);
}
}
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
pickBox.RemoveSelect();
myPanel.Controls.Remove((Control)_sender);
}
catch (Exception ex)
{
ShowError(ex);
}
}
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
var label = (Label)_sender;
var textForm = new EditTextForm(new TbConfig() { C_Point = label.Location, C_Size = label.Size, T_Font = label.Font, T_ForeColor = label.ForeColor, T_Text = label.Text });
if (textForm.ShowDialog() == DialogResult.OK)
{
AddText(textForm.tbConfig, myPanel.Controls.Count + 1);
pickBox.RemoveSelect();
myPanel.Controls.Remove(label);
}
}
catch (Exception ex)
{
ShowError(ex);
}
}
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
pickBox.RemoveSelect();
myPanel.Controls.Remove((Control)_sender);
}
catch (Exception ex)
{
ShowError(ex);
}
}
#endregion
#region
/// <summary>
/// 鼠标进入事件,将获取到的控件信息保存起来,在删除控件的时候用
/// </summary>
private object _sender = null;
/// <summary>
/// 鼠标进去控件时发生,这方法针对所有的要增加修改操作的控件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private new void MouseEnter(object sender, EventArgs e)//当鼠标移到pictuBox内获取焦点
{
this._sender = sender;
}
#endregion
/// <summary>
/// 将图片控件中的Tag内容,转成ImageInfo对象
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
private ImageInfo FindImageInfoByTag(object obj)
{
ImageInfo imageInfo = null;
if (obj.GetType() == typeof(JObject))//这是因为配置文件中json转出来之后还没完全转完毕.
{
var imageInfoJson = HttpExtend.JsonToDictionary(obj.ToString().Replace("{{", "{").Replace("}}", "}"));
imageInfo = new ImageInfo() { ImagePath = imageInfoJson["ImagePath"].ToString(), Varible = imageInfoJson["Varible"].ToString(), Shape = imageInfoJson["Shape"].ToString().ToLower() == "true" };
}
else//这里是用户在设计模板的时候,添加修改操作时.正常的类型
imageInfo = obj as ImageInfo;
return imageInfo;
}
}
}