-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimulatedMica.cs
109 lines (87 loc) · 2.78 KB
/
SimulatedMica.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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MirrorFace
{
public class SimulatedMica
{
public SimulatedMica(Form form)
{
Form = form;
}
public Form Form { get; set; }
private Timer BgCaptureTimer;
private int AttemptCounter = 0;
private bool IsWindows11()
{
return Environment.OSVersion.Version.Build >= 22000;
}
private Bitmap ScreenshotForm()
{
Rectangle bounds = Form.Bounds;
Bitmap bmp = new Bitmap(Form.Width, Form.Height);
using (Graphics g = Graphics.FromImage(bmp))
{
g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
}
return bmp;
}
public void CaptureSetColor(bool delayed = false)
{
if (!IsWindows11())
return;
// Take a screenshot of the form and set a sampled pixel of the titlebar as the form background color
if (delayed)
{
if (BgCaptureTimer != null)
{
BgCaptureTimer.Stop();
BgCaptureTimer.Dispose();
}
BgCaptureTimer = new Timer();
AttemptCounter = 0;
BgCaptureTimer.Tick += new EventHandler(BgCaptureTimerTick);
BgCaptureTimer.Interval = 100;
BgCaptureTimer.Start();
} else
{
BgCaptureTimerTick(null, null);
}
}
private void BgCaptureTimerTick(object sender, EventArgs e)
{
if (!IsWindows11())
return;
if (BgCaptureTimer != null)
{
BgCaptureTimer.Interval = 50;
}
Bitmap formScreenshot = ScreenshotForm();
Color titleBarColor = formScreenshot.GetPixel(formScreenshot.Width / 2, 9);
// Only capture light colors
if (titleBarColor.R + titleBarColor.G + titleBarColor.B >= 700)
{
Form.BackColor = titleBarColor;
if (BgCaptureTimer != null)
{
BgCaptureTimer.Stop();
BgCaptureTimer.Dispose();
}
}
// Prevent an infinite loop where no valid color is sampled
if (AttemptCounter >= 50)
{
if (BgCaptureTimer != null)
{
BgCaptureTimer.Stop();
BgCaptureTimer.Dispose();
}
}
AttemptCounter += 1;
}
}
}