VSoft/Source/VSoft_Dll/IconViewEx.cs
鑫Intel 800c766a67 ### 2020-12-27 dev更新
#### VSoft    V1.0.2012.2701
- *.[新增]新增支持通过双击鼠标中键、侧键的方式来显示主界面。
- *.[改进]当多次最小化后,不再显示最小化通知。
- *.[改进]改进栏目和分类控件皮肤。
2020-12-27 17:04:42 +08:00

188 lines
5.7 KiB
C#

using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace ryControls
{
public class IconViewEx : ListView
{
private ColumnHeader columnHeader1;
public IconViewEx() :
base()
{
// 开启双缓冲
this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
this.OwnerDraw = true;//用于启用重绘
this.View = View.Tile;
this.TileSize = new Size(100,100);
this.MultiSelect = false;
}
/// <summary>
/// 图标
/// </summary>
public Image Icon { get; set; }
/// <summary>
/// 重绘图标
/// </summary>
public bool IsDrawIcon { get; set; }
/// <summary>
/// 重绘网格线
/// </summary>
public bool IsDrawGridLines { get; set; }
protected override void OnDrawItem(DrawListViewItemEventArgs e)
{
Rectangle r = e.Bounds;
Graphics g = e.Graphics;
DrawSelectedBackground(e, g, r);
int paddingHeight = 0;
if (IsDrawIcon)
{
var soft = (VSoft.Prams.SoftInfo)e.Item.Tag;
if (soft == null || soft.Image == null)
{ paddingHeight = this.DrawIcon(g, r, this.Icon, e.Item.BackColor).Height; }
else
{
paddingHeight = this.DrawIcon(g, r, soft.Image, e.Item.BackColor).Height;
}
}
//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, paddingHeight);
}
}
private Color _SelectedBackColor = Color.LightYellow;
[Description("选择项的背景颜色")]
public Color SelectedBackColor
{
get
{
return _SelectedBackColor;
}
set
{
_SelectedBackColor = value;
this.Refresh();
}
}
/// <summary>
/// 重绘选中时背景
/// </summary>
private void DrawSelectedBackground(DrawListViewItemEventArgs e, Graphics g, Rectangle r)
{
if (e.Item.Selected)
{
using (SolidBrush brush = new SolidBrush(_SelectedBackColor))
{
g.FillRectangle(brush, r);
}
}
}
/// <summary>
/// 重绘图标
/// </summary>
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;
}
/// <summary>
/// 重绘文本
/// </summary>
private void DrawText(DrawListViewItemEventArgs e, Graphics g, Rectangle r, int paddingHeight)
{
TextFormatFlags flags = GetFormatFlags(HorizontalAlignment.Center);
var r1 = new Rectangle(r.Location, r.Size)
{
Y = r.Y + paddingHeight + 2,//重绘图标时,文本右移
Height = r.Height - paddingHeight - 2
};
TextRenderer.DrawText(
g,
e.Item.Text,
e.Item.Font,
r1,
e.Item.ForeColor,
flags);
}
/// <summary>
/// 获取文本对齐
/// </summary>
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);
}
}
}