自绘图片下拉项 combobox listbox

时间:2024-08-12 22:04:51

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Study_MyAlbumEditor
{
public partial class Form1 : Form
{
//Dictionary<int, string> PA = new Dictionary<int, string>();
List<string> PA = new List<string>();

private Rectangle _drawRect = new Rectangle(0, 0, 45, 45);

private SolidBrush _textBrush = new SolidBrush(SystemColors.WindowText);

public Form1()
{
InitializeComponent();
// 1.
//int i = 0;
//foreach (string str in Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)))
//{
// PA.Add(i, str);
// i++;
//}
//BindingSource bs = new BindingSource();
//bs.DataSource = PA;
//listBox1.DataSource = bs;
////listBox1.DataSource = PA;
//listBox1.DisplayMember = "Value";

// 2.
PA.AddRange(Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)));
listBox1.DataSource = PA;

comboBox1.DataSource = PA;
}

private void thumbToolStripMenuItem_Click(object sender, EventArgs e)
{
thumbToolStripMenuItem.Checked = !thumbToolStripMenuItem.Checked;
if (thumbToolStripMenuItem.Checked)
{
listBox1.DrawMode = DrawMode.OwnerDrawVariable;
comboBox1.DrawMode = DrawMode.OwnerDrawVariable;
}
else
{
listBox1.DrawMode = DrawMode.Normal;
listBox1.ItemHeight = 16;
comboBox1.DrawMode = DrawMode.Normal;
comboBox1.ItemHeight = 16;
}
}

private void listBox1_MeasureItem(object sender, MeasureItemEventArgs e)
{
e.ItemHeight = 45 + 2;
e.ItemWidth = 45 + 2 + (int)e.Graphics.MeasureString(listBox1.Items[e.Index].ToString(), listBox1.Font).Width;

}

private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
Graphics g = e.Graphics;
Rectangle imageRect = e.Bounds;
imageRect.Y += 1;
imageRect.Height = 45;
imageRect.X += 1;
imageRect.Width = 45;

Rectangle textRect = new Rectangle(imageRect.Right + 2, imageRect.Y + ((imageRect.Height - e.Font.Height) / 2), e.Bounds.Width - imageRect.Width - 4, e.Font.Height);
Rectangle fillRect = e.Bounds;
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
{
_textBrush.Color = SystemColors.Highlight;
//g.FillRectangle(_textBrush, textRect);
g.FillRectangle(_textBrush, fillRect);
_textBrush.Color = SystemColors.HighlightText;
}
else
{
_textBrush.Color = SystemColors.Window;
//g.FillRectangle(_textBrush, textRect);
g.FillRectangle(_textBrush, fillRect);
_textBrush.Color = SystemColors.WindowText;
}

Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback);
try
{
g.DrawImage(Image.FromFile(listBox1.Items[e.Index].ToString()).GetThumbnailImage(45, 45, myCallback, IntPtr.Zero), imageRect);
g.DrawRectangle(Pens.Gray, imageRect);
}
catch (Exception)
{
g.DrawRectangle(Pens.Blue, imageRect);
}

g.DrawString(listBox1.Items[e.Index].ToString(), e.Font, _textBrush, textRect);
}

public bool ThumbnailCallback()
{
return false;
}

private void comboBox1_MeasureItem(object sender, MeasureItemEventArgs e)
{
e.ItemHeight = 45 + 2;
e.ItemWidth = 45 + 2 + (int)e.Graphics.MeasureString(comboBox1.Items[e.Index].ToString(), comboBox1.Font).Width;// listBox1.Items[e.Index].ToString(), listBox1.Font).Width;
}

private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
Graphics g = e.Graphics;
Rectangle imageRect = e.Bounds;
imageRect.Y += 1;
imageRect.Height = 45;
imageRect.X += 1;
imageRect.Width = 45;

Rectangle textRect = new Rectangle(imageRect.Right + 2, imageRect.Y + ((imageRect.Height - e.Font.Height) / 2), e.Bounds.Width - imageRect.Width - 4, e.Font.Height);
Rectangle fillRect = e.Bounds;
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
{
_textBrush.Color = SystemColors.Highlight;
//g.FillRectangle(_textBrush, textRect);
g.FillRectangle(_textBrush, fillRect);
_textBrush.Color = SystemColors.HighlightText;
}
else
{
_textBrush.Color = SystemColors.Window;
//g.FillRectangle(_textBrush, textRect);
g.FillRectangle(_textBrush, fillRect);
_textBrush.Color = SystemColors.WindowText;
}

Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback);
try
{
g.DrawImage(Image.FromFile(comboBox1.Items[e.Index].ToString()).GetThumbnailImage(45, 45, myCallback, IntPtr.Zero), imageRect);
g.DrawRectangle(Pens.Gray, imageRect);
}
catch (Exception)
{
g.DrawRectangle(Pens.Blue, imageRect);
}

g.DrawString(comboBox1.Items[e.Index].ToString(), e.Font, _textBrush, textRect);
}
}
}