windowcapture
исходный код / UI/RecordingOverlay.cs

RecordingOverlay.cs

99 строк · 3,250 байт · модуль UI
 1using System;
 2using System.Drawing;
 3using System.Windows.Forms;
 4using WindowCapture.Native;
 5
 6namespace WindowCapture.UI
 7{
 8    public class RecordingOverlay : Form
 9    {
10        private Label lblTimer;
11        private Button btnStop;
12        private Timer timer;
13        private DateTime startTime;
14
15        public event Action StopRequested;
16
17        public RecordingOverlay()
18        {
19            this.FormBorderStyle = FormBorderStyle.None;
20            this.TopMost = true;
21            this.ShowInTaskbar = false;
22            this.BackColor = Color.Black;
23            this.Size = new Size(180, 45);
24            this.Opacity = 0.85;
25
26            // Rounded corners
27            WinApi.TryEnableRoundedCorners(this.Handle);
28
29            lblTimer = new Label
30            {
31                Text = "00:00",
32                ForeColor = Color.White,
33                Font = new Font("Segoe UI", 12, FontStyle.Bold),
34                Location = new Point(10, 10),
35                AutoSize = true
36            };
37
38            btnStop = new Button
39            {
40                Text = "STOP",
41                FlatStyle = FlatStyle.Flat,
42                ForeColor = Color.White,
43                BackColor = Color.FromArgb(200, 50, 50),
44                Location = new Point(90, 8),
45                Size = new Size(80, 28),
46                Font = new Font("Segoe UI", 9, FontStyle.Bold)
47            };
48            btnStop.FlatAppearance.BorderSize = 0;
49            btnStop.Click += (s, e) => { if (StopRequested != null) StopRequested(); };
50
51            this.Controls.Add(lblTimer);
52            this.Controls.Add(btnStop);
53
54            timer = new Timer { Interval = 1000 };
55            timer.Tick += Timer_Tick;
56        }
57
58        public void Start()
59        {
60            startTime = DateTime.Now;
61            timer.Start();
62
63            // Position at top center, and exclude from capture BEFORE showing — otherwise the
64            // overlay can appear (briefly, at the wrong spot) in the first recorded frames.
65            var screen = Screen.PrimaryScreen.Bounds;
66            this.Location = new Point((screen.Width - this.Width) / 2, 20);
67            WinApi.SetWindowDisplayAffinity(this.Handle, WinApi.WDA_EXCLUDEFROMCAPTURE);
68
69            this.Show();
70        }
71
72        private void Timer_Tick(object sender, EventArgs e)
73        {
74            var elapsed = DateTime.Now - startTime;
75            lblTimer.Text = string.Format("{0:D2}:{1:D2}", (int)elapsed.TotalMinutes, elapsed.Seconds);
76            
77            // Pulse red dot?
78            if (elapsed.Seconds % 2 == 0) lblTimer.ForeColor = Color.Red;
79            else lblTimer.ForeColor = Color.White;
80        }
81
82        protected override void OnFormClosing(FormClosingEventArgs e)
83        {
84            if (timer != null) { timer.Stop(); timer.Dispose(); }
85            base.OnFormClosing(e);
86        }
87
88        protected override void WndProc(ref Message m)
89        {
90            // Allow dragging
91            if (m.Msg == 0x84) // WM_NCHITTEST
92            {
93                m.Result = (IntPtr)2; // HTCAPTION
94                return;
95            }
96            base.WndProc(ref m);
97        }
98    }
99}