• 自绘图片下拉项 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);
    }
    }
    }

  • 相关阅读:
    根据字典中值得大小,对字典中的项排序
    统计序列中元素出现的频度
    为元组中的每个元素命名,提高程序可读性
    CentOS7 zabbix服务 简单安装文档
    使用脚本打印杨辉三角
    python 内置函数
    图片爬取实战一
    logstash的各个场景应用(配置文件均已实践过)
    kettle添加hadoop cluster时报错Caused by: java.lang.IllegalArgumentException: Does not contain a valid host:port authority: hadoop:password@node56:9000
    Jenkins+maven+Tomcat+SVN一键自动打包部署应用到服务器
  • 原文地址:https://www.cnblogs.com/z5337/p/3707202.html
Copyright © 2020-2023  润新知