2

私は質問を修正するために多くの時間を費やしました。私は長い間、1 つの画像と画像の下にいくつかのテキストを描画しようとしました。私のコードの何が問題なのですか。エラーが表示されません。私のカスタム制御コードは次のとおりです。

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

ListBox領収書にwithImageと Textが必要です。

敬具、

ドルチェ

4

1 に答える 1

0

親クラスのテキストのドローアブルを使用する場合は、このテキストをクリアするためbase.OnDrawItem(e);、最後に移動します。OnDrawItemそして、おそらく、「DrawMode.OwnerDrawFixed」を「DrawMode.Normal」にオフにするか、画像を描画した後に手動で文字列を描画する必要があります(のようにg.DrawString(GetText(),...)

于 2012-06-18T11:41:36.583 に答える