WINFORM的请问一下如何做这样的LISTview 本帖最后由 csdsuper 于 2010-06-24 10:44:56 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 一样可以设置;你的这个东西,不用listview,自己写个控件也很简单;只是没有时间帮你写而已 我发现ListVew有点像 就是像它的List模式下的方式,但是它的选中颜色怎么改呢,它的边框色怎么改呢 控件代码using System.Collections.Generic;using System.Drawing;using System.Windows.Forms;namespace QiQi.Windows.Forms{ public class QiQiListBox : Control { private readonly List<QiQiListBoxItem> _items; private Color _border_color; private int _item_height; private Color _selected_back_color; private int _selected_index; public QiQiListBox() { SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true); _items = new List<QiQiListBoxItem>(); _item_height = 24; _selected_index = -1; _border_color = Color.FromArgb(245, 170, 114); _selected_back_color = Color.FromArgb(255, 237, 209); } public Color BorderColor { get { return _border_color; } set { _border_color = value; } } public Color SelectedBackColor { get { return _selected_back_color; } set { _selected_back_color = value; } } public int SelectedIndex { get { return _selected_index; } set { _selected_index = value; } } public int ItemHeight { get { return _item_height; } set { _item_height = value; } } public void insert_item(QiQiListBoxItem item, int index) { _items.Insert(index, item); } public void delete_item(int index) { _items.RemoveAt(index); } protected override void OnPaintBackground(PaintEventArgs pevent) { base.OnPaintBackground(pevent); //边框 using (var pen = new Pen(_border_color)) { Rectangle rect = DisplayRectangle; rect.Width -= 1; rect.Height -= 1; pevent.Graphics.DrawRectangle(pen, rect); } } protected override void OnPaint(PaintEventArgs e) { for (int i = 0; i < _items.Count; i++) { draw_item(e.Graphics, i, i == _selected_index); } } private void draw_item(Graphics g, int index, bool selected) { QiQiListBoxItem item = _items[index]; Rectangle rect = get_item_rect(index); using (Brush bru = new SolidBrush(selected ? _selected_back_color : BackColor)) { g.FillRectangle(bru, rect); } //图标 g.DrawImage(item.Image, 1, 1 + index*_item_height, 24, 24); Rectangle rect_text = rect; rect_text.Width -= 24; rect_text.X += 24 + 10; var sf = new StringFormat(); sf.LineAlignment = StringAlignment.Center; g.DrawString(item.Text, Font, Brushes.Black, rect_text, sf); } private Rectangle get_item_rect(int index) { return new Rectangle(2, index*_item_height + 2, Width - 4, _item_height); } public int hit_test(int x, int y) { for (int i = 0; i < _items.Count; i++) { if (get_item_rect(i).Contains(x, y)) return i; } return -1; } protected override void OnMouseDown(MouseEventArgs e) { base.OnMouseDown(e); int index = hit_test(e.X, e.Y); Graphics g = CreateGraphics(); if (_selected_index != -1) draw_item(g, _selected_index, false); if (index != -1) { _selected_index = index; draw_item(g, index, true); } } } public class QiQiListBoxItem { public Image Image { get; set; } public string Text { get; set; } public object Tag { get; set; } }} 调用代码 list.insert_item(new QiQiListBoxItem { Image = Image.FromFile(@"f:\\temp\\i1.png"), Text = "智能秘书", }, 0); list.insert_item(new QiQiListBoxItem { Image = Image.FromFile(@"f:\\temp\\i2.png"), Text = "信息订阅", }, 0); list.insert_item(new QiQiListBoxItem { Image = Image.FromFile(@"f:\\temp\\i3.png"), Text = "空间信息", }, 0); list.Invalidate();100分 拿出来 你把它改成从 ListBox 继承的。 两个方法效率差别很大,求解 js单线程异步 各位高手麻烦帮我看看这段代码是干什么用的,目的是什么,帮我分析下 这种结构的xml文件怎样得到 谁有.NET所有的类库集合教程!!!就是找不到。。。郁闷 高分提问!!!!!!!! 水晶报表打印Oracle中照片问题。 ############哪位大侠能提供ListView实现复杂表头的文章或代码 WinForm中绑定ComboBox数据出错:无法绑定到新的显示成员! 排样算法 怎么做? 请看这个C#下实现动态系统托盘图标程序!显示结果是什么? 求C#写个程序好的思路! 测试
你的这个东西,不用listview,自己写个控件也很简单;只是没有时间帮你写而已
using System.Drawing;
using System.Windows.Forms;namespace QiQi.Windows.Forms
{
public class QiQiListBox : Control
{
private readonly List<QiQiListBoxItem> _items;
private Color _border_color;
private int _item_height; private Color _selected_back_color;
private int _selected_index; public QiQiListBox()
{
SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
_items = new List<QiQiListBoxItem>();
_item_height = 24;
_selected_index = -1;
_border_color = Color.FromArgb(245, 170, 114);
_selected_back_color = Color.FromArgb(255, 237, 209);
} public Color BorderColor
{
get { return _border_color; }
set { _border_color = value; }
} public Color SelectedBackColor
{
get { return _selected_back_color; }
set { _selected_back_color = value; }
} public int SelectedIndex
{
get { return _selected_index; }
set { _selected_index = value; }
} public int ItemHeight
{
get { return _item_height; }
set { _item_height = value; }
} public void insert_item(QiQiListBoxItem item, int index)
{
_items.Insert(index, item);
} public void delete_item(int index)
{
_items.RemoveAt(index);
} protected override void OnPaintBackground(PaintEventArgs pevent)
{
base.OnPaintBackground(pevent);
//边框
using (var pen = new Pen(_border_color))
{
Rectangle rect = DisplayRectangle;
rect.Width -= 1;
rect.Height -= 1;
pevent.Graphics.DrawRectangle(pen, rect);
}
} protected override void OnPaint(PaintEventArgs e)
{
for (int i = 0; i < _items.Count; i++)
{
draw_item(e.Graphics, i, i == _selected_index);
}
} private void draw_item(Graphics g, int index, bool selected)
{
QiQiListBoxItem item = _items[index];
Rectangle rect = get_item_rect(index);
using (Brush bru = new SolidBrush(selected ? _selected_back_color : BackColor))
{
g.FillRectangle(bru, rect);
}
//图标
g.DrawImage(item.Image, 1, 1 + index*_item_height, 24, 24);
Rectangle rect_text = rect;
rect_text.Width -= 24;
rect_text.X += 24 + 10;
var sf = new StringFormat();
sf.LineAlignment = StringAlignment.Center;
g.DrawString(item.Text, Font, Brushes.Black, rect_text, sf);
} private Rectangle get_item_rect(int index)
{
return new Rectangle(2, index*_item_height + 2, Width - 4, _item_height);
} public int hit_test(int x, int y)
{
for (int i = 0; i < _items.Count; i++)
{
if (get_item_rect(i).Contains(x, y))
return i;
}
return -1;
} protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
int index = hit_test(e.X, e.Y);
Graphics g = CreateGraphics();
if (_selected_index != -1)
draw_item(g, _selected_index, false);
if (index != -1)
{
_selected_index = index;
draw_item(g, index, true);
}
}
} public class QiQiListBoxItem
{
public Image Image { get; set; }
public string Text { get; set; }
public object Tag { get; set; }
}
}
{
Image = Image.FromFile(@"f:\\temp\\i1.png"),
Text = "智能秘书",
}, 0);
list.insert_item(new QiQiListBoxItem
{
Image = Image.FromFile(@"f:\\temp\\i2.png"),
Text = "信息订阅",
}, 0);
list.insert_item(new QiQiListBoxItem
{
Image = Image.FromFile(@"f:\\temp\\i3.png"),
Text = "空间信息",
}, 0);
list.Invalidate();100分 拿出来