首页 / 更多教程 / winform下重画ListBox
winform下重画ListBox
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了winform下重画ListBox,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含8178字,纯文字阅读大概需要12分钟。
内容图文
![winform下重画ListBox](/upload/InfoBanner/zyjiaocheng/1264/b6bb979b22814e0ebda81a8eda1e4da3.jpg)
Windows Forms是由Win32 API封装的开发组件,最初是为了替代mfc,但却没有体现与Model View Controller架构对应的特色,进而在.net framework 3.0中推出了wpf,富控件数据显示方面,利用模板功能轻松实现。
在winform下要想自定义一些用户控件,就需要运用的2D绘画类。下图我们为ListBox重新排列了数据显示方式,并为每一个item加入了删除按钮。
首先我们设计一个承载数据的类ListBoxItem。
1 public class ListBoxItem : IDisposable 2 { 3 public Guid Id { get; set; } 4 5publicstring Name { get; set; } 6 7publicstring IP { get; set; } 8 9publicstring Mac { get; set; } 1011 [System.ComponentModel.DefaultValue(typeof(System.Drawing.Image), "null")] 12public System.Drawing.Image Image { get; set; } 1314publicbool IsFocus { get; set; } 1516public ListBoxItem() { } 1718public ListBoxItem(Guid id, string name, string ip, string mac, System.Drawing.Image image) 19 { 20this.Id = id; 21this.Name = name; 22this.IP = ip; 23this.Mac = mac; 24this.Image = image; 25this.IsFocus = false; 26 } 2728publicvoid Dispose() 29 { 30this.Image = null; 31 } 32 }
然后我们再为ListBox写一个用于展现数据的数据源ListBoxItemCollection,这里实现了迭代和集合操作接口,可以根据需要扩展数据操作方法。
[System.ComponentModel.ListBindable(false)] publicclass ListBoxItemCollection : IList, ICollection, IEnumerable { private UserListBox m_owner; public ListBoxItemCollection(UserListBox owner) { this.m_owner = owner; } internal UserListBox Owner { get { returnthis.m_owner; } } #region override public ListBoxItem this[int index] { get { return Owner.OldItemSource[index] as ListBoxItem; } set { Owner.OldItemSource[index] = value; } } publicint Count { get { return Owner.OldItemSource.Count; } } publicbool IsReadOnly { get { return Owner.OldItemSource.IsReadOnly; } } publicint Add(ListBoxItem item) { if (item == null) { thrownew ArgumentException("item is null"); } return Owner.OldItemSource.Add(item); } publicvoid AddRange(ListBoxItem[] items) { Owner.OldItemSource.AddRange(items); } publicvoid Clear() { if (Owner.OldItemSource.Count > 0) { Owner.OldItemSource.Clear(); } } publicbool Contains(ListBoxItem item) { bool rst = false; foreach (ListBoxItem oldItem in Owner.OldItemSource) { if (oldItem.Id == item.Id) { rst = true; break; } } return rst; } publicvoid CopyTo(ListBoxItem[] destination, int arrayIndex) { Owner.OldItemSource.CopyTo(destination, arrayIndex); } publicint IndexOf(ListBoxItem item) { return Owner.OldItemSource.IndexOf(item); } publicvoid Insert(int index, ListBoxItem item) { if (item == null) { thrownew ArgumentException("item is null"); } Owner.OldItemSource.Insert(index, item); } publicvoid Remove(ListBoxItem item) { Owner.OldItemSource.Remove(item); } publicvoid RemoveAt(int index) { Owner.OldItemSource.RemoveAt(index); } public IEnumerator GetEnumerator() { return Owner.OldItemSource.GetEnumerator(); } int IList.Add(object value) { if (!(value is ListBoxItem)) { thrownew ArgumentException(); } return Add(value as ListBoxItem); } void IList.Clear() { Clear(); } bool IList.Contains(object value) { return Contains(value as ListBoxItem); } int IList.IndexOf(object value) { return IndexOf(value as ListBoxItem); } void IList.Insert(int index, object value) { if (!(value is ListBoxItem)) { thrownew ArgumentException(); } Insert(index, value as ListBoxItem); } bool IList.IsFixedSize { get { returnfalse; } } bool IList.IsReadOnly { get { return IsReadOnly; } } void IList.Remove(object value) { Remove(value as ListBoxItem); } void IList.RemoveAt(int index) { RemoveAt(index); } object IList.this[int index] { get { returnthis[index]; } set { if (!(value is ListBoxItem)) { thrownew ArgumentException(); } this[index] = value as ListBoxItem; } } void ICollection.CopyTo(Array array, int index) { CopyTo((ListBoxItem[])array, index); } int ICollection.Count { get { return Count; } } bool ICollection.IsSynchronized { get { returnfalse; } } object ICollection.SyncRoot { get { returnfalse; } } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } #endregion#region extention public ListBoxItem FindByMac(string mac) { foreach (ListBoxItem item in Owner.OldItemSource) { if (item.Mac == mac) { return item; } } returnnull; } #endregion }
下面可以为工程new一个新项——自定义控件,命名为UserListBox。
这里有几个地方要说明一下,首先在默认构造函数里面的参数:
DrawMode.OwnerDrawVariable启用控件重绘功能。
DoubleBuffer开启后避免复杂绘画造成窗体闪烁,这个缓冲的原理是将绘画操作放在内存里操作,完成后才会复制到图形界面上,进而避免的闪烁。
OnPaint进行了重写,这个方法是根据pc屏幕分辨率刷新频率来执行的,会不断的重复执行,进而持久化图形界面。
Invalidate方法,会立即刷新UI。
Item上的按钮事件,是通过ListBox的click事件,取到鼠标的在界面上的定位,调用相对应的方法。
1 public partial class UserListBox : ListBox 2 { 3 public ListBoxItem mouseItem; 4 private ListBoxItemCollection m_Items; 5 6 public UserListBox() : base() 7 { 8 InitializeComponent(); 9 10 m_Items = new ListBoxItemCollection(this); 11 12base.DrawMode = DrawMode.OwnerDrawVariable; 13this.SetStyle(ControlStyles.UserPaint, true); 14this.SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲 15this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true); // 双缓冲 16this.SetStyle(ControlStyles.ResizeRedraw, true); // 调整大小时重绘 17this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景. 18this.SetStyle(ControlStyles.SupportsTransparentBackColor, true); // 开启控件透明 19 } 20 21publicnew ListBoxItemCollection Items 22 { 23get { return m_Items; } 24 } 25 26internal ListBox.ObjectCollection OldItemSource 27 { 28get { returnbase.Items; } 29 } 30 31protectedoverridevoid OnPaint(PaintEventArgs e) 32 { 33 Graphics g = e.Graphics; 34 35// you can set SeletedItem background 36if (this.Focused && this.SelectedItem != null) 37 { 38 } 39 40for (int i = 0; i < Items.Count; i++) 41 { 42 Rectangle bounds = this.GetItemRectangle(i); 43 44if (mouseItem == Items[i]) 45 { 46 Color leftColor = Color.FromArgb(200, 192, 224, 248); 47using (SolidBrush brush = new SolidBrush(leftColor)) 48 { 49 g.FillRectangle(brush, new Rectangle(bounds.X, bounds.Y, bounds.Width, bounds.Height)); 50 } 51 52 Color rightColor = Color.FromArgb(252, 233, 161); 53using (SolidBrush brush = new SolidBrush(rightColor)) 54 { 55 g.FillRectangle(brush, new Rectangle(bounds.Width - 40, bounds.Y, 40, bounds.Height)); 56 } 57 } 58 59int fontLeft = bounds.Left + 40 + 15; 60 System.Drawing.Font font = new System.Drawing.Font("微软雅黑", 9); 61 g.DrawString(Items[i].Name, font, new SolidBrush(this.ForeColor), fontLeft, bounds.Top + 5); 62 g.DrawString(Items[i].IP, font, new SolidBrush(Color.FromArgb(128, 128, 128)), fontLeft, bounds.Top + 20); 63 g.DrawString(Items[i].Mac, font, new SolidBrush(Color.FromArgb(128, 128, 128)), fontLeft, bounds.Top + 35); 64 65if (Items[i].Image != null) 66 { 67 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear; 68 g.DrawImage(Items[i].Image, new Rectangle(bounds.X + 5, (bounds.Height - 40) / 2 + bounds.Top, 40, 40)); 69 } 70 g.DrawImage(Properties.Resources.error, new Rectangle(bounds.Width - 28, (bounds.Height - 16) / 2 + bounds.Top, 16, 16)); 71 } 72base.OnPaint(e); 73 } 74 75protectedoverridevoid OnMeasureItem(MeasureItemEventArgs e) 76 { 77base.OnMeasureItem(e); 78if (Items.Count > 0) 79 { 80 ListBoxItem item = Items[e.Index]; 81 e.ItemHeight = 54; 82 } 83 84 } 85 86protectedoverridevoid OnSelectedIndexChanged(EventArgs e) 87 { 88base.OnSelectedIndexChanged(e); 89 } 90 91protectedoverridevoid OnMouseMove(MouseEventArgs e) 92 { 93base.OnMouseMove(e); 94for (int i = 0; i < Items.Count; i++) 95 { 96 Rectangle bounds = this.GetItemRectangle(i); 97 Rectangle deleteBounds = new Rectangle(bounds.Width - 28, (bounds.Height - 16) / 2 + bounds.Top, 16, 16); 98 99if (bounds.Contains(e.X, e.Y)) 100 { 101if (Items[i] != mouseItem) 102 { 103 mouseItem = Items[i]; 104 } 105106if (deleteBounds.Contains(e.X, e.Y)) 107 { 108 mouseItem.IsFocus = true; 109this.Cursor = Cursors.Hand; 110 } 111else112 { 113 mouseItem.IsFocus = false; 114this.Cursor = Cursors.Arrow; 115 } 116117this.Invalidate(); 118break; 119 } 120 } 121 } 122123protectedoverridevoid OnMouseClick(MouseEventArgs e) 124 { 125base.OnMouseClick(e); 126if (mouseItem.IsFocus) 127 { 128 ListBoxItem deleteItem = mouseItem; 129if(MessageBox.Show("confirm to delete", "", MessageBoxButtons.OKCancel) == DialogResult.OK) 130 { 131this.Items.Remove(deleteItem); 132 } 133 } 134 } 135136protectedoverridevoid OnMouseLeave(EventArgs e) 137 { 138base.OnMouseLeave(e); 139this.mouseItem = null; 140this.Invalidate(); 141 } 142 }
原文:http://www.cnblogs.com/yuefei/p/4062998.html
内容总结
以上是互联网集市为您收集整理的winform下重画ListBox全部内容,希望文章能够帮你解决winform下重画ListBox所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。