-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
761 lines (642 loc) · 23.3 KB
/
Program.cs
File metadata and controls
761 lines (642 loc) · 23.3 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
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Windows.Automation;
using System.Windows.Automation.Text;
using System.Windows.Forms;
using UiaCom = Interop.UIAutomationClient;
namespace CapsLang;
internal static class Program
{
private const int WH_KEYBOARD_LL = 13;
private const int WM_KEYDOWN = 0x0100;
private const int WM_KEYUP = 0x0101;
private const int WM_SYSKEYDOWN = 0x0104;
private const int WM_SYSKEYUP = 0x0105;
private const int VK_CAPITAL = 0x14;
private const int VK_SHIFT = 0x10;
private const int VK_CONTROL = 0x11;
private const int WM_INPUTLANGCHANGEREQUEST = 0x0050;
private const int INPUTLANGCHANGE_FORWARD = 0x0002;
private static readonly IntPtr HKL_NEXT = new(1);
private static IntPtr hookId = IntPtr.Zero;
private static LowLevelKeyboardProc? hookProc;
private static LanguagePopup? languagePopup;
private static System.Windows.Forms.Timer? languagePopupTimer;
private static AppSettings appSettings = new();
private static readonly Lazy<UiaCom.IUIAutomation> nativeAutomation = new(() => new UiaCom.CUIAutomation8());
[STAThread]
private static void Main()
{
ApplicationConfiguration.Initialize();
appSettings = SettingsStore.Load();
languagePopup = new LanguagePopup();
languagePopupTimer = new System.Windows.Forms.Timer { Interval = 90 };
languagePopupTimer.Tick += (_, _) =>
{
languagePopupTimer.Stop();
ShowIndicator(GetForegroundInputLanguageCode());
};
hookProc = HookCallback;
hookId = SetHook(hookProc);
ForceCapsLockOff();
using var trayIcon = CreateTrayIcon();
Application.ApplicationExit += (_, _) =>
{
trayIcon.Visible = false;
languagePopup?.Close();
if (hookId != IntPtr.Zero)
{
UnhookWindowsHookEx(hookId);
}
};
Application.Run();
}
private static NotifyIcon CreateTrayIcon()
{
var menu = new ContextMenuStrip();
var enabledItem = new ToolStripMenuItem("CapsLang Enabled") { CheckOnClick = true, Checked = appSettings.IsCapsLangEnabled };
var indicatorItem = new ToolStripMenuItem("Show Language Indicator") { CheckOnClick = true, Checked = appSettings.ShowLanguageIndicator };
var startupItem = new ToolStripMenuItem("Start with Windows") { CheckOnClick = true, Checked = StartupShortcut.IsEnabled() };
var followCaretItem = new ToolStripMenuItem("Follow Text Caret");
var screenCornerItem = new ToolStripMenuItem("Screen Corner");
void SaveSettings()
{
SettingsStore.Save(appSettings);
}
void RefreshPositionMenu()
{
followCaretItem.Checked = appSettings.IndicatorPlacement == IndicatorPlacement.FollowCaret;
screenCornerItem.Checked = appSettings.IndicatorPlacement == IndicatorPlacement.ScreenCorner;
}
enabledItem.CheckedChanged += (_, _) =>
{
appSettings.IsCapsLangEnabled = enabledItem.Checked;
SaveSettings();
ShowIndicator(appSettings.IsCapsLangEnabled ? "ON" : "OFF");
};
indicatorItem.CheckedChanged += (_, _) =>
{
appSettings.ShowLanguageIndicator = indicatorItem.Checked;
SaveSettings();
ShowIndicator(appSettings.ShowLanguageIndicator ? "UI ON" : "UI OFF", force: true);
};
startupItem.CheckedChanged += (_, _) =>
{
if (startupItem.Checked)
{
StartupShortcut.Enable();
}
else
{
StartupShortcut.Disable();
}
};
followCaretItem.Click += (_, _) =>
{
appSettings.IndicatorPlacement = IndicatorPlacement.FollowCaret;
RefreshPositionMenu();
SaveSettings();
ShowIndicator("CARET");
};
screenCornerItem.Click += (_, _) =>
{
appSettings.IndicatorPlacement = IndicatorPlacement.ScreenCorner;
RefreshPositionMenu();
SaveSettings();
ShowIndicator("CORNER");
};
RefreshPositionMenu();
var positionMenu = new ToolStripMenuItem("Indicator Position");
positionMenu.DropDownItems.Add(followCaretItem);
positionMenu.DropDownItems.Add(screenCornerItem);
menu.Items.Add(enabledItem);
menu.Items.Add(indicatorItem);
menu.Items.Add(positionMenu);
menu.Items.Add(startupItem);
menu.Items.Add(new ToolStripSeparator());
menu.Items.Add("Turn CapsLock Off", null, (_, _) =>
{
ForceCapsLockOff();
ShowIndicator("CAPS OFF", force: true, pointerInitiated: true);
});
menu.Items.Add(new ToolStripSeparator());
menu.Items.Add("Exit", null, (_, _) => Application.Exit());
return new NotifyIcon
{
Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath) ?? SystemIcons.Application,
Text = "CapsLang: CapsLock switches language",
ContextMenuStrip = menu,
Visible = true
};
}
private static IntPtr SetHook(LowLevelKeyboardProc proc)
{
using var currentProcess = Process.GetCurrentProcess();
using var currentModule = currentProcess.MainModule;
var moduleHandle = currentModule?.ModuleName is { Length: > 0 }
? GetModuleHandle(currentModule.ModuleName)
: IntPtr.Zero;
return SetWindowsHookEx(WH_KEYBOARD_LL, proc, moduleHandle, 0);
}
private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (!appSettings.IsCapsLangEnabled)
{
return CallNextHookEx(hookId, nCode, wParam, lParam);
}
if (nCode >= 0)
{
var message = wParam.ToInt32();
var vkCode = Marshal.ReadInt32(lParam);
if (vkCode == VK_CAPITAL)
{
if (message is WM_KEYDOWN or WM_SYSKEYDOWN)
{
if (IsKeyDown(VK_CONTROL))
{
ForceCapsLockOff();
ShowIndicator("CAPS OFF", force: true);
}
else if (IsKeyDown(VK_SHIFT))
{
ToggleCapsLock();
ShowIndicator(IsCapsLockOn() ? "CAPS ON" : "CAPS OFF", force: true);
}
else
{
ForceCapsLockOff();
SwitchToNextInputLanguage();
languagePopupTimer?.Stop();
languagePopupTimer?.Start();
}
}
if (message is WM_KEYDOWN or WM_KEYUP or WM_SYSKEYDOWN or WM_SYSKEYUP)
{
return new IntPtr(1);
}
}
}
return CallNextHookEx(hookId, nCode, wParam, lParam);
}
private static void SwitchToNextInputLanguage()
{
var foregroundWindow = GetForegroundWindow();
if (foregroundWindow != IntPtr.Zero)
{
PostMessage(foregroundWindow, WM_INPUTLANGCHANGEREQUEST, new IntPtr(INPUTLANGCHANGE_FORWARD), HKL_NEXT);
}
}
private static void ForceCapsLockOff()
{
if (IsCapsLockOn())
{
ToggleCapsLock();
}
}
private static bool IsCapsLockOn()
{
return (GetKeyState(VK_CAPITAL) & 1) != 0;
}
private static bool IsKeyDown(int virtualKey)
{
return (GetAsyncKeyState(virtualKey) & 0x8000) != 0;
}
private static void ToggleCapsLock()
{
keybd_event(VK_CAPITAL, 0x45, KEYEVENTF_EXTENDEDKEY, UIntPtr.Zero);
keybd_event(VK_CAPITAL, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, UIntPtr.Zero);
}
private static string GetForegroundInputLanguageCode()
{
var foregroundWindow = GetForegroundWindow();
if (foregroundWindow == IntPtr.Zero)
{
return "??";
}
var threadId = GetWindowThreadProcessId(foregroundWindow, out _);
var layout = GetKeyboardLayout(threadId);
var languageId = layout.ToInt64() & 0xffff;
try
{
return FormatLanguageLabel(CultureInfo.GetCultureInfo((int)languageId), languageId);
}
catch (CultureNotFoundException)
{
return $"0x{languageId:x4}".ToUpperInvariant();
}
}
private static string FormatLanguageLabel(CultureInfo cultureInfo, long languageId)
{
if (!string.IsNullOrWhiteSpace(cultureInfo.Name))
{
return cultureInfo.Name.ToUpperInvariant();
}
if (!string.IsNullOrWhiteSpace(cultureInfo.TwoLetterISOLanguageName) &&
cultureInfo.TwoLetterISOLanguageName.Length == 2 &&
!string.Equals(cultureInfo.TwoLetterISOLanguageName, "iv", StringComparison.OrdinalIgnoreCase))
{
return cultureInfo.TwoLetterISOLanguageName.ToUpperInvariant();
}
var nativeName = Regex.Replace(cultureInfo.NativeName, @"\s*\(.+?\)\s*", " ").Trim();
if (!string.IsNullOrWhiteSpace(nativeName))
{
return nativeName;
}
return $"0x{languageId:x4}".ToUpperInvariant();
}
private static void ShowIndicator(string text, bool force = false, bool pointerInitiated = false)
{
if (languagePopup is null || (!force && !appSettings.ShowLanguageIndicator))
{
return;
}
var anchor = pointerInitiated
? GetPointerPopupAnchor()
: GetIndicatorAnchor();
languagePopup.ShowLanguage(text, anchor);
}
private static Point GetIndicatorAnchor()
{
return appSettings.IndicatorPlacement switch
{
IndicatorPlacement.ScreenCorner => GetScreenCornerPopupAnchor(),
_ => GetInputPopupAnchor()
};
}
private static Point GetScreenCornerPopupAnchor()
{
var screen = Screen.PrimaryScreen?.WorkingArea ?? new Rectangle(0, 0, 800, 600);
return new Point(screen.Right - 72, screen.Bottom - 54);
}
private static Point GetInputPopupAnchor()
{
if (TryGetNativeAutomationCaretAnchor(out var nativeAutomationCaretPoint))
{
return nativeAutomationCaretPoint;
}
if (TryGetAutomationCaretAnchor(out var automationCaretPoint))
{
return automationCaretPoint;
}
var foregroundWindow = GetForegroundWindow();
if (foregroundWindow != IntPtr.Zero)
{
var threadId = GetWindowThreadProcessId(foregroundWindow, out _);
var guiThreadInfo = new GUITHREADINFO();
guiThreadInfo.cbSize = Marshal.SizeOf<GUITHREADINFO>();
if (GetGUIThreadInfo(threadId, ref guiThreadInfo) && guiThreadInfo.hwndCaret != IntPtr.Zero)
{
var caretPoint = new Point(guiThreadInfo.rcCaret.Left, guiThreadInfo.rcCaret.Bottom);
if (ClientToScreen(guiThreadInfo.hwndCaret, ref caretPoint))
{
return caretPoint;
}
}
var fallbackWindow = guiThreadInfo.hwndFocus != IntPtr.Zero
? guiThreadInfo.hwndFocus
: foregroundWindow;
if (GetWindowRect(fallbackWindow, out var focusedWindowRect))
{
return new Point(focusedWindowRect.Left + 16, focusedWindowRect.Bottom - 16);
}
}
var screen = Screen.PrimaryScreen?.WorkingArea ?? new Rectangle(0, 0, 800, 600);
return new Point(screen.Left + 24, screen.Top + 24);
}
private static bool TryGetNativeAutomationCaretAnchor(out Point anchor)
{
anchor = Point.Empty;
try
{
var focusedElement = nativeAutomation.Value.GetFocusedElement();
var textPatternObject = focusedElement.GetCurrentPattern(UiaCom.UIA_PatternIds.UIA_TextPattern2Id);
if (textPatternObject is not UiaCom.IUIAutomationTextPattern2 textPattern)
{
return false;
}
var caretRange = textPattern.GetCaretRange(out var isActive);
if (isActive == 0)
{
return false;
}
return TryGetNativeTextRangeAnchor(caretRange, out anchor);
}
catch (COMException)
{
}
catch (InvalidCastException)
{
}
catch (InvalidOperationException)
{
}
return false;
}
private static bool TryGetNativeTextRangeAnchor(UiaCom.IUIAutomationTextRange? range, out Point anchor)
{
anchor = Point.Empty;
if (range is null)
{
return false;
}
var rectangles = range.GetBoundingRectangles();
for (var index = 0; index + 3 < rectangles.Length; index += 4)
{
var x = rectangles[index];
var y = rectangles[index + 1];
var width = rectangles[index + 2];
var height = rectangles[index + 3];
if (double.IsNaN(x) || double.IsNaN(y) || double.IsInfinity(x) || double.IsInfinity(y))
{
continue;
}
if (width < 0 || height <= 0)
{
continue;
}
anchor = new Point((int)Math.Round(x + Math.Min(width, 2)), (int)Math.Round(y + height));
return true;
}
return false;
}
private static bool TryGetAutomationCaretAnchor(out Point anchor)
{
anchor = Point.Empty;
try
{
var focusedElement = AutomationElement.FocusedElement;
if (focusedElement is null)
{
return false;
}
if (focusedElement.TryGetCurrentPattern(TextPattern.Pattern, out var textPatternObject) &&
textPatternObject is TextPattern textPattern)
{
foreach (var selectionRange in textPattern.GetSelection())
{
if (TryGetTextRangeAnchor(selectionRange, out anchor))
{
return true;
}
}
}
}
catch (ElementNotAvailableException)
{
}
catch (InvalidOperationException)
{
}
catch (COMException)
{
}
return false;
}
private static bool TryGetTextRangeAnchor(TextPatternRange? range, out Point anchor)
{
anchor = Point.Empty;
if (range is null)
{
return false;
}
foreach (var rectangle in range.GetBoundingRectangles())
{
var x = rectangle.X;
var y = rectangle.Y;
var width = rectangle.Width;
var height = rectangle.Height;
if (double.IsNaN(x) || double.IsNaN(y) || double.IsInfinity(x) || double.IsInfinity(y))
{
continue;
}
if (width < 0 || height <= 0)
{
continue;
}
anchor = new Point((int)Math.Round(x + Math.Min(width, 2)), (int)Math.Round(y + height));
return true;
}
return false;
}
private static Point GetPointerPopupAnchor()
{
return GetCursorPos(out var cursorPoint)
? cursorPoint
: new Point(Screen.PrimaryScreen?.WorkingArea.Left + 24 ?? 24, Screen.PrimaryScreen?.WorkingArea.Top + 24 ?? 24);
}
private const uint KEYEVENTF_EXTENDEDKEY = 0x0001;
private const uint KEYEVENTF_KEYUP = 0x0002;
private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
[StructLayout(LayoutKind.Sequential)]
private struct GUITHREADINFO
{
public int cbSize;
public int flags;
public IntPtr hwndActive;
public IntPtr hwndFocus;
public IntPtr hwndCapture;
public IntPtr hwndMenuOwner;
public IntPtr hwndMoveSize;
public IntPtr hwndCaret;
public RECT rcCaret;
}
[StructLayout(LayoutKind.Sequential)]
private struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("user32.dll")]
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr GetModuleHandle(string lpModuleName);
[DllImport("user32.dll")]
private static extern short GetKeyState(int nVirtKey);
[DllImport("user32.dll")]
private static extern short GetAsyncKeyState(int vKey);
[DllImport("user32.dll")]
private static extern void keybd_event(int bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo);
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
[DllImport("user32.dll")]
private static extern IntPtr GetKeyboardLayout(uint idThread);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetGUIThreadInfo(uint idThread, ref GUITHREADINFO lpgui);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool ClientToScreen(IntPtr hWnd, ref Point lpPoint);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetCursorPos(out Point lpPoint);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool PostMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
}
internal enum IndicatorPlacement
{
FollowCaret,
ScreenCorner
}
internal sealed class AppSettings
{
public bool IsCapsLangEnabled { get; set; } = true;
public bool ShowLanguageIndicator { get; set; } = true;
public IndicatorPlacement IndicatorPlacement { get; set; } = IndicatorPlacement.FollowCaret;
}
internal static class SettingsStore
{
private static readonly JsonSerializerOptions JsonOptions = new()
{
WriteIndented = true
};
private static string SettingsDirectory =>
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "CapsLang");
private static string SettingsPath => Path.Combine(SettingsDirectory, "settings.json");
public static AppSettings Load()
{
try
{
if (!File.Exists(SettingsPath))
{
return new AppSettings();
}
return JsonSerializer.Deserialize<AppSettings>(File.ReadAllText(SettingsPath), JsonOptions) ?? new AppSettings();
}
catch (JsonException)
{
return new AppSettings();
}
catch (IOException)
{
return new AppSettings();
}
catch (UnauthorizedAccessException)
{
return new AppSettings();
}
}
public static void Save(AppSettings settings)
{
Directory.CreateDirectory(SettingsDirectory);
File.WriteAllText(SettingsPath, JsonSerializer.Serialize(settings, JsonOptions));
}
}
internal static class StartupShortcut
{
private static string ShortcutPath =>
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Startup), "CapsLang.lnk");
public static bool IsEnabled()
{
return File.Exists(ShortcutPath);
}
public static void Enable()
{
Directory.CreateDirectory(Path.GetDirectoryName(ShortcutPath)!);
var shellType = Type.GetTypeFromProgID("WScript.Shell");
if (shellType is null)
{
throw new InvalidOperationException("WScript.Shell is not available.");
}
dynamic shell = Activator.CreateInstance(shellType)!;
dynamic shortcut = shell.CreateShortcut(ShortcutPath);
shortcut.TargetPath = Application.ExecutablePath;
shortcut.WorkingDirectory = AppContext.BaseDirectory;
shortcut.Description = "Use CapsLock as input language switcher";
shortcut.Save();
}
public static void Disable()
{
if (File.Exists(ShortcutPath))
{
File.Delete(ShortcutPath);
}
}
}
internal sealed class LanguagePopup : Form
{
private const int WS_EX_NOACTIVATE = 0x08000000;
private const int WS_EX_TOOLWINDOW = 0x00000080;
private const int WS_EX_TOPMOST = 0x00000008;
private readonly Label label;
private readonly System.Windows.Forms.Timer hideTimer;
public LanguagePopup()
{
AutoScaleMode = AutoScaleMode.None;
BackColor = Color.FromArgb(28, 28, 30);
ClientSize = new Size(54, 34);
FormBorderStyle = FormBorderStyle.None;
ShowInTaskbar = false;
StartPosition = FormStartPosition.Manual;
TopMost = true;
label = new Label
{
AutoSize = false,
Dock = DockStyle.Fill,
Font = new Font("Segoe UI Variable Display", 12F, FontStyle.Bold, GraphicsUnit.Point),
ForeColor = Color.White,
TextAlign = ContentAlignment.MiddleCenter,
UseMnemonic = false
};
Controls.Add(label);
hideTimer = new System.Windows.Forms.Timer { Interval = 720 };
hideTimer.Tick += (_, _) =>
{
hideTimer.Stop();
Hide();
};
}
protected override bool ShowWithoutActivation => true;
protected override CreateParams CreateParams
{
get
{
var createParams = base.CreateParams;
createParams.ExStyle |= WS_EX_NOACTIVATE | WS_EX_TOOLWINDOW | WS_EX_TOPMOST;
return createParams;
}
}
public void ShowLanguage(string languageCode, Point anchor)
{
label.Text = languageCode;
Width = Math.Max(54, TextRenderer.MeasureText(languageCode, label.Font).Width + 24);
Location = GetPopupLocation(anchor);
hideTimer.Stop();
Show();
BringToFront();
hideTimer.Start();
}
private Point GetPopupLocation(Point anchor)
{
var screen = Screen.FromPoint(anchor).WorkingArea;
var x = anchor.X + 12;
var y = anchor.Y + 10;
if (x + Width > screen.Right)
{
x = anchor.X - Width - 12;
}
if (y + Height > screen.Bottom)
{
y = anchor.Y - Height - 10;
}
x = Math.Clamp(x, screen.Left, screen.Right - Width);
y = Math.Clamp(y, screen.Top, screen.Bottom - Height);
return new Point(x, y);
}
}