mirror of
https://github.com/microsoft/PowerToys
synced 2025-08-31 22:45:32 +00:00
holy shit it worked
I'm POSITIVE there's a lot of places where this won't. But if we just exclude TOOLWINDOW's from the EnumWindows call, we can toss FG back pretty consistently?
This commit is contained in:
@@ -298,6 +298,34 @@ public sealed partial class MainWindow : WindowEx,
|
||||
|
||||
private void HideWindow()
|
||||
{
|
||||
// now, use EnumWindows to find the first HWND that's not us, and set it as the foreground window
|
||||
PInvoke.EnumWindows(
|
||||
(hwnd, lParam) =>
|
||||
{
|
||||
if (hwnd != _hwnd && PInvoke.IsWindowVisible(hwnd))
|
||||
{
|
||||
// // and make sure the HWND's client area is not empty
|
||||
// if (PInvoke.GetClientRect(hwnd, out var rect) && rect.Width <= 1 && rect.Height <= 1)
|
||||
// {
|
||||
// return true; // continue enumerating
|
||||
// }
|
||||
|
||||
// and make sure it's not a tool window
|
||||
var exStyle = (WINDOW_EX_STYLE)PInvoke.GetWindowLong(hwnd, WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE);
|
||||
|
||||
if (exStyle.HasFlag(WINDOW_EX_STYLE.WS_EX_TOOLWINDOW))
|
||||
{
|
||||
return true; // continue enumerating
|
||||
}
|
||||
|
||||
PInvoke.SetForegroundWindow(hwnd);
|
||||
return false; // stop enumerating
|
||||
}
|
||||
|
||||
return true; // continue enumerating
|
||||
},
|
||||
IntPtr.Zero);
|
||||
|
||||
// Hide our window
|
||||
|
||||
// Instead of hiding the window, cloak it from DWM
|
||||
|
@@ -48,3 +48,9 @@ MessageBox
|
||||
DwmGetWindowAttribute
|
||||
DwmSetWindowAttribute
|
||||
DWM_CLOAKED_APP
|
||||
|
||||
EnumWindows
|
||||
IsWindowVisible
|
||||
GetClientRect
|
||||
GetWindowLong
|
||||
WINDOW_EX_STYLE
|
@@ -4,11 +4,10 @@
|
||||
|
||||
using CommunityToolkit.Mvvm.Messaging;
|
||||
using Microsoft.CmdPal.Common.Messages;
|
||||
using Microsoft.CmdPal.Ext.ClipboardHistory.Helpers;
|
||||
using Microsoft.CmdPal.Ext.ClipboardHistory.Models;
|
||||
using Microsoft.CommandPalette.Extensions.Toolkit;
|
||||
|
||||
using Windows.ApplicationModel.DataTransfer;
|
||||
|
||||
namespace Microsoft.CmdPal.Ext.ClipboardHistory.Commands;
|
||||
|
||||
internal sealed partial class PasteCommand : InvokableCommand
|
||||
@@ -37,8 +36,22 @@ internal sealed partial class PasteCommand : InvokableCommand
|
||||
{
|
||||
ClipboardHelper.SetClipboardContent(_clipboardItem, _clipboardFormat);
|
||||
HideWindow();
|
||||
|
||||
// Get the title of the current foreground window
|
||||
var foregroundWindow = NativeMethods.GetForegroundWindow();
|
||||
if (foregroundWindow == 0)
|
||||
{
|
||||
return CommandResult.ShowToast("No active window to paste into.");
|
||||
}
|
||||
|
||||
// now get it's title
|
||||
var title = NativeMethods.GetWindowTitle(foregroundWindow);
|
||||
|
||||
ExtensionHost.LogMessage($"Pasting into window: '{title}'");
|
||||
|
||||
ClipboardHelper.SendPasteKeyCombination();
|
||||
Clipboard.DeleteItemFromHistory(_clipboardItem.Item);
|
||||
|
||||
// Clipboard.DeleteItemFromHistory(_clipboardItem.Item);
|
||||
return CommandResult.ShowToast("Pasting");
|
||||
}
|
||||
}
|
||||
|
@@ -63,7 +63,8 @@ internal static class ClipboardHelper
|
||||
ClipboardThreadQueue.EnqueueTask(() =>
|
||||
{
|
||||
Clipboard.SetContent(output);
|
||||
Flush();
|
||||
|
||||
// Flush();
|
||||
ExtensionHost.LogMessage(new LogMessage() { Message = "Copied text to clipboard" });
|
||||
});
|
||||
}
|
||||
|
@@ -4,6 +4,7 @@
|
||||
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using Windows.Foundation;
|
||||
|
||||
namespace Microsoft.CmdPal.Ext.ClipboardHistory.Helpers;
|
||||
@@ -104,4 +105,21 @@ public static partial class NativeMethods
|
||||
|
||||
[LibraryImport("ole32.dll")]
|
||||
internal static partial void CoUninitialize();
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
internal static extern IntPtr GetForegroundWindow();
|
||||
|
||||
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
|
||||
internal static extern int GetWindowTextLength(IntPtr hWnd);
|
||||
|
||||
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
|
||||
internal static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
|
||||
|
||||
internal static string GetWindowTitle(IntPtr hWnd)
|
||||
{
|
||||
var length = GetWindowTextLength(hWnd);
|
||||
var sb = new StringBuilder(length + 1);
|
||||
_ = GetWindowText(hWnd, sb, sb.Capacity);
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user