在NW.JS中隐藏程序切换窗口

Hide window from program switcher in NW.JS

本文关键字:窗口 程序 隐藏 NW JS      更新时间:2023-09-26

我正在用NW.JS (node-webkit)编写桌面应用程序。在我的应用程序中,用户可能会打开许多窗口,我想从程序切换器(alt+tab)和任务栏隐藏它们。我已经找到了从taksbar隐藏窗口的选项,但无法找到从程序切换器隐藏窗口的任何方法。这可能吗?或者至少有可能将所有窗口组合为一个(就像在windows上的便利贴)?

这个功能可能会在某个时候出现,但是在0.12.3版本node-webkit没有提供任何实现工具类型窗口的接口,所以没有办法用javascript或使用项目包来完成这个功能。json文件。我有两个选择

1。自己构建node-webkit。这个指南非常直接和全面。需要修改native_aurora_aura中NativeWindowAura的构造函数。Cc,特别是这一点:

#if defined(OS_WIN)
  HWND hwnd = views::HWNDForWidget(window_->GetTopLevelWidget());
  int current_style = ::GetWindowLong(hwnd, GWL_STYLE);
  ::SetWindowLong(hwnd, GWL_STYLE, current_style | WS_CAPTION); //This is the importante line
#endif

:

  ::SetWindowLong(hwnd, GWL_STYLE, current_style | WS_CAPTION | WS_EX_TOOLWINDOW);

注意,这是一个简单的解决方案,它将导致所有新窗口默认为工具样式,并且在切换窗口时不可见。要使此功能对manifest文件或窗口API可用,需要对更多的文件进行更改。

2。编写并部署一个启动应用程序,该应用程序加载打包的项目(或项目文件夹),不断搜索具有特定标题的窗口并设置窗口样式。下面是一个使用c#控制台应用程序的示例,但您也可以使用c++或任何。net语言来实现:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Timers;
using System.Diagnostics;
namespace nw
{
   class Program
   {
      const int GWL_EXSTYLE      = -0x14;
      const int WS_EX_APPWINDOW  = 0x40000;
      const int WS_EX_TOOLWINDOW = 0x80;
      const int WS_EX_COMPOSITED = 0x02000000;
      [DllImport("user32", CharSet = CharSet.Auto)]
      static extern int GetWindowLong(IntPtr hwnd, int index);
      [DllImport("User32.Dll")]
      static extern int SetWindowLong(IntPtr hwnd, int index, int newLong);
      [DllImport("user32.dll", CharSet = CharSet.Unicode)]
      static extern int GetWindowText(IntPtr hWnd, StringBuilder strText, int maxCount);
      [DllImport("user32.dll", CharSet = CharSet.Unicode)]
      static extern int GetWindowTextLength(IntPtr hWnd);
      [DllImport("user32.dll")]
      static extern bool EnumWindows(EnumWindowsProc enumProc, IntPtr lParam);
      [DllImport("user32.dll")]
      static extern IntPtr SetWindowText(IntPtr HWND, string Text);
      delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);

      //------------------------------------------------------------------
      static void Main(string[] args)
      {
         // Test an unpackaged app like:
         // Process.Start(<path'to'nw.exe>, <path'to'project-folder>);
         Process.Start("packaged-nw-app.js");
         Timer timer = new Timer() { Interval = 100, Enabled = true };
         timer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
         Console.ReadLine();
      }

      //------------------------------------------------------------------
      static void OnTimedEvent(object source, ElapsedEventArgs e)
      {
         // Find our target windows (change "nw-tool-window" to your window title)
         IEnumerable<IntPtr> windows = FindWindowsWithText("nw-tool-window");
         foreach (var window in windows)
         {
            // Apply WS_EX_TOOLWINDOW to the current style
            SetWindowLong(window, GWL_EXSTYLE, (GetWindowLong(window, GWL_EXSTYLE) | WS_EX_TOOLWINDOW) & ~WS_EX_APPWINDOW);
            // Change title to flag as changed
            SetWindowText(window, "nw-tool-window-hidden");
         }
      }

      //------------------------------------------------------------------
      static string GetWindowText(IntPtr hwnd)
      {
         int size = GetWindowTextLength(hwnd);
         if (size > 0)
         {
            var builder = new StringBuilder(size + 1);
            GetWindowText(hwnd, builder, builder.Capacity);
            return builder.ToString();
         }
         return String.Empty;
      }

      //------------------------------------------------------------------
      static IEnumerable<IntPtr> FindWindows(EnumWindowsProc filter)
      {
         IntPtr found = IntPtr.Zero;
         List<IntPtr> windows = new List<IntPtr>();
         EnumWindows(delegate(IntPtr wnd, IntPtr param)
         {
            if (filter(wnd, param))
            {
               windows.Add(wnd);
            }
            return true;
         }, IntPtr.Zero);
         return windows;
      }

      //------------------------------------------------------------
      static IEnumerable<IntPtr> FindWindowsWithText(string titleText)
      {
         return FindWindows(delegate(IntPtr wnd, IntPtr param)
         {
            return GetWindowText(wnd).Contains(titleText);
         });
      } 
   }
}

查找标题的代码是从这里取的。如果你只对你的主窗口感兴趣,你可以去掉上面的大部分代码,只使用FindWindowSetWindowLong。您可能还应该隐藏控制台或窗口(如果您使用Forms或WPF这样做),这里有足够的信息说明如何在SO上实现这一点。

第二种方法有点粗糙,不过,我宁愿使用第一种方法。也许实现它正确,而不是硬编码和打开拉请求:)

我从来没有使用过NW.JS,所以这可能是完全错误的,但它看起来像有一个NW.JS的原生UI模块。我想使用这个模块,您应该能够创建本机窗口,并给它们一个WS_EX_TOOLWINDOW样式。看看这个答案:从Alt-Tab程序切换器中隐藏窗口的最佳方法。如果你只针对Windows,这似乎是通知的最佳路径。

另一个要考虑的是使用IFRAMES构建应用程序,这样只有一个本机窗口。然后你可以使用javascript和你自己的接口来决定显示哪些iframe