using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace ryControls
{
public class TabList : ListView
{
private ColumnHeader columnHeader1;
public TabList() :
base()
{
this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
this.OwnerDraw = true;//用于启用重绘
this.View = View.Tile;
this.TileSize = new Size(80,28);
this.Scrollable = false;
this.MultiSelect = false;
}
///
/// 图标
///
public Image Icon { get; set; }
///
/// 重绘图标
///
public bool IsDrawIcon { get; set; }
///
/// 重绘网格线
///
public bool IsDrawGridLines { get; set; }
private int last_selectIndex = 0;
protected override void OnMouseUp(MouseEventArgs e)
{
var pt = this.PointToClient(e.Location);
var item = this.GetItemAt(pt.X, pt.Y);
if (item == null)
{
if (last_selectIndex < this.Items.Count)
{
this.Items[last_selectIndex].Selected = true;
}
}
}
protected override void OnItemSelectionChanged(ListViewItemSelectionChangedEventArgs e)
{
if(e.IsSelected)
{
last_selectIndex = e.ItemIndex;
base.OnItemSelectionChanged(e);
}
else
{
if ( last_selectIndex < this.Items.Count)
{
//this.Items[last_selectIndex].Selected = true;
}
}
}
protected override void OnSelectedIndexChanged(System.EventArgs e)
{
if(this.SelectedItems.Count>0)
{
base.OnSelectedIndexChanged(e);
}
else
{
}
}
protected override void OnDrawItem(DrawListViewItemEventArgs e)
{
Rectangle r = e.Bounds;
Graphics g = e.Graphics;
DrawSelectedBackground(e, g, r);
//if (IsDrawGridLines)
//{
// using (Pen pen = new Pen(Color.Gray))
// {
// g.DrawRectangle(pen, r.X, r.Y, r.Width, r.Height + 1);//高度加1使横向线条重叠
// }
//}
if (!string.IsNullOrEmpty(e.Item.Text))
{
this.DrawText(e, g, r);
}
}
private Color _SelectedBackColor = Color.LightSkyBlue;
[Description("选择项的背景颜色")]
public Color SelectedBackColor
{
get
{
return _SelectedBackColor;
}
set
{
_SelectedBackColor = value;
this.Refresh();
}
}
///
/// 重绘选中时背景
///
private void DrawSelectedBackground(DrawListViewItemEventArgs e, Graphics g, Rectangle r)
{
if (e.Item.Selected)
{
using (SolidBrush brush = new SolidBrush(_SelectedBackColor))
{
g.FillRectangle(brush, r);
}
}
}
///
/// 重绘图标
///
private Size DrawIcon(Graphics g, Rectangle r, Image image, Color backColor)
{
Rectangle imageBounds = new Rectangle(new Point(r.X+10, r.Y+3), new Size(64,64));
if (r.Width > r.Height-20)
{
imageBounds.Width = r.Height-20;
imageBounds.Height = r.Height - 20;
}
else
{
imageBounds.Width = r.Width - 20;
imageBounds.Height = r.Width - 20;
}
imageBounds.X = r.X + (r.Width - imageBounds.Width) / 2;
//使图标不会紧贴着每一列的左上角
//imageBounds.X += 1;
//imageBounds.Y += 1;
g.DrawImage(image, imageBounds);
return imageBounds.Size;
}
///
/// 重绘文本
///
private void DrawText(DrawListViewItemEventArgs e, Graphics g, Rectangle r)
{
TextFormatFlags flags = GetFormatFlags(HorizontalAlignment.Center);
TextRenderer.DrawText(
g,
e.Item.Text,
e.Item.Font,
r,
e.Item.ForeColor,
flags);
}
///
/// 获取文本对齐
///
private TextFormatFlags GetFormatFlags(
HorizontalAlignment align)
{
TextFormatFlags flags =
TextFormatFlags.EndEllipsis |
TextFormatFlags.VerticalCenter;
switch (align)
{
case HorizontalAlignment.Center:
flags |= TextFormatFlags.HorizontalCenter;
break;
case HorizontalAlignment.Right:
flags |= TextFormatFlags.Right;
break;
case HorizontalAlignment.Left:
flags |= TextFormatFlags.Left;
break;
}
return flags;
}
private void InitializeComponent()
{
this.columnHeader1 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.SuspendLayout();
//
// columnHeader1
//
this.columnHeader1.Width = 10;
//
// IconViewEx
//
this.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.columnHeader1});
this.LabelWrap = false;
this.ShowItemToolTips = true;
this.TileSize = new System.Drawing.Size(60, 60);
this.View = System.Windows.Forms.View.Tile;
this.ResumeLayout(false);
}
}
}