-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathItemsControl.cs
More file actions
93 lines (78 loc) · 2.73 KB
/
Copy pathItemsControl.cs
File metadata and controls
93 lines (78 loc) · 2.73 KB
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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace TestProject1 {
public class ItemsControl : ScrollableControl {
public List<Item> Items { get; set; }
ItemsControlInfo ViewInfo { get; }
public ItemsControl() {
Items = new List<Item>();
ViewInfo = new ItemsControlInfo(this);
AutoScroll = true;
}
protected override void OnResize(EventArgs e) {
base.OnResize(e);
ViewInfo.Update();
Invalidate();
}
protected override void OnScroll(ScrollEventArgs se) {
base.OnScroll(se);
Invalidate();
}
protected override void OnPaint(PaintEventArgs e) {
base.OnPaint(e);
ViewInfo.Draw(e, ClientRectangle, AutoScrollPosition.Y);
}
}
public class Item {
public Color Color { get; set; }
public Size Size { get; set; }
}
public class ItemsControlInfo {
protected ItemsControl Control { get; }
public List<ItemInfo> ItemInfoList { get; }
public ItemsControlInfo(ItemsControl control) {
Control = control;
ItemInfoList = new List<ItemInfo>();
}
public void Update() {
ItemInfoList.Clear();
int y = CalcLayout(Control.ClientRectangle);
Control.AutoScrollMinSize = new Size(0, y);
}
protected virtual int CalcLayout(Rectangle bounds) {
int y = 0;
foreach(var item in Control.Items) {
var size = item.Size;
var pt = new Point(0, y);
var itemInfo = new ItemInfo(item, new Rectangle(pt, size));
ItemInfoList.Add(itemInfo);
y += size.Width;
}
return y;
}
public void Draw(PaintEventArgs e, Rectangle drawBounds, int scrollOffset) {
foreach(var itemInfo in ItemInfoList) {
itemInfo.Draw(e, scrollOffset);
}
}
}
public class ItemInfo {
public Rectangle Bounds { get; }
public Item Item { get; }
public ItemInfo(Item item, Rectangle bounds) {
Bounds = bounds;
Item = item;
}
public void Draw(PaintEventArgs e, int scrollOffset) {
var rect = Bounds;
rect.Offset(0, scrollOffset);
var brush = new SolidBrush(Item.Color);
e.Graphics.FillRectangle(brush, rect);
e.Graphics.DrawRectangle(Pens.Gray, rect);
var textBrush = new SolidBrush(Color.Black);
e.Graphics.DrawString(Item.Size.ToString(), Control.DefaultFont, textBrush, rect);
}
}
}