Werbung nur für Gäste sichtbar - Nach der Registrierung sehen sie auch keine Werbung mehr

Jenso

Administrator

  • "Jenso" is male
  • "Jenso" started this thread

Posts: 2,280

ActivityPoints: 13,730

Date of registration: Aug 15th 2010

  • Send private message

1

Sunday, August 29th 2010, 11:42pm

C# Steuerelement ImageListBox

Für ein derzeitiges Projekt brauchte ich ein neues Steuerlement, die ImageListBox.
Sie enthält Bilder vor den Listenelementen.

ImageListBox

C# Quelltext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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;
 
namespace System.Windows.Forms
{
public class ImageListBox : ListBox
{
    public ImageListBox()
    {
        this.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
        this.ItemHeight = 16;
    }
 
    public Icon getIcon(string file)
    {
        //gets an file Icon
        Icon icon = Icon.ExtractAssociatedIcon(file);
        //16x16 px convert
        Bitmap icnBmp = new Bitmap(icon.ToBitmap(), 16, 16);
        using (Graphics g = Graphics.FromImage(icnBmp))
        {
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            g.DrawImage(icon.ToBitmap(), new Rectangle(0, 0, 16, 16));
        }
        icon = Icon.FromHandle(icnBmp.GetHicon());
        return icon;
    }
    protected override void OnDrawItem(DrawItemEventArgs e)
    {
        //testing whether the item is existing or not
        if (e.Index >= this.Items.Count || e.Index <= -1) return;
 
        //item object
        ImageListBoxItem item = (ImageListBoxItem) this.Items[e.Index];
 
        //checking if item  is existing
        if (item == null) return;
 
        string text = item.ToString();
        SizeF stringSize = e.Graphics.MeasureString(text, this.Font);
        e.Graphics.DrawString(text, this.Font, new SolidBrush(Color.Black), new PointF(this.ItemHeight, e.Bounds.Y + (e.Bounds.Height - stringSize.Height) / 2));
        e.Graphics.DrawIcon(item.icon, 0, e.Bounds.Top);
 
    }
 
 
}
}

ImageListBoxItem

C# Quelltext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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;
namespace System.Windows.Forms
{
class ImageListBoxItem
{
    public Icon _icon;
    public string _text;
    public string _path;
 
    public Icon icon
    {
        get { return _icon; }
        set { _icon = value; }
    }
    public string text
    {
        get { return _text; }
        set { _text = value; }
    }
    public string path
    {
        get { return _path; }
        set { _path = value; }
    }
 
    //constructor
    public ImageListBoxItem(string text, Icon icon, string path)
    {
        _text = text;
        _icon = icon;
        _path = path;
    }
 
    //toString()-Method
    public override string ToString()
    {
        return _text;
    }
 
    //returns filepath
    public string getPath()
    {
        return _path;
    }
 
    //returns Icon
    public Icon getIcon()
    {
        return _icon;
    }
 
}
}

Elemente hinzufügen:

C# Quelltext

1
programImageListBox.Items.Add(new ImageListBoxItem(text, programImageListBox.getIcon(pfad), pfad));
Jenso has attached the following files:

Social Bookmarks


Rate this thread
Werbung nur für Gäste sichtbar - Nach der Registrierung sehen sie auch keine Werbung mehr