-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSafeDisplay.cs
More file actions
1701 lines (1500 loc) · 66.7 KB
/
Copy pathSafeDisplay.cs
File metadata and controls
1701 lines (1500 loc) · 66.7 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
using System.Collections.Generic;
using Microsoft.Win32;
namespace SafeDisplay
{
public class Program
{
[DllImport("shcore.dll", EntryPoint = "SetProcessDpiAwareness")]
private static extern int SetProcessDpiAwareness(int value);
[DllImport("user32.dll", EntryPoint = "SetProcessDPIAware")]
private static extern bool SetProcessDPIAware();
private static void TrySetDpiAware()
{
try
{
// Try calling SetProcessDpiAwareness (Windows 8.1+)
// Process_Per_Monitor_DPI_Aware = 2
SetProcessDpiAwareness(2);
}
catch
{
try
{
// Fallback for older Windows (Vista/7/8)
SetProcessDPIAware();
}
catch {}
}
}
[STAThread]
public static void Main(string[] args)
{
Application.ThreadException += (s, e) => {
string logPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "crash_log.txt");
File.WriteAllText(logPath, "Thread Exception: " + e.Exception.ToString());
};
AppDomain.CurrentDomain.UnhandledException += (s, e) => {
string logPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "crash_log.txt");
File.WriteAllText(logPath, "Unhandled Exception: " + e.ExceptionObject.ToString());
};
try
{
TrySetDpiAware();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
bool startMinimized = false;
if (args.Length > 0 && (args[0] == "--startup" || args[0] == "/startup"))
{
startMinimized = true;
}
Application.Run(new MainForm(startMinimized));
}
catch (Exception ex)
{
string logPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "crash_log.txt");
File.WriteAllText(logPath, ex.ToString());
throw;
}
}
}
public class MainForm : Form
{
// Custom styling colors
private static readonly Color ColorBg = Color.FromArgb(30, 30, 36);
private static readonly Color ColorHeader = Color.FromArgb(17, 17, 22);
private static readonly Color ColorCard = Color.FromArgb(40, 40, 48);
private static readonly Color ColorAccent = Color.FromArgb(0, 173, 181);
private static readonly Color ColorTextLight = Color.FromArgb(238, 238, 238);
private static readonly Color ColorTextDim = Color.FromArgb(170, 170, 180);
private static readonly Color ColorRed = Color.FromArgb(231, 76, 60);
// Win32 API functions for dragging window
[DllImport("user32.dll")]
public static extern bool ReleaseCapture();
[DllImport("user32.dll")]
public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
private const int WM_NCLBUTTONDOWN = 0xA1;
private const int HT_CAPTION = 0x2;
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern uint RegisterWindowMessage(string lpString);
// GUI controls
private Panel headerPanel;
private Label titleLabel;
private Button closeButton;
private Button minimizeButton;
private ComboBox monitorComboBox;
private ComboBox presetComboBox;
private TrackBar topTrackBar;
private TrackBar bottomTrackBar;
private TrackBar leftTrackBar;
private TrackBar rightTrackBar;
private Label topValLabel;
private Label bottomValLabel;
private Label leftValLabel;
private Label rightValLabel;
private CheckBox startupCheckBox;
private CheckBox closeToTrayCheckBox;
private CheckBox blockCursorCheckBox;
private CheckBox integrateTaskbarCheckBox;
private Button applyButton;
private Button resetButton;
private Panel previewPanel;
private NotifyIcon trayIcon;
private ContextMenu trayMenu;
// State variables
private Screen selectedScreen;
private int currentTopMargin = 0;
private int currentBottomMargin = 0;
private int currentLeftMargin = 0;
private int currentRightMargin = 0;
private int currentDimmerOpacity = 0;
private bool isUpdatingSliders = false;
private TrackBar dimmerTrackBar;
private Label dimmerValLabel;
private Dictionary<string, DimmerWindow> activeDimmers = new Dictionary<string, DimmerWindow>();
private Dictionary<string, MarginConfig> configMap = new Dictionary<string, MarginConfig>();
private string configPath;
private bool startMinimized = false;
private bool isExiting = false;
private uint taskbarCreatedMsg;
private List<AppBarWindow> activeAppBars = new List<AppBarWindow>();
private System.Windows.Forms.Timer mouseTimer;
public class MarginConfig
{
public string DeviceName { get; set; }
public int Top { get; set; }
public int Bottom { get; set; }
public int Left { get; set; }
public int Right { get; set; }
public int DimmerOpacity { get; set; }
public bool IntegrateTaskbar { get; set; }
}
public MainForm(bool startMinimized)
{
this.startMinimized = startMinimized;
configPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "config.txt");
// Form properties
this.Size = new Size(720, 520);
this.FormBorderStyle = FormBorderStyle.None;
this.StartPosition = FormStartPosition.CenterScreen;
this.BackColor = ColorBg;
this.Text = "SafeDisplay - Corrección de Pantalla";
InitializeGUI();
LoadConfig();
// Populate monitors
PopulateMonitors();
// Set up initial active AppBars
UpdateAppBars();
UpdateDimmers();
// Set up dynamic cursor barrier timer
mouseTimer = new System.Windows.Forms.Timer();
mouseTimer.Interval = 10; // 10ms check for smooth barrier physics
mouseTimer.Tick += MouseTimer_Tick;
mouseTimer.Start();
// Register system display settings change event
SystemEvents.DisplaySettingsChanged += SystemEvents_DisplaySettingsChanged;
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
taskbarCreatedMsg = RegisterWindowMessage("TaskbarCreated");
}
protected override void WndProc(ref Message m)
{
if (m.Msg == taskbarCreatedMsg)
{
// Explorer restarted! Re-register AppBars to restore boundaries
UpdateAppBars();
}
base.WndProc(ref m);
}
private void InitializeGUI()
{
// Custom Title Bar
headerPanel = new Panel();
headerPanel.Size = new Size(this.Width, 40);
headerPanel.Location = new Point(0, 0);
headerPanel.BackColor = ColorHeader;
headerPanel.MouseDown += Header_MouseDown;
this.Controls.Add(headerPanel);
titleLabel = new Label();
titleLabel.Text = "⚡ SafeDisplay // Corrector de Pantalla";
titleLabel.Font = new Font("Segoe UI", 10, FontStyle.Bold);
titleLabel.ForeColor = ColorAccent;
titleLabel.AutoSize = true;
titleLabel.Location = new Point(12, 10);
titleLabel.MouseDown += Header_MouseDown;
headerPanel.Controls.Add(titleLabel);
closeButton = new Button();
closeButton.Text = "✕";
closeButton.Font = new Font("Segoe UI", 10, FontStyle.Bold);
closeButton.ForeColor = ColorTextLight;
closeButton.BackColor = Color.Transparent;
closeButton.FlatStyle = FlatStyle.Flat;
closeButton.FlatAppearance.BorderSize = 0;
closeButton.FlatAppearance.MouseOverBackColor = ColorRed;
closeButton.Size = new Size(40, 40);
closeButton.Location = new Point(this.Width - 40, 0);
closeButton.Click += CloseButton_Click;
headerPanel.Controls.Add(closeButton);
minimizeButton = new Button();
minimizeButton.Text = "—";
minimizeButton.Font = new Font("Segoe UI", 8, FontStyle.Bold);
minimizeButton.ForeColor = ColorTextLight;
minimizeButton.BackColor = Color.Transparent;
minimizeButton.FlatStyle = FlatStyle.Flat;
minimizeButton.FlatAppearance.BorderSize = 0;
minimizeButton.FlatAppearance.MouseOverBackColor = Color.FromArgb(50, 50, 60);
minimizeButton.Size = new Size(40, 40);
minimizeButton.Location = new Point(this.Width - 80, 0);
minimizeButton.Click += (s, e) => { this.WindowState = FormWindowState.Minimized; };
headerPanel.Controls.Add(minimizeButton);
// Left panel (Controls Area)
Panel leftPanel = new Panel();
leftPanel.Size = new Size(330, this.Height - 40);
leftPanel.Location = new Point(0, 40);
leftPanel.BackColor = ColorBg;
this.Controls.Add(leftPanel);
Label monitorLabel = new Label();
monitorLabel.Text = "Selecciona la Pantalla (TV):";
monitorLabel.Font = new Font("Segoe UI", 9, FontStyle.Bold);
monitorLabel.ForeColor = ColorTextLight;
monitorLabel.Location = new Point(20, 10);
monitorLabel.Size = new Size(200, 20);
leftPanel.Controls.Add(monitorLabel);
monitorComboBox = new ComboBox();
monitorComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
monitorComboBox.Font = new Font("Segoe UI", 9);
monitorComboBox.BackColor = ColorCard;
monitorComboBox.ForeColor = ColorTextLight;
monitorComboBox.FlatStyle = FlatStyle.Flat;
monitorComboBox.Location = new Point(20, 32);
monitorComboBox.Size = new Size(290, 25);
monitorComboBox.SelectedIndexChanged += MonitorComboBox_SelectedIndexChanged;
leftPanel.Controls.Add(monitorComboBox);
// Presets Dropdown
Label presetLabel = new Label();
presetLabel.Text = "Preajuste de Tamaño (Monitor 27\" 2K):";
presetLabel.Font = new Font("Segoe UI", 8.5f, FontStyle.Bold);
presetLabel.ForeColor = ColorTextLight;
presetLabel.Location = new Point(20, 65);
presetLabel.Size = new Size(250, 18);
leftPanel.Controls.Add(presetLabel);
presetComboBox = new ComboBox();
presetComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
presetComboBox.Font = new Font("Segoe UI", 9);
presetComboBox.BackColor = ColorCard;
presetComboBox.ForeColor = ColorTextLight;
presetComboBox.FlatStyle = FlatStyle.Flat;
presetComboBox.Location = new Point(20, 85);
presetComboBox.Size = new Size(290, 25);
presetComboBox.Items.AddRange(new object[] {
"Personalizado (Manual)",
"24.0\" Centrado (Barra integrada)",
"24.0\" Alineado Abajo (Barra integrada)",
"24.5\" Centrado (Barra integrada)",
"24.5\" Alineado Abajo (Barra integrada)",
"23.8\" Centrado (Barra integrada)",
"23.8\" Alineado Abajo (Barra integrada)"
});
presetComboBox.SelectedIndex = 0;
presetComboBox.SelectedIndexChanged += PresetComboBox_SelectedIndexChanged;
leftPanel.Controls.Add(presetComboBox);
// Sliders Container
Panel slidersCard = new Panel();
slidersCard.Size = new Size(290, 220);
slidersCard.Location = new Point(20, 120);
slidersCard.BackColor = ColorCard;
leftPanel.Controls.Add(slidersCard);
// Top Margin Slider
Label topLabel = new Label();
topLabel.Text = "Margen Superior (Franja Rota)";
topLabel.Font = new Font("Segoe UI", 8, FontStyle.Bold);
topLabel.ForeColor = ColorTextDim;
topLabel.Location = new Point(15, 10);
topLabel.Size = new Size(180, 15);
slidersCard.Controls.Add(topLabel);
topValLabel = new Label();
topValLabel.Text = "0 px";
topValLabel.Font = new Font("Segoe UI", 8, FontStyle.Bold);
topValLabel.ForeColor = ColorAccent;
topValLabel.Location = new Point(230, 10);
topValLabel.Size = new Size(50, 15);
topValLabel.TextAlign = ContentAlignment.TopRight;
slidersCard.Controls.Add(topValLabel);
topTrackBar = new TrackBar();
topTrackBar.Minimum = 0;
topTrackBar.Maximum = 720;
topTrackBar.TickStyle = TickStyle.None;
topTrackBar.AutoSize = false;
topTrackBar.Location = new Point(10, 28);
topTrackBar.Size = new Size(270, 22);
topTrackBar.Scroll += (s, e) => {
currentTopMargin = topTrackBar.Value;
topValLabel.Text = currentTopMargin + " px";
OnSliderScrolled();
previewPanel.Invalidate();
};
slidersCard.Controls.Add(topTrackBar);
// Bottom Margin Slider
Label bottomLabel = new Label();
bottomLabel.Text = "Margen Inferior";
bottomLabel.Font = new Font("Segoe UI", 8, FontStyle.Bold);
bottomLabel.ForeColor = ColorTextDim;
bottomLabel.Location = new Point(15, 58);
bottomLabel.Size = new Size(180, 15);
slidersCard.Controls.Add(bottomLabel);
bottomValLabel = new Label();
bottomValLabel.Text = "0 px";
bottomValLabel.Font = new Font("Segoe UI", 8, FontStyle.Bold);
bottomValLabel.ForeColor = ColorAccent;
bottomValLabel.Location = new Point(230, 58);
bottomValLabel.Size = new Size(50, 15);
bottomValLabel.TextAlign = ContentAlignment.TopRight;
slidersCard.Controls.Add(bottomValLabel);
bottomTrackBar = new TrackBar();
bottomTrackBar.Minimum = 0;
bottomTrackBar.Maximum = 720;
bottomTrackBar.TickStyle = TickStyle.None;
bottomTrackBar.AutoSize = false;
bottomTrackBar.Location = new Point(10, 76);
bottomTrackBar.Size = new Size(270, 22);
bottomTrackBar.Scroll += (s, e) => {
currentBottomMargin = bottomTrackBar.Value;
bottomValLabel.Text = currentBottomMargin + " px";
OnSliderScrolled();
previewPanel.Invalidate();
};
slidersCard.Controls.Add(bottomTrackBar);
// Left Margin Slider
Label leftLabel = new Label();
leftLabel.Text = "Margen Izquierdo";
leftLabel.Font = new Font("Segoe UI", 8, FontStyle.Bold);
leftLabel.ForeColor = ColorTextDim;
leftLabel.Location = new Point(15, 106);
leftLabel.Size = new Size(180, 15);
slidersCard.Controls.Add(leftLabel);
leftValLabel = new Label();
leftValLabel.Text = "0 px";
leftValLabel.Font = new Font("Segoe UI", 8, FontStyle.Bold);
leftValLabel.ForeColor = ColorAccent;
leftValLabel.Location = new Point(230, 106);
leftValLabel.Size = new Size(50, 15);
leftValLabel.TextAlign = ContentAlignment.TopRight;
slidersCard.Controls.Add(leftValLabel);
leftTrackBar = new TrackBar();
leftTrackBar.Minimum = 0;
leftTrackBar.Maximum = 1280;
leftTrackBar.TickStyle = TickStyle.None;
leftTrackBar.AutoSize = false;
leftTrackBar.Location = new Point(10, 124);
leftTrackBar.Size = new Size(270, 22);
leftTrackBar.Scroll += (s, e) => {
currentLeftMargin = leftTrackBar.Value;
leftValLabel.Text = currentLeftMargin + " px";
OnSliderScrolled();
previewPanel.Invalidate();
};
slidersCard.Controls.Add(leftTrackBar);
// Right Margin Slider
Label rightLabel = new Label();
rightLabel.Text = "Margen Derecho";
rightLabel.Font = new Font("Segoe UI", 8, FontStyle.Bold);
rightLabel.ForeColor = ColorTextDim;
rightLabel.Location = new Point(15, 154);
rightLabel.Size = new Size(180, 15);
slidersCard.Controls.Add(rightLabel);
rightValLabel = new Label();
rightValLabel.Text = "0 px";
rightValLabel.Font = new Font("Segoe UI", 8, FontStyle.Bold);
rightValLabel.ForeColor = ColorAccent;
rightValLabel.Location = new Point(230, 154);
rightValLabel.Size = new Size(50, 15);
rightValLabel.TextAlign = ContentAlignment.TopRight;
slidersCard.Controls.Add(rightValLabel);
rightTrackBar = new TrackBar();
rightTrackBar.Minimum = 0;
rightTrackBar.Maximum = 1280;
rightTrackBar.TickStyle = TickStyle.None;
rightTrackBar.AutoSize = false;
rightTrackBar.Location = new Point(10, 172);
rightTrackBar.Size = new Size(270, 22);
rightTrackBar.Scroll += (s, e) => {
currentRightMargin = rightTrackBar.Value;
rightValLabel.Text = currentRightMargin + " px";
OnSliderScrolled();
previewPanel.Invalidate();
};
slidersCard.Controls.Add(rightTrackBar);
// Extra checkboxes
startupCheckBox = new CheckBox();
startupCheckBox.Text = "Iniciar automáticamente con Windows";
startupCheckBox.Font = new Font("Segoe UI", 8);
startupCheckBox.ForeColor = ColorTextDim;
startupCheckBox.Location = new Point(20, 350);
startupCheckBox.Size = new Size(290, 18);
startupCheckBox.FlatStyle = FlatStyle.Flat;
startupCheckBox.CheckedChanged += StartupCheckBox_CheckedChanged;
leftPanel.Controls.Add(startupCheckBox);
closeToTrayCheckBox = new CheckBox();
closeToTrayCheckBox.Text = "Minimizar a la bandeja al cerrar (X)";
closeToTrayCheckBox.Font = new Font("Segoe UI", 8);
closeToTrayCheckBox.ForeColor = ColorTextDim;
closeToTrayCheckBox.Location = new Point(20, 370);
closeToTrayCheckBox.Size = new Size(290, 18);
closeToTrayCheckBox.FlatStyle = FlatStyle.Flat;
closeToTrayCheckBox.Checked = true;
leftPanel.Controls.Add(closeToTrayCheckBox);
blockCursorCheckBox = new CheckBox();
blockCursorCheckBox.Text = "Bloquear mouse en la franja rota (Barrera)";
blockCursorCheckBox.Font = new Font("Segoe UI", 8, FontStyle.Bold);
blockCursorCheckBox.ForeColor = ColorAccent;
blockCursorCheckBox.Location = new Point(20, 390);
blockCursorCheckBox.Size = new Size(290, 18);
blockCursorCheckBox.FlatStyle = FlatStyle.Flat;
blockCursorCheckBox.Checked = true;
blockCursorCheckBox.CheckedChanged += (s, e) => SaveConfig();
leftPanel.Controls.Add(blockCursorCheckBox);
integrateTaskbarCheckBox = new CheckBox();
integrateTaskbarCheckBox.Text = "Integrar barra de tareas en margen inferior";
integrateTaskbarCheckBox.Font = new Font("Segoe UI", 8, FontStyle.Bold);
integrateTaskbarCheckBox.ForeColor = ColorAccent;
integrateTaskbarCheckBox.Location = new Point(20, 410);
integrateTaskbarCheckBox.Size = new Size(290, 18);
integrateTaskbarCheckBox.FlatStyle = FlatStyle.Flat;
integrateTaskbarCheckBox.Checked = false;
integrateTaskbarCheckBox.CheckedChanged += (s, e) => {
OnSliderScrolled();
SaveConfig();
};
leftPanel.Controls.Add(integrateTaskbarCheckBox);
// Action Buttons
applyButton = new Button();
applyButton.Text = "APLICAR MÁRGENES";
applyButton.Font = new Font("Segoe UI", 9, FontStyle.Bold);
applyButton.BackColor = ColorAccent;
applyButton.ForeColor = ColorHeader;
applyButton.FlatStyle = FlatStyle.Flat;
applyButton.FlatAppearance.BorderSize = 0;
applyButton.Size = new Size(180, 35);
applyButton.Location = new Point(20, 445);
applyButton.Click += ApplyButton_Click;
leftPanel.Controls.Add(applyButton);
resetButton = new Button();
resetButton.Text = "RESTABLECER";
resetButton.Font = new Font("Segoe UI", 9, FontStyle.Bold);
resetButton.BackColor = Color.FromArgb(55, 55, 65);
resetButton.ForeColor = ColorTextLight;
resetButton.FlatStyle = FlatStyle.Flat;
resetButton.FlatAppearance.BorderSize = 0;
resetButton.Size = new Size(100, 35);
resetButton.Location = new Point(210, 445);
resetButton.Click += ResetButton_Click;
leftPanel.Controls.Add(resetButton);
// Right panel (Preview Area)
Panel rightPanel = new Panel();
rightPanel.Size = new Size(390, this.Height - 40);
rightPanel.Location = new Point(330, 40);
rightPanel.BackColor = Color.FromArgb(22, 22, 28);
this.Controls.Add(rightPanel);
Label previewTitle = new Label();
previewTitle.Text = "VISTA PREVIA DE PANTALLA EN VIVO";
previewTitle.Font = new Font("Segoe UI", 8, FontStyle.Bold);
previewTitle.ForeColor = ColorTextDim;
previewTitle.Location = new Point(20, 15);
previewTitle.Size = new Size(300, 20);
rightPanel.Controls.Add(previewTitle);
// Live Preview Canvas
previewPanel = new Panel();
previewPanel.Size = new Size(350, 210);
previewPanel.Location = new Point(20, 38);
previewPanel.BackColor = Color.FromArgb(12, 12, 16);
previewPanel.Paint += PreviewPanel_Paint;
rightPanel.Controls.Add(previewPanel);
// Dimmer card
Panel dimmerCard = new Panel();
dimmerCard.Size = new Size(350, 85);
dimmerCard.Location = new Point(20, 270);
dimmerCard.BackColor = ColorCard;
rightPanel.Controls.Add(dimmerCard);
Label dimmerTitle = new Label();
dimmerTitle.Text = "Atenuador de Pantalla Nocturno";
dimmerTitle.Font = new Font("Segoe UI", 8.5f, FontStyle.Bold);
dimmerTitle.ForeColor = ColorTextLight;
dimmerTitle.Location = new Point(15, 10);
dimmerTitle.Size = new Size(200, 15);
dimmerCard.Controls.Add(dimmerTitle);
dimmerValLabel = new Label();
dimmerValLabel.Text = "0%";
dimmerValLabel.Font = new Font("Segoe UI", 8.5f, FontStyle.Bold);
dimmerValLabel.ForeColor = ColorAccent;
dimmerValLabel.Location = new Point(285, 10);
dimmerValLabel.Size = new Size(50, 15);
dimmerValLabel.TextAlign = ContentAlignment.TopRight;
dimmerCard.Controls.Add(dimmerValLabel);
dimmerTrackBar = new TrackBar();
dimmerTrackBar.Minimum = 0;
dimmerTrackBar.Maximum = 90;
dimmerTrackBar.TickStyle = TickStyle.None;
dimmerTrackBar.Location = new Point(10, 32);
dimmerTrackBar.Size = new Size(330, 30);
dimmerTrackBar.Scroll += (s, e) => {
currentDimmerOpacity = dimmerTrackBar.Value;
dimmerValLabel.Text = currentDimmerOpacity + "%";
previewPanel.Invalidate();
// Real-time update
if (selectedScreen != null)
{
if (!configMap.ContainsKey(selectedScreen.DeviceName))
{
configMap[selectedScreen.DeviceName] = new MarginConfig();
}
configMap[selectedScreen.DeviceName].DeviceName = selectedScreen.DeviceName;
configMap[selectedScreen.DeviceName].DimmerOpacity = currentDimmerOpacity;
UpdateDimmers();
}
};
dimmerCard.Controls.Add(dimmerTrackBar);
// Legend card
Panel legendCard = new Panel();
legendCard.Size = new Size(350, 75);
legendCard.Location = new Point(20, 365);
legendCard.BackColor = ColorCard;
rightPanel.Controls.Add(legendCard);
Label infoTitle = new Label();
infoTitle.Text = "Información del Área de Trabajo";
infoTitle.Font = new Font("Segoe UI", 8.5f, FontStyle.Bold);
infoTitle.ForeColor = ColorTextLight;
infoTitle.Location = new Point(15, 8);
infoTitle.Size = new Size(200, 15);
legendCard.Controls.Add(infoTitle);
// Legend indicators
Panel redDot = new Panel() { Size = new Size(10, 10), Location = new Point(15, 30), BackColor = ColorRed };
legendCard.Controls.Add(redDot);
Label redText = new Label() { Text = "Zona Excluida (Borde de pantalla)", ForeColor = ColorTextDim, Font = new Font("Segoe UI", 8), Location = new Point(32, 28), Size = new Size(300, 15) };
legendCard.Controls.Add(redText);
Panel tealDot = new Panel() { Size = new Size(10, 10), Location = new Point(15, 50), BackColor = ColorAccent };
legendCard.Controls.Add(tealDot);
Label tealText = new Label() { Text = "Área Segura Utilizable", ForeColor = ColorTextDim, Font = new Font("Segoe UI", 8), Location = new Point(32, 48), Size = new Size(300, 15) };
legendCard.Controls.Add(tealText);
Button aboutButton = new Button();
aboutButton.Text = "Acerca de...";
aboutButton.Font = new Font("Segoe UI", 8, FontStyle.Bold);
aboutButton.BackColor = Color.FromArgb(55, 55, 65);
aboutButton.ForeColor = ColorTextLight;
aboutButton.FlatStyle = FlatStyle.Flat;
aboutButton.FlatAppearance.BorderSize = 0;
aboutButton.Size = new Size(100, 25);
aboutButton.Location = new Point(270, 448);
aboutButton.Click += (s, e) => {
DialogResult res = MessageBox.Show("SafeDisplay - Corrección de Pantalla\n\n© Service PC Glew 2026\n\n¿Deseas visitar nuestro GitHub para más proyectos?", "Acerca de...", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (res == DialogResult.Yes) {
System.Diagnostics.Process.Start("https://github.com/servicepcglew");
}
};
rightPanel.Controls.Add(aboutButton);
Button donateButton = new Button();
donateButton.Text = "👍 Apoyar en Matecito";
donateButton.Font = new Font("Segoe UI", 8, FontStyle.Bold);
donateButton.BackColor = Color.FromArgb(245, 124, 0); // Orange / Naranja
donateButton.ForeColor = Color.White;
donateButton.FlatStyle = FlatStyle.Flat;
donateButton.FlatAppearance.BorderSize = 0;
donateButton.Size = new Size(150, 25);
donateButton.Location = new Point(20, 448);
donateButton.Cursor = Cursors.Hand;
donateButton.Click += (s, e) => {
// Abre el link de donación en el navegador (matecito)
System.Diagnostics.Process.Start("https://matecito.co/servicepcglew");
};
rightPanel.Controls.Add(donateButton);
// System Tray Setup
trayMenu = new ContextMenu();
trayMenu.MenuItems.Add("Mostrar Configuración", (s, e) => ShowMainForm());
trayMenu.MenuItems.Add("Restablecer Todo", (s, e) => {
ResetAllMargins();
MessageBox.Show("Márgenes restablecidos por completo.", "SafeDisplay", MessageBoxButtons.OK, MessageBoxIcon.Information);
});
trayMenu.MenuItems.Add("-");
trayMenu.MenuItems.Add("Salir", (s, e) => ExitApplication());
trayIcon = new NotifyIcon();
trayIcon.Text = "SafeDisplay - Administrador de Pantalla";
trayIcon.Icon = SystemIcons.Application; // Default icon
trayIcon.ContextMenu = trayMenu;
trayIcon.DoubleClick += (s, e) => ShowMainForm();
trayIcon.Visible = true;
}
private void Header_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(this.Handle, WM_NCLBUTTONDOWN, (IntPtr)HT_CAPTION, IntPtr.Zero);
}
}
private void CloseButton_Click(object sender, EventArgs e)
{
if (closeToTrayCheckBox.Checked)
{
HideToTray();
}
else
{
ExitApplication();
}
}
private void HideToTray()
{
this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false;
this.Hide();
trayIcon.ShowBalloonTip(3000, "SafeDisplay Activo", "La aplicación se está ejecutando en segundo plano para mantener los márgenes de pantalla.", ToolTipIcon.Info);
}
private void ShowMainForm()
{
this.Show();
this.WindowState = FormWindowState.Normal;
this.ShowInTaskbar = true;
this.Activate();
}
private void ExitApplication()
{
isExiting = true;
mouseTimer.Stop();
SystemEvents.DisplaySettingsChanged -= SystemEvents_DisplaySettingsChanged;
// Restore original work areas by removing all AppBars
CloseAllAppBars();
CloseAllDimmers();
trayIcon.Visible = false;
trayIcon.Dispose();
Application.Exit();
}
protected override void OnShown(EventArgs e)
{
base.OnShown(e);
if (startMinimized)
{
HideToTray();
}
}
private void OnSliderScrolled()
{
if (isUpdatingSliders) return;
isUpdatingSliders = true;
try
{
presetComboBox.SelectedIndex = 0; // Manual
}
finally
{
isUpdatingSliders = false;
}
}
private void PresetComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (isUpdatingSliders) return;
int idx = presetComboBox.SelectedIndex;
if (idx <= 0) return; // Personalizado / Manual
isUpdatingSliders = true;
try
{
int top = 0, bottom = 0, left = 0, right = 0;
bool integrate = false;
if (idx == 1) // 24.0" Centrado (Barra integrada)
{
left = 142; right = 142; top = 80; bottom = 80; integrate = true;
}
else if (idx == 2) // 24.0" Alineado Abajo (Barra integrada)
{
left = 142; right = 142; top = 160; bottom = 0; integrate = true;
}
else if (idx == 3) // 24.5" Centrado (Barra integrada)
{
left = 118; right = 118; top = 67; bottom = 67; integrate = true;
}
else if (idx == 4) // 24.5" Alineado Abajo (Barra integrada)
{
left = 118; right = 118; top = 134; bottom = 0; integrate = true;
}
else if (idx == 5) // 23.8" Centrado (Barra integrada)
{
left = 152; right = 152; top = 85; bottom = 85; integrate = true;
}
else if (idx == 6) // 23.8" Alineado Abajo (Barra integrada)
{
left = 152; right = 152; top = 170; bottom = 0; integrate = true;
}
currentLeftMargin = left;
currentRightMargin = right;
currentTopMargin = top;
currentBottomMargin = bottom;
integrateTaskbarCheckBox.Checked = integrate;
leftTrackBar.Value = Math.Min(leftTrackBar.Maximum, left);
rightTrackBar.Value = Math.Min(rightTrackBar.Maximum, right);
topTrackBar.Value = Math.Min(topTrackBar.Maximum, top);
bottomTrackBar.Value = Math.Min(bottomTrackBar.Maximum, bottom);
leftValLabel.Text = left + " px";
rightValLabel.Text = right + " px";
topValLabel.Text = top + " px";
bottomValLabel.Text = bottom + " px";
previewPanel.Invalidate();
}
finally
{
isUpdatingSliders = false;
}
}
private void UpdatePresetComboBoxFromMargins()
{
isUpdatingSliders = true;
try
{
bool integrate = integrateTaskbarCheckBox != null ? integrateTaskbarCheckBox.Checked : false;
if (currentLeftMargin == 142 && currentRightMargin == 142 && currentTopMargin == 80 && currentBottomMargin == 80 && !integrate)
{
presetComboBox.SelectedIndex = 1; // 24.0" Centrado (Sin barra)
}
else if (currentLeftMargin == 142 && currentRightMargin == 142 && currentTopMargin == 80 && currentBottomMargin == 80 && integrate)
{
presetComboBox.SelectedIndex = 2; // 24.0" Centrado (Barra integrada)
}
else if (currentLeftMargin == 142 && currentRightMargin == 142 && currentTopMargin == 160 && currentBottomMargin == 0)
{
presetComboBox.SelectedIndex = 3; // 24.0" Alineado Abajo
}
else if (currentLeftMargin == 118 && currentRightMargin == 118 && currentTopMargin == 67 && currentBottomMargin == 67 && !integrate)
{
presetComboBox.SelectedIndex = 4; // 24.5" Centrado (Sin barra)
}
else if (currentLeftMargin == 118 && currentRightMargin == 118 && currentTopMargin == 67 && currentBottomMargin == 67 && integrate)
{
presetComboBox.SelectedIndex = 5; // 24.5" Centrado (Barra integrada)
}
else if (currentLeftMargin == 118 && currentRightMargin == 118 && currentTopMargin == 134 && currentBottomMargin == 0)
{
presetComboBox.SelectedIndex = 6; // 24.5" Alineado Abajo
}
else if (currentLeftMargin == 152 && currentRightMargin == 152 && currentTopMargin == 85 && currentBottomMargin == 85 && !integrate)
{
presetComboBox.SelectedIndex = 7; // 23.8" Centrado (Sin barra)
}
else if (currentLeftMargin == 152 && currentRightMargin == 152 && currentTopMargin == 85 && currentBottomMargin == 85 && integrate)
{
presetComboBox.SelectedIndex = 8; // 23.8" Centrado (Barra integrada)
}
else if (currentLeftMargin == 152 && currentRightMargin == 152 && currentTopMargin == 170 && currentBottomMargin == 0)
{
presetComboBox.SelectedIndex = 9; // 23.8" Alineado Abajo
}
else
{
presetComboBox.SelectedIndex = 0; // Manual
}
}
finally
{
isUpdatingSliders = false;
}
}
private void PopulateMonitors()
{
monitorComboBox.Items.Clear();
Screen[] screens = Screen.AllScreens;
for (int i = 0; i < screens.Length; i++)
{
string info = string.Format("Pantalla {0} ({1}x{2}) {3}",
i + 1,
screens[i].Bounds.Width,
screens[i].Bounds.Height,
screens[i].Primary ? "[Principal]" : "");
monitorComboBox.Items.Add(info);
}
if (screens.Length > 0)
{
monitorComboBox.SelectedIndex = 0;
}
}
private void MonitorComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
int idx = monitorComboBox.SelectedIndex;
if (idx >= 0 && idx < Screen.AllScreens.Length)
{
selectedScreen = Screen.AllScreens[idx];
bool integrate = false;
// Load current values from our configuration map
if (configMap.ContainsKey(selectedScreen.DeviceName))
{
var cfg = configMap[selectedScreen.DeviceName];
currentTopMargin = cfg.Top;
currentBottomMargin = cfg.Bottom;
currentLeftMargin = cfg.Left;
currentRightMargin = cfg.Right;
currentDimmerOpacity = cfg.DimmerOpacity;
integrate = cfg.IntegrateTaskbar;
}
else
{
currentTopMargin = 0;
currentBottomMargin = 0;
currentLeftMargin = 0;
currentRightMargin = 0;
currentDimmerOpacity = 0;
integrate = false;
}
// Update controls
topTrackBar.Value = Math.Min(topTrackBar.Maximum, currentTopMargin);
bottomTrackBar.Value = Math.Min(bottomTrackBar.Maximum, currentBottomMargin);
leftTrackBar.Value = Math.Min(leftTrackBar.Maximum, currentLeftMargin);
rightTrackBar.Value = Math.Min(rightTrackBar.Maximum, currentRightMargin);
dimmerTrackBar.Value = Math.Min(dimmerTrackBar.Maximum, currentDimmerOpacity);
if (integrateTaskbarCheckBox != null)
{
integrateTaskbarCheckBox.Checked = integrate;
}
topValLabel.Text = currentTopMargin + " px";
bottomValLabel.Text = currentBottomMargin + " px";
leftValLabel.Text = currentLeftMargin + " px";
rightValLabel.Text = currentRightMargin + " px";
dimmerValLabel.Text = currentDimmerOpacity + "%";
UpdatePresetComboBoxFromMargins();
previewPanel.Invalidate();
}
}
private void PreviewPanel_Paint(object sender, PaintEventArgs e)
{
if (selectedScreen == null) return;
Graphics g = e.Graphics;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
int pw = previewPanel.Width;
int ph = previewPanel.Height;
// Maintain aspect ratio inside the canvas
double screenAspect = (double)selectedScreen.Bounds.Width / selectedScreen.Bounds.Height;
double panelAspect = (double)pw / ph;
int mw, mh;
if (screenAspect > panelAspect)
{
mw = pw - 40;
mh = (int)(mw / screenAspect);
}
else
{
mh = ph - 40;
mw = (int)(mh * screenAspect);
}
int mx = (pw - mw) / 2;
int my = (ph - mh) / 2;
// Draw monitor body background
using (Brush bgBrush = new SolidBrush(Color.FromArgb(40, 40, 48)))
{
g.FillRectangle(bgBrush, mx, my, mw, mh);
}
double scaleX = (double)mw / selectedScreen.Bounds.Width;
double scaleY = (double)mh / selectedScreen.Bounds.Height;
int topPx = (int)(currentTopMargin * scaleY);
int bottomPx = (int)(currentBottomMargin * scaleY);
int leftPx = (int)(currentLeftMargin * scaleX);
int rightPx = (int)(currentRightMargin * scaleX);
// Shaded dead area (broken/inaccessible)
using (Brush deadBrush = new SolidBrush(Color.FromArgb(140, 231, 76, 60))) // Semi-transparent Red
{
if (topPx > 0) g.FillRectangle(deadBrush, mx, my, mw, topPx);
if (bottomPx > 0) g.FillRectangle(deadBrush, mx, my + mh - bottomPx, mw, bottomPx);
if (leftPx > 0) g.FillRectangle(deadBrush, mx, my, leftPx, mh);
if (rightPx > 0) g.FillRectangle(deadBrush, mx + mw - rightPx, my, rightPx, mh);
}
// Draw border of physical monitor
using (Pen borderPen = new Pen(Color.FromArgb(90, 90, 100), 2))
{
g.DrawRectangle(borderPen, mx, my, mw, mh);
}
// Draw border of usable safe area
int safeX = mx + leftPx;
int safeY = my + topPx;
int safeW = mw - leftPx - rightPx;
int safeH = mh - topPx - bottomPx;
if (safeW > 0 && safeH > 0)
{
using (Pen safePen = new Pen(ColorAccent, 2))
{
safePen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
g.DrawRectangle(safePen, safeX, safeY, safeW, safeH);
}