Skip to content

Commit ffd44f1

Browse files
authored
Merge pull request #14 from LanYunSeven/master
3.4.0.0更新
2 parents 5b7d810 + 5206fcf commit ffd44f1

File tree

5 files changed

+546
-195
lines changed

5 files changed

+546
-195
lines changed

ContextMenuManager/BluePointLilac.Controls/MyCheckBox.cs

Lines changed: 54 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,26 @@ public bool Checked
2323
get => _Checked == true;
2424
set
2525
{
26-
if(_Checked == value) return;
26+
if (_Checked == value) return;
2727
Image = SwitchImage(value);
28-
if(_Checked == null)
28+
if (_Checked == null)
2929
{
3030
_Checked = value;
3131
return;
3232
}
33-
if(PreCheckChanging != null && !PreCheckChanging.Invoke())
33+
if (PreCheckChanging != null && !PreCheckChanging.Invoke())
3434
{
3535
Image = SwitchImage(!value);
3636
return;
3737
}
38-
else CheckChanging?.Invoke();
39-
if(PreCheckChanged != null && !PreCheckChanged.Invoke())
38+
CheckChanging?.Invoke();
39+
if (PreCheckChanged != null && !PreCheckChanged.Invoke())
4040
{
4141
Image = SwitchImage(!value);
4242
return;
4343
}
44-
else
45-
{
46-
_Checked = value;
47-
CheckChanged?.Invoke();
48-
}
44+
_Checked = value;
45+
CheckChanged?.Invoke();
4946
}
5047
}
5148

@@ -57,66 +54,80 @@ public bool Checked
5754
public Image TurnOnImage { get; set; } = TurnOn;
5855
public Image TurnOffImage { get; set; } = TurnOff;
5956

60-
private Image SwitchImage(bool value)
61-
{
62-
return value ? TurnOnImage : TurnOffImage;
63-
}
57+
private Image SwitchImage(bool value) => value ? TurnOnImage : TurnOffImage;
6458

6559
protected override void OnMouseDown(MouseEventArgs e)
6660
{
6761
base.OnMouseDown(e);
68-
if(e.Button == MouseButtons.Left) Checked = !Checked;
62+
if (e.Button == MouseButtons.Left) Checked = !Checked;
6963
}
7064

7165
private static readonly Image TurnOn = DrawImage(true);
7266
private static readonly Image TurnOff = DrawImage(false);
7367

7468
private static Image DrawImage(bool value)
7569
{
76-
int w = 80.DpiZoom();
77-
int r1 = 16.DpiZoom();
78-
float r2 = 13F.DpiZoom();
79-
int d1 = r1 * 2;
80-
float d2 = r2 * 2;
81-
float a = r1 - r2;
82-
Bitmap bitmap = new Bitmap(w, d1);
83-
using(Graphics g = Graphics.FromImage(bitmap))
70+
int w = 80.DpiZoom(), h = 40.DpiZoom();
71+
int r = h / 2, padding = 4.DpiZoom();
72+
var bitmap = new Bitmap(w, h);
73+
74+
using (Graphics g = Graphics.FromImage(bitmap))
8475
{
8576
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
8677
g.CompositingQuality = CompositingQuality.HighQuality;
8778
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
88-
g.SmoothingMode = SmoothingMode.HighQuality;
89-
using(GraphicsPath path = new GraphicsPath())
79+
g.SmoothingMode = SmoothingMode.AntiAlias;
80+
81+
// 背景渐变
82+
using (var bgPath = CreateRoundedRect(0, 0, w, h, r))
9083
{
91-
path.AddArc(new RectangleF(0, 0, d1, d1), 90, 180);
92-
path.AddLine(new PointF(r1, 0), new PointF(w - r1, 0));
93-
path.AddArc(new RectangleF(w - d1, 0, d1, d1), -90, 180);
94-
path.AddLine(new PointF(w - r1, d1), new PointF(r1, d1));
95-
Color color = value ? MyMainForm.MainColor : Color.FromArgb(130, 136, 144);//Fixed color
96-
using(Brush brush = new SolidBrush(color))
84+
Color startColor = value ? MyMainForm.MainColor : Color.FromArgb(200, 200, 200);
85+
Color endColor = value ? Color.FromArgb(160, MyMainForm.MainColor.R, MyMainForm.MainColor.G, MyMainForm.MainColor.B) :
86+
Color.FromArgb(160, 160, 160);
87+
using (var bgBrush = new LinearGradientBrush(new Rectangle(0, 0, w, h), startColor, endColor, 90f))
9788
{
98-
g.FillPath(brush, path);
89+
g.FillPath(bgBrush, bgPath);
9990
}
10091
}
101-
using(GraphicsPath path = new GraphicsPath())
92+
93+
// 按钮位置计算
94+
int buttonX = value ? w - h + padding : padding;
95+
int buttonY = padding;
96+
97+
// 按钮绘制(带阴影)
98+
using (var shadowPath = CreateRoundedRect(buttonX - 2, buttonY - 2, h - padding * 2 + 4, h - padding * 2 + 4, (h - padding * 2) / 2))
99+
using (var shadowBrush = new SolidBrush(Color.FromArgb(50, 0, 0, 0)))
100+
{
101+
g.FillPath(shadowBrush, shadowPath);
102+
}
103+
104+
using (var buttonPath = CreateRoundedRect(buttonX, buttonY, h - padding * 2, h - padding * 2, (h - padding * 2) / 2))
102105
{
103-
path.AddArc(new RectangleF(a, a, d2, d2), 90, 180);
104-
path.AddLine(new PointF(r1, a), new PointF(w - r1, a));
105-
path.AddArc(new RectangleF(w - d2 - a, a, d2, d2), -90, 180);
106-
path.AddLine(new PointF(w - r1, d2 + a), new PointF(r1, d2 + a));
107-
Color color = value ? MyMainForm.MainColor : Color.FromArgb(153, 160, 169);//Fixed color
108-
using (Brush brush = new SolidBrush(color))
106+
using (var buttonBrush = new SolidBrush(Color.White))
109107
{
110-
g.FillPath(brush, path);
108+
g.FillPath(buttonBrush, buttonPath);
111109
}
112110
}
113-
using(GraphicsPath path = new GraphicsPath())
111+
112+
// 高光效果
113+
using (var highlightPath = CreateRoundedRect(buttonX + 2, buttonY + 2, (h - padding * 2) / 2, (h - padding * 2) / 2, (h - padding * 2) / 4))
114+
using (var highlightBrush = new SolidBrush(Color.FromArgb(100, 255, 255, 255)))
114115
{
115-
path.AddEllipse(new RectangleF(value ? (w - d2 - a) : a, a, d2, d2));
116-
g.FillPath(Brushes.White, path);
116+
g.FillPath(highlightBrush, highlightPath);
117117
}
118118
}
119119
return bitmap;
120120
}
121+
122+
private static GraphicsPath CreateRoundedRect(float x, float y, float width, float height, float radius)
123+
{
124+
var path = new GraphicsPath();
125+
path.AddArc(x, y, radius * 2, radius * 2, 180, 90); // 左上角
126+
path.AddArc(x + width - radius * 2, y, radius * 2, radius * 2, 270, 90); // 右上角
127+
path.AddArc(x + width - radius * 2, y + height - radius * 2, radius * 2, radius * 2, 0, 90); // 右下角
128+
path.AddArc(x, y + height - radius * 2, radius * 2, radius * 2, 90, 90); // 左下角
129+
path.CloseFigure();
130+
return path;
131+
}
121132
}
122133
}

ContextMenuManager/BluePointLilac.Controls/MyListBox.cs

Lines changed: 82 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public MyListBox()
1717

1818
protected override void OnMouseWheel(MouseEventArgs e)
1919
{
20-
//使滚动幅度与MyListItem的高度相配合,防止滚动过快导致来不及重绘界面变花
20+
// 使滚动幅度与MyListItem的高度相配合,防止滚动过快导致来不及重绘界面变花
2121
base.OnMouseWheel(new MouseEventArgs(e.Button, e.Clicks, e.X, e.Y, Math.Sign(e.Delta) * 50.DpiZoom()));
2222
}
2323
}
@@ -50,18 +50,18 @@ public MyListItem HoveredItem
5050
get => hoveredItem;
5151
set
5252
{
53-
if(hoveredItem == value) return;
54-
if(hoveredItem != null)
53+
if (hoveredItem == value) return;
54+
if (hoveredItem != null)
5555
{
56-
hoveredItem.ForeColor = MyMainForm.FormFore;
56+
hoveredItem.StartColorAnimation(MyMainForm.FormFore);
5757
hoveredItem.Font = new Font(hoveredItem.Font, FontStyle.Regular);
5858
}
5959
hoveredItem = value;
6060
if (hoveredItem != null)
6161
{
62-
value.ForeColor = MyMainForm.MainColor;
63-
value.Font = new Font(hoveredItem.Font, FontStyle.Bold);
64-
value.Focus();
62+
hoveredItem.StartColorAnimation(MyMainForm.MainColor);
63+
hoveredItem.Font = new Font(hoveredItem.Font, FontStyle.Bold);
64+
hoveredItem.Focus();
6565
}
6666
HoveredItemChanged?.Invoke(this, null);
6767
}
@@ -75,6 +75,23 @@ public void AddItem(MyListItem item)
7575
item.Parent = this;
7676
item.MouseEnter += (sender, e) => HoveredItem = item;
7777
MouseWheel += (sender, e) => item.ContextMenuStrip?.Close();
78+
79+
// 淡入动画
80+
item.Opacity = 0;
81+
Timer fadeTimer = new Timer { Interval = 15 };
82+
fadeTimer.Tick += (sender, e) =>
83+
{
84+
if (item.Opacity >= 1)
85+
{
86+
fadeTimer.Stop();
87+
fadeTimer.Dispose();
88+
return;
89+
}
90+
item.Opacity = Math.Min(item.Opacity + 0.1F, 1);
91+
item.Invalidate(); // 触发重绘
92+
};
93+
fadeTimer.Start();
94+
7895
void ResizeItem() => item.Width = Owner.Width - item.Margin.Horizontal;
7996
Owner.Resize += (sender, e) => ResizeItem();
8097
ResizeItem();
@@ -103,16 +120,16 @@ public int GetItemIndex(MyListItem item)
103120

104121
public void InsertItem(MyListItem item, int index)
105122
{
106-
if(item == null) return;
123+
if (item == null) return;
107124
AddItem(item);
108125
SetItemIndex(item, index);
109126
}
110127

111128
public virtual void ClearItems()
112129
{
113-
if(Controls.Count == 0) return;
130+
if (Controls.Count == 0) return;
114131
SuspendLayout();
115-
for(int i = Controls.Count - 1; i >= 0; i--)
132+
for (int i = Controls.Count - 1; i >= 0; i--)
116133
{
117134
Control ctr = Controls[i];
118135
Controls.Remove(ctr);
@@ -124,7 +141,7 @@ public virtual void ClearItems()
124141
public void SortItemByText()
125142
{
126143
List<MyListItem> items = new List<MyListItem>();
127-
foreach(MyListItem item in Controls) items.Add(item);
144+
foreach (MyListItem item in Controls) items.Add(item);
128145
Controls.Clear();
129146
items.Sort(new TextComparer());
130147
items.ForEach(item => AddItem(item));
@@ -134,10 +151,10 @@ public class TextComparer : IComparer<MyListItem>
134151
{
135152
public int Compare(MyListItem x, MyListItem y)
136153
{
137-
if(x.Equals(y)) return 0;
154+
if (x.Equals(y)) return 0;
138155
string[] strs = { x.Text, y.Text };
139156
Array.Sort(strs);
140-
if(strs[0] == x.Text) return -1;
157+
if (strs[0] == x.Text) return -1;
141158
else return 1;
142159
}
143160
}
@@ -173,16 +190,19 @@ public Image Image
173190
get => picImage.Image;
174191
set => picImage.Image = value;
175192
}
193+
176194
public new string Text
177195
{
178196
get => lblText.Text;
179197
set => lblText.Text = value;
180198
}
199+
181200
public new Font Font
182201
{
183202
get => lblText.Font;
184203
set => lblText.Font = value;
185204
}
205+
186206
public new Color ForeColor
187207
{
188208
get => lblText.ForeColor;
@@ -206,13 +226,15 @@ public bool HasImage
206226
AutoSize = true,
207227
Name = "Text"
208228
};
229+
209230
private readonly PictureBox picImage = new PictureBox
210231
{
211232
SizeMode = PictureBoxSizeMode.AutoSize,
212233
Left = 20.DpiZoom(),
213234
Enabled = false,
214235
Name = "Image"
215236
};
237+
216238
private readonly FlowLayoutPanel flpControls = new FlowLayoutPanel
217239
{
218240
AutoSizeMode = AutoSizeMode.GrowAndShrink,
@@ -221,18 +243,20 @@ public bool HasImage
221243
AutoSize = true,
222244
Name = "Controls"
223245
};
246+
224247
private readonly Label lblSeparator = new Label
225248
{
226249
BackColor = MyMainForm.FormFore,
227250
Dock = DockStyle.Bottom,
228251
Name = "Separator",
229252
Height = 1
230-
};//分割线
253+
}; // 分割线
254+
231255
private readonly Panel pnlScrollbar = new Panel
232256
{
233257
Width = SystemInformation.VerticalScrollBarWidth,
234258
Enabled = false
235-
};//预留滚动条宽度
259+
}; // 预留滚动条宽度
236260

237261
protected override void OnMouseDown(MouseEventArgs e)
238262
{
@@ -243,10 +267,10 @@ private void CenterControl(Control ctr)
243267
{
244268
void reSize()
245269
{
246-
if(ctr.Parent == null) return;
270+
if (ctr.Parent == null) return;
247271
int top = (ClientSize.Height - ctr.Height) / 2;
248272
ctr.Top = top;
249-
if(ctr.Parent == flpControls)
273+
if (ctr.Parent == flpControls)
250274
{
251275
ctr.Margin = new Padding(0, top, ctr.Margin.Right, top);
252276
}
@@ -279,7 +303,7 @@ public void AddCtrs(Control[] ctrs)
279303

280304
public void RemoveCtrAt(int index)
281305
{
282-
if(flpControls.Controls.Count > index) flpControls.Controls.RemoveAt(index + 1);
306+
if (flpControls.Controls.Count > index) flpControls.Controls.RemoveAt(index + 1);
283307
}
284308

285309
public int GetCtrIndex(Control ctr)
@@ -291,5 +315,45 @@ public void SetCtrIndex(Control ctr, int newIndex)
291315
{
292316
flpControls.Controls.SetChildIndex(ctr, newIndex + 1);
293317
}
318+
319+
private Timer colorAnimTimer;
320+
private Color startColor;
321+
private Color targetColor;
322+
private const int AnimDuration = 200; // 动画时长(ms)
323+
private const int AnimInterval = 15; // 刷新间隔
324+
public float Opacity { get; set; } = 1f; // 添加透明度属性
325+
326+
public void StartColorAnimation(Color newColor)
327+
{
328+
if (colorAnimTimer != null)
329+
{
330+
colorAnimTimer.Stop();
331+
colorAnimTimer.Dispose();
332+
}
333+
334+
startColor = this.ForeColor;
335+
targetColor = newColor;
336+
colorAnimTimer = new Timer { Interval = AnimInterval };
337+
338+
DateTime startTime = DateTime.Now;
339+
colorAnimTimer.Tick += (sender, e) =>
340+
{
341+
double progress = (DateTime.Now - startTime).TotalMilliseconds / AnimDuration;
342+
if (progress >= 1d)
343+
{
344+
this.ForeColor = targetColor;
345+
colorAnimTimer.Stop();
346+
return;
347+
}
348+
349+
int r = (int)(startColor.R + (targetColor.R - startColor.R) * progress);
350+
int g = (int)(startColor.G + (targetColor.G - startColor.G) * progress);
351+
int b = (int)(startColor.B + (targetColor.B - startColor.B) * progress);
352+
this.ForeColor = Color.FromArgb(r, g, b);
353+
this.Invalidate(); // 触发重绘
354+
};
355+
356+
colorAnimTimer.Start();
357+
}
294358
}
295359
}

0 commit comments

Comments
 (0)