-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNoteSearchForm.cs
More file actions
149 lines (130 loc) · 5.89 KB
/
NoteSearchForm.cs
File metadata and controls
149 lines (130 loc) · 5.89 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
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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace StickyNote
{
/// <summary>
/// 搜索便签对话框(WinForms 版本)
/// </summary>
public static class NoteSearchForm
{
public static void Show(IReadOnlyList<NoteData> notes, Action<string>? onActivate = null)
{
var dlg = new Form
{
Text = "搜索便签",
Width = 440,
Height = 520,
StartPosition = FormStartPosition.CenterScreen,
TopMost = true,
Font = new Font("Microsoft YaHei", 9f),
BackColor = Color.FromArgb(0xFD, 0xF6, 0xE3),
FormBorderStyle = FormBorderStyle.FixedSingle,
MaximizeBox = false
};
var searchBox = new TextBox
{
Dock = DockStyle.None,
Left = 10, Top = 10, Width = 400,
Font = new Font("Microsoft YaHei", 11f),
PlaceholderText = "输入关键词搜索…",
BorderStyle = BorderStyle.FixedSingle
};
var listBox = new ListBox
{
Left = 10, Top = 44,
Width = 400, Height = 400,
Font = new Font("Microsoft YaHei", 10f),
BorderStyle = BorderStyle.None,
ItemHeight = 42,
DrawMode = DrawMode.OwnerDrawFixed,
BackColor = Color.FromArgb(0xFD, 0xF6, 0xE3)
};
// 搜索结果数据
var results = new List<NoteData>();
void Refresh()
{
string q = searchBox.Text.Trim().ToLower();
results.Clear();
foreach (var n in notes)
{
string plain = GetPlain(n.Content);
if (string.IsNullOrEmpty(q)
|| n.Title.Contains(q, StringComparison.OrdinalIgnoreCase)
|| plain.Contains(q, StringComparison.OrdinalIgnoreCase))
results.Add(n);
}
listBox.BeginUpdate();
listBox.Items.Clear();
foreach (var r in results)
listBox.Items.Add(r.Title.Length > 0 ? r.Title : "便签");
listBox.EndUpdate();
}
// 自绘列表项
listBox.DrawItem += (s, e) =>
{
if (e.Index < 0 || e.Index >= results.Count) return;
e.DrawBackground();
var note = results[e.Index];
var g = e.Graphics;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
bool sel = (e.State & DrawItemState.Selected) != 0;
var bgColor = sel ? Color.FromArgb(180, 227, 200, 135) : Color.FromArgb(0xFD, 0xF6, 0xE3);
g.FillRectangle(new SolidBrush(bgColor), e.Bounds);
// 左色条
var noteColor = TryParse(note.ColorHex, Color.FromArgb(0xE3, 0xC8, 0x87));
g.FillRectangle(new SolidBrush(noteColor), e.Bounds.Left, e.Bounds.Top + 4, 4, e.Bounds.Height - 8);
// 标题
string title = string.IsNullOrEmpty(note.Title) ? "便签" : note.Title;
using (var tf = new Font("Microsoft YaHei", 9.5f, FontStyle.Bold))
using (var tb = new SolidBrush(Color.FromArgb(62, 39, 35)))
g.DrawString(title, tf, tb, e.Bounds.Left + 12, e.Bounds.Top + 5);
// 预览
string preview = GetPreviewLine(GetPlain(note.Content));
using (var pf = new Font("Microsoft YaHei", 8f))
using (var pb = new SolidBrush(Color.FromArgb(130, 93, 64, 55)))
g.DrawString(preview, pf, pb, e.Bounds.Left + 12, e.Bounds.Top + 23);
// 分隔线
g.DrawLine(new Pen(Color.FromArgb(30, 93, 64, 55)), e.Bounds.Left + 8, e.Bounds.Bottom - 1, e.Bounds.Right - 8, e.Bounds.Bottom - 1);
};
searchBox.TextChanged += (s, e) => Refresh();
void Activate()
{
if (listBox.SelectedIndex < 0 || listBox.SelectedIndex >= results.Count) return;
onActivate?.Invoke(results[listBox.SelectedIndex].Id);
dlg.Close();
}
listBox.MouseDoubleClick += (s, e) => Activate();
listBox.KeyDown += (s, e) => { if (e.KeyCode == Keys.Enter) Activate(); };
searchBox.KeyDown += (s, e) =>
{
if (e.KeyCode == Keys.Down && listBox.Items.Count > 0) { listBox.SelectedIndex = 0; listBox.Focus(); }
if (e.KeyCode == Keys.Enter) Activate();
};
dlg.Controls.Add(searchBox);
dlg.Controls.Add(listBox);
Refresh();
dlg.ShowDialog();
}
private static string GetPlain(string content)
{
if (string.IsNullOrEmpty(content)) return "";
if (content.StartsWith("{\\rtf")) return TabPanel.StripRtf(content);
return content;
}
private static string GetPreviewLine(string text)
{
if (string.IsNullOrEmpty(text)) return "";
var lines = text.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
string preview = lines.Length > 1 ? lines[1] : "";
if (preview.Length > 50) preview = preview[..50] + "…";
return preview;
}
private static Color TryParse(string hex, Color fallback)
{
try { return ColorTranslator.FromHtml(hex); } catch { return fallback; }
}
}
}