更新UIListBox消息处理Bug

This commit is contained in:
sunny 2020-05-27 11:38:28 +08:00
parent a50b47626d
commit fa512aee15
9 changed files with 241 additions and 126 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -35,7 +35,6 @@ namespace Sunny.UI
public sealed partial class UIImageListBox : UIPanel
{
private readonly ImageListBox listbox = new ImageListBox();
private readonly UIPanel panel = new UIPanel();
private readonly UIScrollBar bar = new UIScrollBar();
public UIImageListBox()
@ -45,28 +44,18 @@ namespace Sunny.UI
Padding = new Padding(2);
panel.Radius = 0;
panel.RadiusSides = UICornerRadiusSides.None;
panel.RectSides = ToolStripStatusLabelBorderSides.None;
panel.Parent = this;
panel.Width = 0;
panel.Dock = DockStyle.Right;
panel.Show();
bar.ValueChanged += Bar_ValueChanged;
bar.Parent = panel;
bar.Dock = DockStyle.Fill;
bar.Width = SystemInformation.VerticalScrollBarWidth + 2;
bar.Parent = this;
bar.Dock = DockStyle.None;
bar.Style = UIStyle.Custom;
bar.Show();
bar.Visible = false;
listbox.Parent = this;
listbox.Dock = DockStyle.Fill;
listbox.Show();
listbox.panel = panel;
listbox.Bar = bar;
panel.SendToBack();
listbox.SelectedIndexChanged += Listbox_SelectedIndexChanged;
listbox.SelectedValueChanged += Listbox_SelectedValueChanged;
listbox.Click += Listbox_Click;
@ -74,6 +63,14 @@ namespace Sunny.UI
listbox.BeforeDrawItem += Listbox_BeforeDrawItem;
}
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
bar.Top = 2;
bar.Height = Height - 4;
bar.Left = Width - bar.Width - 2;
}
private void Listbox_BeforeDrawItem(object sender, ListBox.ObjectCollection items, DrawItemEventArgs e)
{
if (Items.Count != LastCount)
@ -146,11 +143,6 @@ namespace Sunny.UI
base.SetStyleColor(uiColor);
if (uiColor.IsCustom()) return;
if (panel != null)
{
panel.FillColor = Color.White;
}
if (bar != null)
{
bar.ForeColor = uiColor.PrimaryColor;
@ -283,8 +275,6 @@ namespace Sunny.UI
[DefaultValue(null)]
public string TagString { get; set; }
public UIPanel panel { get; set; }
public UIScrollBar Bar
{
get => bar;
@ -312,38 +302,29 @@ namespace Sunny.UI
protected override void OnSizeChanged(EventArgs e)
{
if (!PanelVisible)
{
SetScrollInfo();
}
SetScrollInfo();
}
public void SetScrollInfo()
{
if (Bar == null || panel == null)
if (Bar == null)
{
return;
}
PanelVisible = true;
var si = ScrollBarInfo.GetInfo(Handle);
if (si.ScrollMax > 0)
{
Bar.Maximum = si.ScrollMax;
panel.Width = (si.ScrollMax > 0 && si.nMax > 0 && si.nPage > 0) ? SystemInformation.VerticalScrollBarWidth + 2 : 0;
panel.SendToBack();
Bar.Visible = si.ScrollMax > 0 && si.nMax > 0 && si.nPage > 0;
Bar.Value = si.nPos;
}
else
{
panel.Width = 0;
Bar.Visible = false;
}
PanelVisible = false;
}
private bool PanelVisible;
protected override void OnMeasureItem(MeasureItemEventArgs e)
{
e.ItemHeight = e.ItemHeight + ItemHeight;
@ -363,13 +344,6 @@ namespace Sunny.UI
[Browsable(false)]
public int Count => Items.Count;
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
//隐藏滚动条
ScrollBarInfo.ShowScrollBar(Handle, 3, false);//0:horizontal,1:vertical,3:both
}
public void SelectedFirst()
{
if (Count > 0 && SelectedIndex < 0)
@ -382,7 +356,7 @@ namespace Sunny.UI
{
base.OnMouseWheel(e);
if (panel.Width > 0)
if (Bar.Visible)
{
var si = ScrollBarInfo.GetInfo(Handle);
if (e.Delta > 10)

View File

@ -36,7 +36,6 @@ namespace Sunny.UI
public sealed partial class UIListBox : UIPanel
{
private readonly ListBoxEx listbox = new ListBoxEx();
private readonly UIPanel panel = new UIPanel();
private readonly UIScrollBar bar = new UIScrollBar();
public UIListBox()
@ -45,28 +44,18 @@ namespace Sunny.UI
ShowText = false;
Padding = new Padding(2);
panel.Radius = 0;
panel.RadiusSides = UICornerRadiusSides.None;
panel.RectSides = ToolStripStatusLabelBorderSides.None;
panel.Parent = this;
panel.Width = 0;
panel.Dock = DockStyle.Right;
panel.Show();
bar.ValueChanged += Bar_ValueChanged;
bar.Parent = panel;
bar.Dock = DockStyle.Fill;
bar.Width = SystemInformation.VerticalScrollBarWidth + 2;
bar.Parent = this;
bar.Dock = DockStyle.None;
bar.Style = UIStyle.Custom;
bar.Show();
bar.Visible = false;
listbox.Parent = this;
listbox.Dock = DockStyle.Fill;
listbox.Show();
listbox.panel = panel;
listbox.Bar = bar;
panel.SendToBack();
listbox.SelectedIndexChanged += Listbox_SelectedIndexChanged;
listbox.SelectedValueChanged += Listbox_SelectedValueChanged;
listbox.Click += Listbox_Click;
@ -74,6 +63,14 @@ namespace Sunny.UI
listbox.BeforeDrawItem += Listbox_BeforeDrawItem;
}
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
bar.Top = 2;
bar.Height = Height - 4;
bar.Left = Width - bar.Width - 2;
}
private void Listbox_BeforeDrawItem(object sender, ListBox.ObjectCollection items, DrawItemEventArgs e)
{
if (Items.Count != LastCount)
@ -137,11 +134,6 @@ namespace Sunny.UI
bar.FillColor = Color.White;
}
if (panel != null)
{
panel.FillColor = Color.White;
}
hoverColor = uiColor.TreeViewHoverColor;
if (listbox != null)
{
@ -246,8 +238,6 @@ namespace Sunny.UI
{
private UIScrollBar bar;
public UIPanel panel { get; set; }
[DefaultValue(null)]
public string TagString { get; set; }
@ -281,38 +271,29 @@ namespace Sunny.UI
protected override void OnSizeChanged(EventArgs e)
{
if (!PanelVisible)
{
SetScrollInfo();
}
SetScrollInfo();
}
public void SetScrollInfo()
{
if (Bar == null || panel == null)
if (Bar == null)
{
return;
}
PanelVisible = true;
var si = ScrollBarInfo.GetInfo(Handle);
if (si.ScrollMax > 0)
{
Bar.Maximum = si.ScrollMax;
panel.Width = (si.ScrollMax > 0 && si.nMax > 0 && si.nPage > 0) ? SystemInformation.VerticalScrollBarWidth + 2 : 0;
panel.SendToBack();
Bar.Visible = si.ScrollMax > 0 && si.nMax > 0 && si.nPage > 0;
Bar.Value = si.nPos;
}
else
{
panel.Width = 0;
Bar.Visible = false;
}
PanelVisible = false;
}
private bool PanelVisible;
public string Version { get; }
[DefaultValue(false)]
@ -352,17 +333,17 @@ namespace Sunny.UI
set => SetStyle(value);
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
//隐藏滚动条
ScrollBarInfo.ShowScrollBar(Handle, 3, false);//0:horizontal,1:vertical,3:both
}
// protected override void WndProc(ref Message m)
// {
// base.WndProc(ref m);
// //隐藏滚动条
// ScrollBarInfo.ShowScrollBar(Handle, 3, false);//0:horizontal,1:vertical,3:both
// }
protected override void OnMouseWheel(MouseEventArgs e)
{
base.OnMouseWheel(e);
if (panel.Width > 0)
if (Bar.Visible)
{
var si = ScrollBarInfo.GetInfo(Handle);
if (e.Delta > 10)
@ -532,15 +513,15 @@ namespace Sunny.UI
{
if (mouseIndex != value)
{
if (lastIndex >= 0 && lastIndex != SelectedIndex)
if (lastIndex >= 0 && lastIndex >= 0 && lastIndex < Items.Count && lastIndex != SelectedIndex)
{
OnDrawItem(new DrawItemEventArgs(this.CreateGraphics(), Font, GetItemRectangle(lastIndex), lastIndex, DrawItemState.Grayed));
OnDrawItem(new DrawItemEventArgs(CreateGraphics(), Font, GetItemRectangle(lastIndex), lastIndex, DrawItemState.Grayed));
}
mouseIndex = value;
if (mouseIndex >= 0 && mouseIndex != SelectedIndex)
if (mouseIndex >= 0 && mouseIndex >= 0 && mouseIndex < Items.Count && mouseIndex != SelectedIndex)
{
OnDrawItem(new DrawItemEventArgs(this.CreateGraphics(), Font, GetItemRectangle(value), value, DrawItemState.HotLight));
OnDrawItem(new DrawItemEventArgs(CreateGraphics(), Font, GetItemRectangle(value), value, DrawItemState.HotLight));
}
lastIndex = mouseIndex;

View File

@ -477,5 +477,117 @@ namespace Sunny.UI
public bool StyleCustomMode { get; set; }
public string Version { get; }
public TreeNode CreateNode(string text, int pageIndex)
{
return CreateNode(new NavMenuItem(text, pageIndex));
}
public TreeNode CreateNode(UIPage page)
{
return CreateNode(new NavMenuItem(page.Text, page.PageIndex));
}
public TreeNode CreateNode(NavMenuItem item)
{
TreeNode node = new TreeNode(item.Text);
Nodes.Add(node);
SetNodeItem(node, item);
return node;
}
public TreeNode CreateNode(string text, int imageIndex, int pageIndex)
{
return CreateNode(new NavMenuItem(text, pageIndex), imageIndex);
}
public TreeNode CreateNode(UIPage page, int imageIndex)
{
return CreateNode(new NavMenuItem(page.Text, page.PageIndex), imageIndex);
}
public TreeNode CreateNode(NavMenuItem item, int imageIndex)
{
TreeNode node = new TreeNode(item.Text);
Nodes.Add(node);
SetNodeItem(node, item);
node.ImageIndex = imageIndex;
return node;
}
public TreeNode CreateNode(string text, int symbol, int symbolSize, int pageIndex)
{
return CreateNode(new NavMenuItem(text, pageIndex), symbol, symbolSize);
}
public TreeNode CreateNode(UIPage page, int symbol, int symbolSize)
{
return CreateNode(new NavMenuItem(page.Text, page.PageIndex), symbol, symbolSize);
}
public TreeNode CreateNode(NavMenuItem item, int symbol, int symbolSize)
{
TreeNode node = new TreeNode(item.Text);
Nodes.Add(node);
SetNodeItem(node, item);
MenuHelper.SetSymbol(node, symbol, symbolSize);
return node;
}
public TreeNode CreateChildNode(TreeNode parent, string text, int pageIndex)
{
return CreateChildNode(parent, new NavMenuItem(text, pageIndex));
}
public TreeNode CreateChildNode(TreeNode parent, UIPage page)
{
return CreateChildNode(parent, new NavMenuItem(page.Text, page.PageIndex));
}
public TreeNode CreateChildNode(TreeNode parent, NavMenuItem item)
{
TreeNode childNode = new TreeNode(item.Text);
parent.Nodes.Add(childNode);
SetNodeItem(childNode, item);
return childNode;
}
public TreeNode CreateChildNode(TreeNode parent, int imageIndex, string text, int pageIndex)
{
return CreateChildNode(parent, imageIndex, new NavMenuItem(text, pageIndex));
}
public TreeNode CreateChildNode(TreeNode parent, int imageIndex, UIPage page)
{
return CreateChildNode(parent, imageIndex, new NavMenuItem(page.Text, page.PageIndex));
}
public TreeNode CreateChildNode(TreeNode parent, int imageIndex, NavMenuItem item)
{
TreeNode childNode = new TreeNode(item.Text);
parent.Nodes.Add(childNode);
SetNodeItem(childNode, item);
childNode.ImageIndex = imageIndex;
return childNode;
}
public TreeNode CreateChildNode(TreeNode parent, int symbol, int symbolSize, string text, int pageIndex)
{
return CreateChildNode(parent, symbol, symbolSize, new NavMenuItem(text, pageIndex));
}
public TreeNode CreateChildNode(TreeNode parent, int symbol, int symbolSize, UIPage page)
{
return CreateChildNode(parent, symbol, symbolSize, new NavMenuItem(page.Text, page.PageIndex));
}
public TreeNode CreateChildNode(TreeNode parent, int symbol, int symbolSize, NavMenuItem item)
{
TreeNode childNode = new TreeNode(item.Text);
parent.Nodes.Add(childNode);
SetNodeItem(childNode, item);
MenuHelper.SetSymbol(childNode, symbol, symbolSize);
return childNode;
}
}
}

View File

@ -348,6 +348,11 @@ namespace Sunny.UI
MenuHelper.SetPageIndex(node, pageIndex);
}
public void SetNodeSymbol(TreeNode node, int symbol, int symbolSize = 24)
{
MenuHelper.SetSymbol(node, symbol, symbolSize);
}
protected override void OnDrawNode(DrawTreeNodeEventArgs e)
{
if (BorderStyle != BorderStyle.None)
@ -411,7 +416,7 @@ namespace Sunny.UI
}
else
{
if (e.Selected() && e.Node.SelectedImageIndex >= 0 && e.Node.SelectedImageIndex < ImageList.Images.Count)
if (TreeNodeSelected(e) && e.Node.SelectedImageIndex >= 0 && e.Node.SelectedImageIndex < ImageList.Images.Count)
e.Graphics.DrawImage(ImageList.Images[e.Node.SelectedImageIndex], imageLeft, e.Bounds.Y + (e.Bounds.Height - ImageList.ImageSize.Height) / 2);
else
e.Graphics.DrawImage(ImageList.Images[e.Node.ImageIndex], imageLeft, e.Bounds.Y + (e.Bounds.Height - ImageList.ImageSize.Height) / 2);
@ -436,6 +441,12 @@ namespace Sunny.UI
}
}
private bool TreeNodeSelected(DrawTreeNodeEventArgs e)
{
return e.State == TreeNodeStates.Selected || e.State == TreeNodeStates.Focused ||
e.State == (TreeNodeStates.Focused | TreeNodeStates.Selected);
}
[Description("展开节点后选中第一个子节点"), DefaultValue(true)]
public bool ExpandSelectFirst { get; set; } = true;
@ -596,85 +607,116 @@ namespace Sunny.UI
//隐藏滚动条
ScrollBarInfo.ShowScrollBar(Handle, 3, false);//0:horizontal,1:vertical,3:both
}
}
public static class TreeViewHelper
{
public static bool Selected(this DrawTreeNodeEventArgs e)
public TreeNode CreateNode(string text, int pageIndex)
{
return e.State == TreeNodeStates.Selected || e.State == TreeNodeStates.Focused ||
e.State == (TreeNodeStates.Focused | TreeNodeStates.Selected);
return CreateNode(new NavMenuItem(text, pageIndex));
}
public static TreeNode CreateNode(this UINavMenu navMenu, string text, int pageIndex)
public TreeNode CreateNode(UIPage page)
{
return navMenu.CreateNode(new NavMenuItem(text, pageIndex));
return CreateNode(new NavMenuItem(page.Text, page.PageIndex));
}
public static TreeNode CreateNode(this UINavMenu navMenu, UIPage page)
{
return navMenu.CreateNode(new NavMenuItem(page.Text, page.PageIndex));
}
public static TreeNode CreateNode(this UINavMenu navMenu, NavMenuItem item)
public TreeNode CreateNode(NavMenuItem item)
{
TreeNode node = new TreeNode(item.Text);
navMenu.Nodes.Add(node);
navMenu.SetNodeItem(node, item);
Nodes.Add(node);
SetNodeItem(node, item);
return node;
}
public static TreeNode CreateNode(this UINavBar navBar, string text, int pageIndex)
public TreeNode CreateNode(string text, int imageIndex, int pageIndex)
{
return navBar.CreateNode(new NavMenuItem(text, pageIndex));
return CreateNode(new NavMenuItem(text, pageIndex), imageIndex);
}
public static TreeNode CreateNode(this UINavBar navBar, UIPage page)
public TreeNode CreateNode(UIPage page, int imageIndex)
{
return navBar.CreateNode(new NavMenuItem(page.Text, page.PageIndex));
return CreateNode(new NavMenuItem(page.Text, page.PageIndex), imageIndex);
}
public static TreeNode CreateNode(this UINavBar navBar, NavMenuItem item)
public TreeNode CreateNode(NavMenuItem item, int imageIndex)
{
TreeNode node = new TreeNode(item.Text);
navBar.Nodes.Add(node);
navBar.SetNodeItem(node, item);
Nodes.Add(node);
SetNodeItem(node, item);
node.ImageIndex = imageIndex;
return node;
}
public static TreeNode CreateChildNode(this UINavMenu navMenu, TreeNode parent, string text, int pageIndex)
public TreeNode CreateNode(string text, int symbol, int symbolSize, int pageIndex)
{
return navMenu.CreateChildNode(parent, new NavMenuItem(text, pageIndex));
return CreateNode(new NavMenuItem(text, pageIndex), symbol, symbolSize);
}
public static TreeNode CreateChildNode(this UINavMenu navMenu, TreeNode parent, UIPage page)
public TreeNode CreateNode(UIPage page, int symbol, int symbolSize)
{
return navMenu.CreateChildNode(parent, new NavMenuItem(page.Text, page.PageIndex));
return CreateNode(new NavMenuItem(page.Text, page.PageIndex), symbol, symbolSize);
}
public static TreeNode CreateChildNode(this UINavMenu navMenu, TreeNode parent, NavMenuItem item)
public TreeNode CreateNode(NavMenuItem item, int symbol, int symbolSize)
{
TreeNode node = new TreeNode(item.Text);
Nodes.Add(node);
SetNodeItem(node, item);
MenuHelper.SetSymbol(node, symbol, symbolSize);
return node;
}
public TreeNode CreateChildNode(TreeNode parent, string text, int pageIndex)
{
return CreateChildNode(parent, new NavMenuItem(text, pageIndex));
}
public TreeNode CreateChildNode(TreeNode parent, UIPage page)
{
return CreateChildNode(parent, new NavMenuItem(page.Text, page.PageIndex));
}
public TreeNode CreateChildNode(TreeNode parent, NavMenuItem item)
{
TreeNode childNode = new TreeNode(item.Text);
parent.Nodes.Add(childNode);
navMenu.SetNodeItem(childNode, item);
SetNodeItem(childNode, item);
return childNode;
}
public static TreeNode CreateChildNode(this UINavBar navBar, TreeNode parent, string text, int pageIndex)
public TreeNode CreateChildNode(TreeNode parent, int imageIndex, string text, int pageIndex)
{
return navBar.CreateChildNode(parent, new NavMenuItem(text, pageIndex));
return CreateChildNode(parent, imageIndex, new NavMenuItem(text, pageIndex));
}
public static TreeNode CreateChildNode(this UINavBar navBar, TreeNode parent, UIPage page)
public TreeNode CreateChildNode(TreeNode parent, int imageIndex, UIPage page)
{
return navBar.CreateChildNode(parent, new NavMenuItem(page.Text, page.PageIndex));
return CreateChildNode(parent, imageIndex, new NavMenuItem(page.Text, page.PageIndex));
}
public static TreeNode CreateChildNode(this UINavBar navBar, TreeNode parent, NavMenuItem item)
public TreeNode CreateChildNode(TreeNode parent, int imageIndex, NavMenuItem item)
{
TreeNode childNode = new TreeNode(item.Text);
parent.Nodes.Add(childNode);
navBar.SetNodeItem(childNode, item);
SetNodeItem(childNode, item);
childNode.ImageIndex = imageIndex;
return childNode;
}
public TreeNode CreateChildNode(TreeNode parent, int symbol, int symbolSize, string text, int pageIndex)
{
return CreateChildNode(parent, symbol, symbolSize, new NavMenuItem(text, pageIndex));
}
public TreeNode CreateChildNode(TreeNode parent, int symbol, int symbolSize, UIPage page)
{
return CreateChildNode(parent, symbol, symbolSize, new NavMenuItem(page.Text, page.PageIndex));
}
public TreeNode CreateChildNode(TreeNode parent, int symbol, int symbolSize, NavMenuItem item)
{
TreeNode childNode = new TreeNode(item.Text);
parent.Nodes.Add(childNode);
SetNodeItem(childNode, item);
MenuHelper.SetSymbol(childNode, symbol, symbolSize);
return childNode;
}
}

View File

@ -393,7 +393,7 @@ namespace Sunny.UI
}
else
{
if (e.Selected() && e.Node.SelectedImageIndex >= 0 && e.Node.SelectedImageIndex < ImageList.Images.Count)
if (TreeNodeSelected(e) && e.Node.SelectedImageIndex >= 0 && e.Node.SelectedImageIndex < ImageList.Images.Count)
e.Graphics.DrawImage(ImageList.Images[e.Node.SelectedImageIndex], imageLeft, e.Bounds.Y + (e.Bounds.Height - ImageList.ImageSize.Height) / 2);
else
e.Graphics.DrawImage(ImageList.Images[e.Node.ImageIndex], imageLeft, e.Bounds.Y + (e.Bounds.Height - ImageList.ImageSize.Height) / 2);
@ -482,6 +482,12 @@ namespace Sunny.UI
}
}
private bool TreeNodeSelected(DrawTreeNodeEventArgs e)
{
return e.State == TreeNodeStates.Selected || e.State == TreeNodeStates.Focused ||
e.State == (TreeNodeStates.Focused | TreeNodeStates.Selected);
}
public string Version { get; }
protected override void OnNodeMouseClick(TreeNodeMouseClickEventArgs e)