-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathListBoxEx.cs
204 lines (172 loc) · 6.31 KB
/
ListBoxEx.cs
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
namespace KCB2
{
public class ListBoxEx : ListBox
{
/*
* 背景色をオーナードローする
*
*/
/// <summary>
/// リストボックスに表示されるアイテム
/// </summary>
public class ListBoxItem
{
public ListBoxItem()
{
Text = "";
ToolTip = null;
BackColor = SystemColors.Window;
Color = SystemColors.WindowText;
}
/// <summary>
/// 非選択時の背景色
/// </summary>
public Color BackColor { get; set; }
/// <summary>
/// 非選択時の文字色
/// </summary>
public Color Color { get; set; }
/// <summary>
/// 文字列
/// </summary>
public string Text { get; set; }
/// <summary>
/// ツールチップ
/// </summary>
public string ToolTip { get; set; }
public override string ToString()
{
return Text;
}
}
public ListBoxEx() : base()
{
DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
_currentItemSet = false;
_toolTipDisplayed = false;
_toolTipDisplayTimer = new Timer();
_toolTip = new ToolTip();
_toolTipDisplayTimer.Interval = SystemInformation.MouseHoverTime;
_toolTipDisplayTimer.Tick += _toolTipDisplayTimer_Tick;
}
/// <summary>
/// 背景色・文字色を変更するオーナードロー
/// </summary>
/// <param name="e"></param>
protected override void OnDrawItem(DrawItemEventArgs e)
{
base.OnDrawItem(e);
e.DrawBackground();
if (e.Index > -1 && Items.Count > e.Index)
{
ListBoxItem item = Items[e.Index] as ListBoxItem;
Brush b = null;
if(e.State.HasFlag(DrawItemState.Selected))
//選ばれたアイテムは色をシステム規定で塗る。
b = new SolidBrush(e.ForeColor);
else
{
//選ばれなかったアイテム
// 分けるの面倒なのでSystemBrushesは使わない。SystemBrushedをdisposeしたら何が起きる?
if (item == null)
b = new SolidBrush(e.ForeColor);
else
b = new SolidBrush(item.Color);
//背景を塗りつぶす
if (item != null)
using (Brush bb = new SolidBrush(item.BackColor))
e.Graphics.FillRectangle(bb, e.Bounds);
}
string txt = Items[e.Index].ToString();
e.Graphics.DrawString(txt, e.Font, b, e.Bounds);
b.Dispose();
}
e.DrawFocusRectangle();
}
public override void Refresh()
{
// Create a Graphics object to use when determining the size of the largest item in the ListBox.
Graphics g = CreateGraphics();
// Determine the size for HorizontalExtent using the MeasureString method using the last item in the list.
int hzSize = 0;
foreach(var item in Items)
hzSize = Math.Max(hzSize,(int)g.MeasureString(item.ToString(), Font).Width);
//微妙に足らない。
hzSize += 4;
// Set the HorizontalExtent property.
HorizontalExtent = hzSize;
base.Refresh();
}
/*
*
* tooltip表示
* http://www.codeproject.com/Articles/457444/Listbox-Control-with-Tooltip-for-Each-Item
* より。
*
*/
int _currentItem;
bool _currentItemSet;
bool _toolTipDisplayed;
Timer _toolTipDisplayTimer;
ToolTip _toolTip;
void _toolTipDisplayTimer_Tick(object sender, EventArgs e)
{
// Display tooltip text since the mouse has hovered over an item
if (!_toolTipDisplayed && _currentItem != ListBox.NoMatches &&
_currentItem < this.Items.Count)
{
ListBoxItem toolTipDisplayer = this.Items[_currentItem] as ListBoxItem;
if (toolTipDisplayer != null)
{
_toolTip.SetToolTip(this, toolTipDisplayer.ToolTip);
_toolTipDisplayed = true;
}
}
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
// Get the item that the mouse is currently over
Point cursorPoint = Cursor.Position;
cursorPoint = this.PointToClient(cursorPoint);
int itemIndex = this.IndexFromPoint(cursorPoint);
if (itemIndex == ListBox.NoMatches)
{
// Mouse is over empty space in the listbox so hide tooltip
_toolTip.Hide(this);
_currentItemSet = false;
_toolTipDisplayed = false;
_toolTipDisplayTimer.Stop();
}
else if (!_currentItemSet)
{
// Mouse is over a new item so start timer to display tooltip
_currentItem = itemIndex;
_currentItemSet = true;
_toolTipDisplayTimer.Start();
}
else if (itemIndex != _currentItem)
{
// Mouse is over a different item so hide tooltip and restart timer
_currentItem = itemIndex;
_toolTipDisplayTimer.Stop();
_toolTipDisplayTimer.Start();
_toolTip.Hide(this);
_toolTipDisplayed = false;
}
}
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
_currentItemSet = false;
_toolTipDisplayed = false;
_toolTipDisplayTimer.Stop();
}
}
}