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..2f535522 100644 --- a/src/ManagedShell.Common/Helpers/KeyboardLayoutHelper.cs +++ b/src/ManagedShell.Common/Helpers/KeyboardLayoutHelper.cs @@ -1,26 +1,19 @@ using ManagedShell.Common.Structs; using ManagedShell.Interop; using System.Collections.Generic; -using System.Globalization; using System.Linq; +using System; 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() - { - HKL = layout, - NativeName = CultureInfo.GetCultureInfo((short)layout).NativeName, - ThreeLetterName = CultureInfo.GetCultureInfo((short)layout).ThreeLetterISOLanguageName.ToUpper() - }; + return new KeyboardLayout(layout); } public static List GetKeyboardLayoutList() @@ -29,20 +22,12 @@ 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) { - return NativeMethods.PostMessage(0xffff, - (uint) NativeMethods.WM.INPUTLANGCHANGEREQUEST, - 0, - NativeMethods.LoadKeyboardLayout(layoutId.ToString("x8"), (uint)(NativeMethods.KLF.SUBSTITUTE_OK | NativeMethods.KLF.ACTIVATE))); + return NativeMethods.PostMessage(NativeMethods.GetForegroundWindow(), (int)NativeMethods.WM.INPUTLANGCHANGEREQUEST, IntPtr.Zero, new IntPtr(layoutId)); } } } 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.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; + } } } 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 {