From 5d3eae93cfe006c2eb74798b97bad719fb91be94 Mon Sep 17 00:00:00 2001 From: Matt Date: Sun, 6 Mar 2022 18:52:27 +0300 Subject: [PATCH 1/4] Make AppBarWindow to be WS_EX_NOACTIVATE --- src/ManagedShell.AppBar/AppBarWindow.cs | 2 ++ src/ManagedShell.Common/Helpers/KeyboardLayoutHelper.cs | 7 +++---- src/ManagedShell.Common/Helpers/WindowHelper.cs | 8 +++++++- src/ManagedShell.Interop/NativeMethods.User32.cs | 5 ++++- 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/ManagedShell.AppBar/AppBarWindow.cs b/src/ManagedShell.AppBar/AppBarWindow.cs index b6d8303d..dba2410d 100644 --- a/src/ManagedShell.AppBar/AppBarWindow.cs +++ b/src/ManagedShell.AppBar/AppBarWindow.cs @@ -92,6 +92,8 @@ protected virtual void OnSourceInitialized(object sender, EventArgs e) helper = new WindowInteropHelper(this); Handle = helper.Handle; + WindowHelper.SetWindowNoActivate(Handle); + // set up window procedure HwndSource source = HwndSource.FromHwnd(Handle); source.AddHook(WndProc); diff --git a/src/ManagedShell.Common/Helpers/KeyboardLayoutHelper.cs b/src/ManagedShell.Common/Helpers/KeyboardLayoutHelper.cs index 3a9a93cb..d5010b51 100644 --- a/src/ManagedShell.Common/Helpers/KeyboardLayoutHelper.cs +++ b/src/ManagedShell.Common/Helpers/KeyboardLayoutHelper.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Globalization; using System.Linq; +using System; namespace ManagedShell.Common.Helpers { @@ -39,10 +40,8 @@ public static List GetKeyboardLayoutList() public static bool SetKeyboardLayout(int layoutId) { - return NativeMethods.PostMessage(0xffff, - (uint) NativeMethods.WM.INPUTLANGCHANGEREQUEST, - 0, - NativeMethods.LoadKeyboardLayout(layoutId.ToString("x8"), (uint)(NativeMethods.KLF.SUBSTITUTE_OK | NativeMethods.KLF.ACTIVATE))); + var loadedHkl = new IntPtr(NativeMethods.LoadKeyboardLayout(((short)layoutId).ToString("x8"), (uint)NativeMethods.KLF.SUBSTITUTE_OK)); + return NativeMethods.PostMessage(NativeMethods.GetForegroundWindow(), (int)NativeMethods.WM.INPUTLANGCHANGEREQUEST, IntPtr.Zero, loadedHkl); } } } diff --git a/src/ManagedShell.Common/Helpers/WindowHelper.cs b/src/ManagedShell.Common/Helpers/WindowHelper.cs index 640627b5..5ae38a69 100644 --- a/src/ManagedShell.Common/Helpers/WindowHelper.cs +++ b/src/ManagedShell.Common/Helpers/WindowHelper.cs @@ -8,6 +8,12 @@ public static class WindowHelper { public const string TrayWndClass = "Shell_TrayWnd"; + public static void SetWindowNoActivate(IntPtr handle) + { + SetWindowLong(handle, GWL_EXSTYLE, + GetWindowLong(handle, GWL_EXSTYLE) | (int) ExtendedWindowStyles.WS_EX_NOACTIVATE); + } + public static void ShowWindowBottomMost(IntPtr handle) { SetWindowPos( @@ -29,7 +35,7 @@ public static void ShowWindowTopMost(IntPtr handle) 0, 0, 0, - (int)SetWindowPosFlags.SWP_NOSIZE | (int)SetWindowPosFlags.SWP_NOMOVE | (int)SetWindowPosFlags.SWP_SHOWWINDOW/* | (int)SetWindowPosFlags.SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOOWNERZORDER*/); + (int)SetWindowPosFlags.SWP_NOSIZE | (int)SetWindowPosFlags.SWP_NOMOVE | (int)SetWindowPosFlags.SWP_SHOWWINDOW | (int)SetWindowPosFlags.SWP_NOACTIVATE /* | SWP_NOZORDER | SWP_NOOWNERZORDER*/); } public static void ShowWindowDesktop(IntPtr hwnd) diff --git a/src/ManagedShell.Interop/NativeMethods.User32.cs b/src/ManagedShell.Interop/NativeMethods.User32.cs index b833792e..5180fb1d 100644 --- a/src/ManagedShell.Interop/NativeMethods.User32.cs +++ b/src/ManagedShell.Interop/NativeMethods.User32.cs @@ -2997,7 +2997,10 @@ public static extern IntPtr CreateWindowEx( public static extern int ActivateKeyboardLayout(int hkl, uint flags); [DllImport(User32_DllName)] - public static extern bool PostMessage(int hWnd, uint msg, int wParam, long lParam); + public static extern bool PostMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam); + + [DllImport(User32_DllName)] + public static extern IntPtr GetTopWindow(IntPtr hWnd); public enum KLF : uint { From 30aefb2642c09d737c0552d7c3d2a7216f709bb0 Mon Sep 17 00:00:00 2001 From: Matt Date: Sun, 6 Mar 2022 19:05:09 +0300 Subject: [PATCH 2/4] Update KeyboardLayoutHelper --- src/ManagedShell.Common/Helpers/KeyboardLayoutHelper.cs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/ManagedShell.Common/Helpers/KeyboardLayoutHelper.cs b/src/ManagedShell.Common/Helpers/KeyboardLayoutHelper.cs index d5010b51..da310092 100644 --- a/src/ManagedShell.Common/Helpers/KeyboardLayoutHelper.cs +++ b/src/ManagedShell.Common/Helpers/KeyboardLayoutHelper.cs @@ -9,11 +9,9 @@ namespace ManagedShell.Common.Helpers { public static class KeyboardLayoutHelper { - public static KeyboardLayout GetKeyboardLayout(bool currentThread = false) + public static KeyboardLayout GetKeyboardLayout() { - uint threadId = 0; - if (!currentThread) - threadId = NativeMethods.GetWindowThreadProcessId(NativeMethods.GetForegroundWindow(), out _); + uint threadId = NativeMethods.GetWindowThreadProcessId(NativeMethods.GetForegroundWindow(), out _); var layout = NativeMethods.GetKeyboardLayout(threadId); return new KeyboardLayout() From 89d1747546fdcb21cb855a50a266bed2af60e0d8 Mon Sep 17 00:00:00 2001 From: Matt Date: Mon, 7 Mar 2022 12:22:33 +0300 Subject: [PATCH 3/4] Add KeyboardLayout constructor --- .../Helpers/KeyboardLayoutHelper.cs | 15 ++------------- src/ManagedShell.Common/Structs/KeyboardLayout.cs | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/ManagedShell.Common/Helpers/KeyboardLayoutHelper.cs b/src/ManagedShell.Common/Helpers/KeyboardLayoutHelper.cs index da310092..c9f28d88 100644 --- a/src/ManagedShell.Common/Helpers/KeyboardLayoutHelper.cs +++ b/src/ManagedShell.Common/Helpers/KeyboardLayoutHelper.cs @@ -1,7 +1,6 @@ using ManagedShell.Common.Structs; using ManagedShell.Interop; using System.Collections.Generic; -using System.Globalization; using System.Linq; using System; @@ -14,12 +13,7 @@ public static KeyboardLayout GetKeyboardLayout() uint threadId = NativeMethods.GetWindowThreadProcessId(NativeMethods.GetForegroundWindow(), out _); var layout = NativeMethods.GetKeyboardLayout(threadId); - return new KeyboardLayout() - { - HKL = layout, - NativeName = CultureInfo.GetCultureInfo((short)layout).NativeName, - ThreeLetterName = CultureInfo.GetCultureInfo((short)layout).ThreeLetterISOLanguageName.ToUpper() - }; + return new KeyboardLayout(layout); } public static List GetKeyboardLayoutList() @@ -28,12 +22,7 @@ public static List GetKeyboardLayoutList() var result = new long[size]; NativeMethods.GetKeyboardLayoutList(size, result); - return result.Select(x => new KeyboardLayout() - { - HKL = (int)x, - NativeName = CultureInfo.GetCultureInfo((short)x).NativeName, - ThreeLetterName = CultureInfo.GetCultureInfo((short)x).ThreeLetterISOLanguageName.ToUpper() - }).ToList(); + return result.Select(x => new KeyboardLayout((int)x)).ToList(); } public static bool SetKeyboardLayout(int layoutId) diff --git a/src/ManagedShell.Common/Structs/KeyboardLayout.cs b/src/ManagedShell.Common/Structs/KeyboardLayout.cs index fb9bce2b..36b4a217 100644 --- a/src/ManagedShell.Common/Structs/KeyboardLayout.cs +++ b/src/ManagedShell.Common/Structs/KeyboardLayout.cs @@ -1,9 +1,22 @@ -namespace ManagedShell.Common.Structs +using System.Globalization; + +namespace ManagedShell.Common.Structs { public struct KeyboardLayout { public int HKL { get; set; } public string NativeName { get; set; } public string ThreeLetterName { get; set; } + public string DisplayName { get; set; } + + public KeyboardLayout(int hkl) + { + HKL = hkl; + var cultureInfo = CultureInfo.GetCultureInfo((short)hkl); + + NativeName = cultureInfo.NativeName; + ThreeLetterName = cultureInfo.ThreeLetterISOLanguageName.ToUpper(); + DisplayName = cultureInfo.DisplayName; + } } } From cd20b591b7efd281092e236efd26d06753d69e75 Mon Sep 17 00:00:00 2001 From: Matt Date: Mon, 7 Mar 2022 23:45:21 +0300 Subject: [PATCH 4/4] Remove keyboard layout loading --- src/ManagedShell.Common/Helpers/KeyboardLayoutHelper.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/ManagedShell.Common/Helpers/KeyboardLayoutHelper.cs b/src/ManagedShell.Common/Helpers/KeyboardLayoutHelper.cs index c9f28d88..2f535522 100644 --- a/src/ManagedShell.Common/Helpers/KeyboardLayoutHelper.cs +++ b/src/ManagedShell.Common/Helpers/KeyboardLayoutHelper.cs @@ -27,8 +27,7 @@ public static List GetKeyboardLayoutList() public static bool SetKeyboardLayout(int layoutId) { - var loadedHkl = new IntPtr(NativeMethods.LoadKeyboardLayout(((short)layoutId).ToString("x8"), (uint)NativeMethods.KLF.SUBSTITUTE_OK)); - return NativeMethods.PostMessage(NativeMethods.GetForegroundWindow(), (int)NativeMethods.WM.INPUTLANGCHANGEREQUEST, IntPtr.Zero, loadedHkl); + return NativeMethods.PostMessage(NativeMethods.GetForegroundWindow(), (int)NativeMethods.WM.INPUTLANGCHANGEREQUEST, IntPtr.Zero, new IntPtr(layoutId)); } } }