2

我花了很多时间来解决我的问题。我已经尝试了很长时间在图像下方绘制一张图像和一些文字。我的代码有什么问题。我看不到错误。我的自定义控制代码是:

using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;


namespace ImageListBox
{
    /// <summary>
    /// ImageListBox
    /// </summary>
    public partial class ImageListBox : ListBox
    {
        private Brush _brushText;
        private string _TextMessage;

        public ImageListBox():base()
        {
            DrawMode = DrawMode.OwnerDrawFixed;
        }

        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            const int iWidth = 800;
            const int iHeight = 800;
            PointF ptfCenter = new PointF(iWidth / 2, iHeight / 2);

            base.OnDrawItem(e);
            if (e.Index > -1 && Items.Count > e.Index)
            {
                object itm = Items[e.Index];
                if (itm != null)
                {
                    string fileName = itm.ToString();
                    if (File.Exists(fileName))
                    {
                        Image img = null;
                        try
                        {
                            img = Image.FromFile(fileName);
                            {
                                _brushText = Brushes.Black;
                                Font font = new Font("Arial", 12);
                                Graphics g = e.Graphics;
                                Image img2 = GetThumbnail(img, e.Bounds);
                                //g.DrawString(_TextMessage,font,_brushText,0,img2.Height);
                                g.DrawImageUnscaled(img2, e.Bounds);

                            }
                        }
                        catch (Exception ex)
                        {
                            Graphics g = e.Graphics;
                            var sf = new StringFormat {Trimming = StringTrimming.EllipsisCharacter};
                            string message = string.Format("{0} konnte nicht geladen werden. Exception: {1}", fileName,
                                                           ex);
                            g.DrawString(message, Font, new SolidBrush(ForeColor), e.Bounds, sf);
                        }    
                    }

                }
            }
        }

        private Image GetThumbnail(Image pImage, Rectangle pBounds)
        {
            Font font = new Font("Arial", 12);
            Image ret = new Bitmap(pBounds.Width, pBounds.Height);
            Graphics g = Graphics.FromImage(ret);
            g.FillRectangle(new SolidBrush(Color.White), 0, 0, ret.Width, ret.Height * 10);

            float factor = Math.Max(pImage.Width/(float) pBounds.Width, pImage.Height/(float) pBounds.Height);
            g.DrawImage(pImage, 0, 0, pImage.Width/factor, pImage.Height/factor);
            g.Dispose();
            //g.DrawString("This is a Test a TEST!! ",font, _brushText, 0, pImage.Height);

            return ret;
        }

        public string GetText
        {
            get { return _TextMessage; }
            set { _TextMessage = value; }
        }
    }
}

表格中的测试代码是:

namespace WindowsFormsApplication1
{
    public partial class RibbonForm1 : DevComponents.DotNetBar.Office2007RibbonForm
    {
        public RibbonForm1()
        {
            InitializeComponent();
            //Höhe der Items festlegen (die Breite floatet)
            this.imageListBox1.ItemHeight=200;
            //imageListBox1.GetText = "   Test auf verfügbarkeit   "; // Doesn't work

            //imageListBox1.Items.Add("----------------------------"); // Doesn't work
            //imageListBox1.Items.Add("   Test auf verfügbarkeit   "); // Doesn't work
            //imageListBox1.Items.Add("----------------------------"); // Doesn't work

            string windir = System.Environment.GetEnvironmentVariable("SystemRoot");
            if (windir != null)
            {
                string[] files = System.IO.Directory.GetFiles(windir, "*.bmp");
                foreach (string file in files)
                {
                    imageListBox1.Items.Add(file);

                    //imageListBox1.Items.Add("Text"); // Doesn't work
                }
            }
        }
    }
}

我的收据需要ListBoxwithImage和 Text。

亲切的问候,

甜蜜的

4

1 回答 1

0

如果您想使用父类中的可绘制文本,请移至base.OnDrawItem(e);末尾,OnDrawItem因为您清除了此文本。而且,也许,您需要在绘制图像后将“DrawMode.OwnerDrawFixed”关闭为“DrawMode.Normal”或手动绘制字符串(如g.DrawString(GetText(),...)

于 2012-06-18T11:41:36.583 回答