自绘图片下拉项 combobox listbox
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);
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!