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

最佳答案

此功能可能会在某个时候出现,但从0.12.3版开始,node-webkit尚不提供用于实现工具类型窗口的任何接口(interface),因此无法使用javascript或使用项目的package.json文件来完成此功能。我在这里可以想到两个选择。

1. 自己构建node-webkit。 The guide非常简单和全面。您需要在native_aurora_aura.cc中修改NativeWindowAura的构造函数,特别是这一点:

#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);

请注意,这是一个简单的解决方案,它将导致所有新窗口默认为工具样式,并且在切换窗口时不可见。要使此功能可用于 list 文件或window 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);
         });
      }
   }
}

查找标题的代码来自here。如果您只对主窗口感兴趣,则可以摆脱上面的大多数代码,而只需要使用FindWindowSetWindowLong即可。您可能还应该隐藏控制台或窗口(如果使用Forms或WPF执行此操作),则有足够的信息说明如何在SO上完成此操作。

第二种方法有些棘手,但是,我宁愿选择第一种。也许可以正确地实现它,而不是硬编码并打开拉取请求:)

关于javascript - 在NW.JS中从程序切换器隐藏窗口,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32779344/

10-10 23:12
查看更多