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
{
///
/// 用户应用指定样式
///
/// 样式组
/// 样式
public delegate void ApplyStyleHandler(string group, string style);
///
/// 样式管理界面类
///
public partial class FrmStyleMain : Form
{
///
/// 用户点击应用或确定
///
public event ApplyStyleHandler ApplyStyle;
private Dictionary cache = new Dictionary();
private readonly ImageList imageList = new ImageList()
{
ColorDepth = ColorDepth.Depth24Bit,
ImageSize = new Size(50, 50)
};
///
/// 当前选中的组,如果什么都没选中,返回 null
///
public string CurrentGroup
{
get
{
return StyleGroupTreeView.SelectedNode?.Text;
}
}
///
/// 当前选中的样式,如果什么都没选中,返回 null
///
public string CurrentStyle
{
get
{
if (StyleListView.SelectedItems.Count > 0)
{
return StyleListView.SelectedItems[0].Text;
}
return null;
}
}
///
/// 构造函数
///
public FrmStyleMain()
{
InitializeComponent();
InitStyleListView();
}
///
/// 构造函数
///
/// 库路径
/// 空指针异常,geo 或 path 为 null
/// IO异常,初始化样式库失败,通常是找不到目录或没有访问权限
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);
}
}
}
}