mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-07 19:57:07 +02:00
[TextExtractor] Fix focus on overlay on the first activation (#21098)
* TextExtractor-20950: fix focus on overlay on the first TextExtractor activation * TextExtractor-20950: fix typo
This commit is contained in:
@@ -69,8 +69,20 @@ public static class OSInterop
|
|||||||
internal static extern short GetAsyncKeyState(int vKey);
|
internal static extern short GetAsyncKeyState(int vKey);
|
||||||
|
|
||||||
public delegate IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam);
|
public delegate IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam);
|
||||||
#pragma warning restore CA1401 // P/Invokes should not be visible
|
|
||||||
|
|
||||||
|
[DllImport("user32.dll", SetLastError = true)]
|
||||||
|
public static extern IntPtr SetForegroundWindow(IntPtr hWnd);
|
||||||
|
|
||||||
|
[DllImport("user32.dll")]
|
||||||
|
public static extern IntPtr GetForegroundWindow();
|
||||||
|
|
||||||
|
[DllImport("user32.dll")]
|
||||||
|
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr processId);
|
||||||
|
|
||||||
|
[DllImport("user32.dll")]
|
||||||
|
public static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach);
|
||||||
|
|
||||||
|
#pragma warning restore CA1401 // P/Invokes should not be visible
|
||||||
[StructLayout(LayoutKind.Sequential)]
|
[StructLayout(LayoutKind.Sequential)]
|
||||||
internal struct LowLevelKeyboardInputEvent
|
internal struct LowLevelKeyboardInputEvent
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -2,7 +2,6 @@
|
|||||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||||
// See the LICENSE file in the project root for more information.
|
// See the LICENSE file in the project root for more information.
|
||||||
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Windows;
|
using System.Windows;
|
||||||
using System.Windows.Forms;
|
using System.Windows.Forms;
|
||||||
using Microsoft.PowerToys.Telemetry;
|
using Microsoft.PowerToys.Telemetry;
|
||||||
@@ -16,36 +15,12 @@ public static class WindowUtilities
|
|||||||
{
|
{
|
||||||
if (IsOCROverlayCreated())
|
if (IsOCROverlayCreated())
|
||||||
{
|
{
|
||||||
Logger.LogWarning("Tired to launch the overlay but it was already created.");
|
Logger.LogWarning("Tried to launch the overlay, but it has been already created.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Screen[] allScreens = Screen.AllScreens;
|
foreach (Screen screen in Screen.AllScreens)
|
||||||
WindowCollection allWindows = System.Windows.Application.Current.Windows;
|
|
||||||
|
|
||||||
List<OCROverlay> allFullscreenGrab = new List<OCROverlay>();
|
|
||||||
|
|
||||||
foreach (Screen screen in allScreens)
|
|
||||||
{
|
{
|
||||||
bool screenHasWindow = true;
|
|
||||||
|
|
||||||
foreach (Window window in allWindows)
|
|
||||||
{
|
|
||||||
System.Drawing.Point windowCenter =
|
|
||||||
new System.Drawing.Point(
|
|
||||||
(int)(window.Left + (window.Width / 2)),
|
|
||||||
(int)(window.Top + (window.Height / 2)));
|
|
||||||
screenHasWindow = screen.Bounds.Contains(windowCenter);
|
|
||||||
|
|
||||||
// if (window is EditTextWindow)
|
|
||||||
// isEditWindowOpen = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (allWindows.Count < 1)
|
|
||||||
{
|
|
||||||
screenHasWindow = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
OCROverlay overlay = new OCROverlay()
|
OCROverlay overlay = new OCROverlay()
|
||||||
{
|
{
|
||||||
WindowStartupLocation = WindowStartupLocation.Manual,
|
WindowStartupLocation = WindowStartupLocation.Manual,
|
||||||
@@ -73,8 +48,7 @@ public static class WindowUtilities
|
|||||||
}
|
}
|
||||||
|
|
||||||
overlay.Show();
|
overlay.Show();
|
||||||
overlay.Activate();
|
ActivateWindow(overlay);
|
||||||
allFullscreenGrab.Add(overlay);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
PowerToysTelemetry.Log.WriteEvent(new PowerOCR.Telemetry.PowerOCRInvokedEvent());
|
PowerToysTelemetry.Log.WriteEvent(new PowerOCR.Telemetry.PowerOCRInvokedEvent());
|
||||||
@@ -110,4 +84,24 @@ public static class WindowUtilities
|
|||||||
// TODO: Decide when to close the process
|
// TODO: Decide when to close the process
|
||||||
// System.Windows.Application.Current.Shutdown();
|
// System.Windows.Application.Current.Shutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void ActivateWindow(Window window)
|
||||||
|
{
|
||||||
|
var handle = new System.Windows.Interop.WindowInteropHelper(window).Handle;
|
||||||
|
var fgHandle = OSInterop.GetForegroundWindow();
|
||||||
|
|
||||||
|
var threadId1 = OSInterop.GetWindowThreadProcessId(handle, System.IntPtr.Zero);
|
||||||
|
var threadId2 = OSInterop.GetWindowThreadProcessId(fgHandle, System.IntPtr.Zero);
|
||||||
|
|
||||||
|
if (threadId1 != threadId2)
|
||||||
|
{
|
||||||
|
OSInterop.AttachThreadInput(threadId1, threadId2, true);
|
||||||
|
OSInterop.SetForegroundWindow(handle);
|
||||||
|
OSInterop.AttachThreadInput(threadId1, threadId2, false);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
OSInterop.SetForegroundWindow(handle);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user