You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

205 lines
5.8 KiB
C#

1 month ago
using GeoSigmaDrawLib;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace StyleLibManager
{
/// <summary>
/// 用户应用指定样式
/// </summary>
/// <param name="group">样式组</param>
/// <param name="style">样式</param>
public delegate void ApplyStyleHandler(string group, string style);
/// <summary>
/// 样式管理界面类
/// </summary>
public partial class FrmStyleMain : Form
{
/// <summary>
/// 用户点击应用或确定
/// </summary>
public event ApplyStyleHandler ApplyStyle;
private Dictionary<string, Bitmap> cache = new Dictionary<string, Bitmap>();
private readonly ImageList imageList = new ImageList()
{
ColorDepth = ColorDepth.Depth24Bit,
ImageSize = new Size(50, 50)
};
/// <summary>
/// 当前选中的组,如果什么都没选中,返回 null
/// </summary>
public string CurrentGroup
{
get
{
return StyleGroupTreeView.SelectedNode?.Text;
}
}
/// <summary>
/// 当前选中的样式,如果什么都没选中,返回 null
/// </summary>
public string CurrentStyle
{
get
{
if (StyleListView.SelectedItems.Count > 0)
{
return StyleListView.SelectedItems[0].Text;
}
return null;
}
}
/// <summary>
/// 构造函数
/// </summary>
public FrmStyleMain()
{
InitializeComponent();
InitStyleListView();
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="path">库路径</param>
/// <exception cref="ArgumentNullException">空指针异常geo 或 path 为 null</exception>
/// <exception cref="IOException">IO异常初始化样式库失败通常是找不到目录或没有访问权限</exception>
public FrmStyleMain(string path)
{
InitializeComponent();
InitStyleListView();
StyleGroupTreeView.AfterSelect += StyleGroupTreeView_AfterSelect;
if (string.IsNullOrWhiteSpace(path))
{
throw new ArgumentNullException(nameof(path));
}
if (!GeoSigmaXY.StyleLibraryInit(path, true))
{
throw new IOException("初始化样式库失败");
}
var nodes = GeoSigmaXY.StyleLibraryListGroup()
.Select(it => new TreeNode(it))
.ToArray();
StyleGroupTreeView.Nodes.AddRange(nodes);
}
private void StyleGroupTreeView_AfterSelect(object sender, TreeViewEventArgs e)
{
if (e.Node == null)
{
return;
}
StyleListView.BeginUpdate();
try
{
StyleListView.Clear();
StyleListView.LargeImageList.Images.Clear();
string group = e.Node.Text;
string[] styles = GeoSigmaXY.StyleLibraryListStyle(group);
for (int i = 0; i < styles.Length; i++)
{
AddStyleItem(group, styles, i);
}
}
finally
{
StyleListView.EndUpdate();
}
}
private void AddStyleItem(string group, string[] styles, int i)
{
string style = styles[i];
string key = CacheKey(group, style);
if (!cache.ContainsKey(key))
{
cache[key] = CreateBitmap(group, style);
}
StyleListView.LargeImageList.Images.Add(cache[key]);
StyleListView.Items.Add(new ListViewItem(style, i));
}
private static string CacheKey(string group, string style)
{
return Path.Combine(group, style).ToString();
}
private Bitmap CreateBitmap(string group, string style)
{
int width = 50;
int height = 50;
Bitmap bmp = new Bitmap(width, height);
using (Graphics gBmp = Graphics.FromImage(bmp))
{
IntPtr pBmpDC = gBmp.GetHdc();
try
{
GeoSigmaXY.StyleLibraryRenderStyle(group, style, pBmpDC, width, height);
}
finally
{
gBmp.ReleaseHdc();
}
}
return bmp;
}
private void InitStyleListView()
{
StyleListView.LargeImageList = imageList;
StyleListView.View = View.LargeIcon;
StyleListView.HideSelection = false;
//StyleListView.VirtualMode = true;
StyleListView.MultiSelect = false;
StyleListView.LabelEdit = true;
}
private void OkButton_Click(object sender, EventArgs e)
{
if (CurrentGroup != null && CurrentStyle != null)
{
ApplyStyle?.Invoke(CurrentGroup, CurrentStyle);
}
DialogResult = DialogResult.OK;
Close();
}
private void CancelButton_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
private void ApplyButton_Click(object sender, EventArgs e)
{
if (CurrentGroup != null && CurrentStyle != null)
{
ApplyStyle?.Invoke(CurrentGroup, CurrentStyle);
}
}
}
}