mirror of
https://github.com/microsoft/PowerToys
synced 2025-09-04 08:25:11 +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()
|
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
|
// Hide our window
|
||||||
|
|
||||||
// Instead of hiding the window, cloak it from DWM
|
// Instead of hiding the window, cloak it from DWM
|
||||||
|
@@ -48,3 +48,9 @@ MessageBox
|
|||||||
DwmGetWindowAttribute
|
DwmGetWindowAttribute
|
||||||
DwmSetWindowAttribute
|
DwmSetWindowAttribute
|
||||||
DWM_CLOAKED_APP
|
DWM_CLOAKED_APP
|
||||||
|
|
||||||
|
EnumWindows
|
||||||
|
IsWindowVisible
|
||||||
|
GetClientRect
|
||||||
|
GetWindowLong
|
||||||
|
WINDOW_EX_STYLE
|
@@ -4,11 +4,10 @@
|
|||||||
|
|
||||||
using CommunityToolkit.Mvvm.Messaging;
|
using CommunityToolkit.Mvvm.Messaging;
|
||||||
using Microsoft.CmdPal.Common.Messages;
|
using Microsoft.CmdPal.Common.Messages;
|
||||||
|
using Microsoft.CmdPal.Ext.ClipboardHistory.Helpers;
|
||||||
using Microsoft.CmdPal.Ext.ClipboardHistory.Models;
|
using Microsoft.CmdPal.Ext.ClipboardHistory.Models;
|
||||||
using Microsoft.CommandPalette.Extensions.Toolkit;
|
using Microsoft.CommandPalette.Extensions.Toolkit;
|
||||||
|
|
||||||
using Windows.ApplicationModel.DataTransfer;
|
|
||||||
|
|
||||||
namespace Microsoft.CmdPal.Ext.ClipboardHistory.Commands;
|
namespace Microsoft.CmdPal.Ext.ClipboardHistory.Commands;
|
||||||
|
|
||||||
internal sealed partial class PasteCommand : InvokableCommand
|
internal sealed partial class PasteCommand : InvokableCommand
|
||||||
@@ -37,8 +36,22 @@ internal sealed partial class PasteCommand : InvokableCommand
|
|||||||
{
|
{
|
||||||
ClipboardHelper.SetClipboardContent(_clipboardItem, _clipboardFormat);
|
ClipboardHelper.SetClipboardContent(_clipboardItem, _clipboardFormat);
|
||||||
HideWindow();
|
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();
|
ClipboardHelper.SendPasteKeyCombination();
|
||||||
Clipboard.DeleteItemFromHistory(_clipboardItem.Item);
|
|
||||||
|
// Clipboard.DeleteItemFromHistory(_clipboardItem.Item);
|
||||||
return CommandResult.ShowToast("Pasting");
|
return CommandResult.ShowToast("Pasting");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -63,7 +63,8 @@ internal static class ClipboardHelper
|
|||||||
ClipboardThreadQueue.EnqueueTask(() =>
|
ClipboardThreadQueue.EnqueueTask(() =>
|
||||||
{
|
{
|
||||||
Clipboard.SetContent(output);
|
Clipboard.SetContent(output);
|
||||||
Flush();
|
|
||||||
|
// Flush();
|
||||||
ExtensionHost.LogMessage(new LogMessage() { Message = "Copied text to clipboard" });
|
ExtensionHost.LogMessage(new LogMessage() { Message = "Copied text to clipboard" });
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
|
using System.Text;
|
||||||
using Windows.Foundation;
|
using Windows.Foundation;
|
||||||
|
|
||||||
namespace Microsoft.CmdPal.Ext.ClipboardHistory.Helpers;
|
namespace Microsoft.CmdPal.Ext.ClipboardHistory.Helpers;
|
||||||
@@ -104,4 +105,21 @@ public static partial class NativeMethods
|
|||||||
|
|
||||||
[LibraryImport("ole32.dll")]
|
[LibraryImport("ole32.dll")]
|
||||||
internal static partial void CoUninitialize();
|
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