* UIListBox: 增加DrawItem和Demo

This commit is contained in:
Sunny 2021-10-04 23:57:12 +08:00
parent 8abaf9bbdc
commit 47c717dd02
5 changed files with 32 additions and 27 deletions

Binary file not shown.

Binary file not shown.

View File

@ -77,6 +77,7 @@ namespace Sunny.UI.Demo
this.uiListBox1.Size = new System.Drawing.Size(270, 343);
this.uiListBox1.TabIndex = 27;
this.uiListBox1.Text = "uiListBox1";
this.uiListBox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.ListBox_DrawItem);
this.uiListBox1.ItemDoubleClick += new System.EventHandler(this.uiListBox1_ItemDoubleClick);
//
// uiLine2
@ -118,7 +119,7 @@ namespace Sunny.UI.Demo
this.uiImageListBox1.TextAlignment = System.Drawing.ContentAlignment.MiddleCenter;
this.uiImageListBox1.ItemDoubleClick += new System.EventHandler(this.uiImageListBox1_ItemDoubleClick);
//
// FListBox1
// FListBox
//
this.AllowShowTitle = true;
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
@ -129,7 +130,7 @@ namespace Sunny.UI.Demo
this.Controls.Add(this.uiLine2);
this.Controls.Add(this.uiLine1);
this.Controls.Add(this.uiImageListBox1);
this.Name = "FListBox1";
this.Name = "FListBox";
this.Padding = new System.Windows.Forms.Padding(0, 35, 0, 0);
this.ShowTitle = true;
this.Symbol = 61474;

View File

@ -1,5 +1,6 @@
using Sunny.UI.Demo.Properties;
using System;
using System.Drawing;
namespace Sunny.UI.Demo
{
@ -10,6 +11,17 @@ namespace Sunny.UI.Demo
InitializeComponent();
}
private void ListBox_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
string text = uiListBox1.GetItemText(e.Index);
if (text.Contains("6"))
{
e.Graphics.FillRectangle(UIColor.Green, e.Bounds);
e.Graphics.DrawString(text, e.Font, Color.Blue, e.Bounds);
}
}
public override void Init()
{
uiListBox1.Items.Clear();

View File

@ -82,12 +82,20 @@ namespace Sunny.UI
listbox.KeyUp += Listbox_KeyUp;
listbox.MouseEnter += Listbox_MouseEnter;
listbox.MouseLeave += Listbox_MouseLeave;
listbox.DrawItem += Listbox_DrawItem;
timer = new Timer();
timer.Tick += Timer_Tick;
timer.Start();
}
private void Listbox_DrawItem(object sender, DrawItemEventArgs e)
{
DrawItem?.Invoke(sender, e);
}
public event DrawItemEventHandler DrawItem;
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
@ -574,6 +582,12 @@ namespace Sunny.UI
{
return listbox.GetItemText(item);
}
public string GetItemText(int index)
{
if (index < 0 || index >= Items.Count) return string.Empty;
return GetItemText(Items[index]);
}
}
/// <summary>
@ -829,8 +843,6 @@ namespace Sunny.UI
protected override void OnDrawItem(DrawItemEventArgs e)
{
base.OnDrawItem(e);
BeforeDrawItem?.Invoke(this, Items, e);
if (Items.Count == 0)
{
@ -857,29 +869,7 @@ namespace Sunny.UI
Color foreColor = isSelected ? ItemSelectForeColor : ForeColor;
Rectangle rect = new Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width - 1, e.Bounds.Height - 1);
string showText;
if (DisplayMember.IsNullOrEmpty())
{
showText = Items[e.Index].ToString();
}
else
{
//var list = Items[e.Index].GetType().GetNeedProperties();
//foreach (var info in list)
//{
// if (info.Name == DisplayMember)
// {
// object defaultobj = info.GetValue(Items[e.Index], null);
// showText = defaultobj.ToString();
// }
//}
showText = GetItemText(Items[e.Index]);
// if (showText.IsNullOrEmpty())
// {
// showText = Items[e.Index].ToString();
// }
}
string showText = DisplayMember.IsNullOrEmpty() ? Items[e.Index].ToString() : GetItemText(Items[e.Index]);
if (!otherState)
{
@ -907,6 +897,8 @@ namespace Sunny.UI
}
AfterDrawItem?.Invoke(this, Items, e);
base.OnDrawItem(e);
}
private Color hoverColor = Color.FromArgb(155, 200, 255);