using System;
using System.Drawing;
using System.Windows.Forms;
using GeoSigmaDrawLib;
namespace GeoSigma.SigmaDrawerStyle
{
///
/// 符号列表
///
public class MarkListBox : ListBox
{
private int padding = 2;
private int itemHeight = 24;
public event Action LineTypeSelected;
///
/// Initializes a new instance of the class.
///
public MarkListBox()
{
this.DrawMode = DrawMode.OwnerDrawFixed;
this.ItemHeight = itemHeight;
if (GeoSigmaWellPoleXY.GetCurveFillSymbolNames(out string[] names))
{
this.Items.AddRange(names);
}
}
///
protected override void OnSelectedIndexChanged(EventArgs e)
{
base.OnSelectedIndexChanged(e);
if (SelectedIndex >= 0 && SelectedIndex < Items.Count)
{
LineTypeSelected?.Invoke(SelectedItem.ToString());
}
}
///
protected override void OnDrawItem(DrawItemEventArgs e)
{
if (e.Index < 0 || e.Index >= Items.Count)
{
return;
}
string name = Items[e.Index].ToString();
Rectangle bounds = e.Bounds;
// 绘制背景(包括选中效果)
e.DrawBackground();
// 设置图标和文字区域
int iconSize = bounds.Height - (2 * padding);
Rectangle imageRect = new Rectangle(bounds.Left + padding, bounds.Top + padding, iconSize, iconSize);
Rectangle textRect = new Rectangle(imageRect.Right + padding, bounds.Top, bounds.Width - imageRect.Width - (3 * padding), bounds.Height);
// 绘制图标
using (Bitmap bitmap = GetBitmap(name, imageRect.Width, imageRect.Height))
{
if (bitmap != null)
{
e.Graphics.DrawImage(bitmap, imageRect);
}
}
// 绘制文字
TextRenderer.DrawText(
e.Graphics,
name,
this.Font,
textRect,
(e.State & DrawItemState.Selected) != 0 ? SystemColors.HighlightText : this.ForeColor,
TextFormatFlags.VerticalCenter | TextFormatFlags.Left);
// 绘制焦点框
e.DrawFocusRectangle();
}
private Bitmap GetBitmap(string name, int width, int height)
{
Bitmap bitmap = new Bitmap(width, height);
using (Graphics image = Graphics.FromImage(bitmap))
{
IntPtr hdc = image.GetHdc();
bool success = false;
try
{
success = GeoSigmaWellPoleXY.GetDrawCurveFillSymbol(name, hdc, width, height);
}
finally
{
image.ReleaseHdc(hdc);
}
if (!success)
{
bitmap.Dispose();
return null;
}
}
return bitmap;
}
}
}