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);
    }
    }
    }

  • 相关阅读:
    QT自定义控件插件化简要概述
    wildfly9 配置SSL单向认证/https
    wildfly-9.0.2 web项目部署详细步骤
    SQL Server 2008 数据库日志文件丢失处理方法
    win7 64位系统 pl/sql 无法解析指定的连接标识符解决办法
    mybatis 应用参考
    去除浏览器下jquey easyui datagrid、combotree 缓存问题
    java 页面url传值中文乱码的解决方法
    jasperreports-5.6 + jaspersoftstudio-5.6 生成pdf 文件中文无法正常显示问题
    HTML5实现在线抓拍
  • 原文地址:https://www.cnblogs.com/z5337/p/3707202.html
Copyright © 2011-2022 走看看