zoukankan      html  css  js  c++  java
  • 自绘图片下拉项 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);
    }
    }
    }

  • 相关阅读:
    C#调用WinAPI(转)
    C++升级到C#,内存数据读取问题
    锦里未成行
    创业用人九招成功法则
    彩霞满天
    特别提醒: 7种不良习惯直接影响你晋升!
    生意大展示:49种简易创业方法大比拼
    阴阳天
    受益无穷的28条职场语录
    哈佛:创业者需具备的素质及培养方法
  • 原文地址:https://www.cnblogs.com/z5337/p/3707202.html
Copyright © 2011-2022 走看看