Javascript 在NW.JS中从程序切换器隐藏窗口
我正在用NW.JS(node webkit)编写桌面应用程序。在我的应用程序中,用户可能会打开许多窗口,我想从程序切换器(alt+tab)和任务栏中隐藏它们。我已经找到了对taksbar隐藏窗口的选项,但找不到任何方法对程序切换器隐藏窗口。有可能吗?或者至少可以将所有窗口分组为一个窗口(就像windows上的便笺一样)?我从来没有使用过NW.JS,所以这可能是完全错误的,但看起来有一个NM.JS模块。我可以想象,使用这个模块,您应该能够创建本机窗口,并为它们提供WS_EX_TOOLWINDOW样式。请参阅以下答案:。如果只针对Windows,这似乎是通知的最佳路径Javascript 在NW.JS中从程序切换器隐藏窗口,javascript,node.js,node-webkit,nw.js,Javascript,Node.js,Node Webkit,Nw.js,我正在用NW.JS(node webkit)编写桌面应用程序。在我的应用程序中,用户可能会打开许多窗口,我想从程序切换器(alt+tab)和任务栏中隐藏它们。我已经找到了对taksbar隐藏窗口的选项,但找不到任何方法对程序切换器隐藏窗口。有可能吗?或者至少可以将所有窗口分组为一个窗口(就像windows上的便笺一样)?我从来没有使用过NW.JS,所以这可能是完全错误的,但看起来有一个NM.JS模块。我可以想象,使用这个模块,您应该能够创建本机窗口,并为它们提供WS_EX_TOOLWINDOW样
另一个需要考虑的问题是使用iframe构建应用程序,这样只有一个本机窗口。然后,您可以使用javascript和您自己的界面来决定显示哪些iFrame。此功能可能会在某个时候出现,但从版本0.12.3开始,node webkit没有提供任何用于实现工具类型窗口的界面,因此无法使用javascript或使用项目的package.json文件来实现这一点。我可以想到两个选择 1.自己构建节点webkit。是非常直接和全面的。您需要在中修改
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
致:
请注意,这是一个简单的解决方案,它将使所有新窗口默认为工具样式,并且在切换窗口时不可见。要使此功能可用于清单文件或,则需要对多个文件进行更改
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);
});
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Runtime.InteropServices;
使用系统计时器;
使用系统诊断;
命名空间nw
{
班级计划
{
const int GWL_EXSTYLE=-0x14;
常量int WS_EX_APPWINDOW=0x40000;
常量int WS_EX_TOOLWINDOW=0x80;
const int WS_EX_COMPOSITED=0x02000000;
[DllImport(“user32”,CharSet=CharSet.Auto)]
静态外部int GetWindowLong(IntPtr hwnd,int索引);
[DllImport(“User32.Dll”)]
静态外部int SetWindowLong(IntPtr hwnd、int index、int newLong);
[DllImport(“user32.dll”,CharSet=CharSet.Unicode)]
静态外部int GetWindowText(IntPtr hWnd、StringBuilder strText、int maxCount);
[DllImport(“user32.dll”,CharSet=CharSet.Unicode)]
静态外部int GetWindowTextLength(IntPtr hWnd);
[DllImport(“user32.dll”)]
静态外部bool EnumWindows(EnumWindowsProc enumProc、IntPtr lParam);
[DllImport(“user32.dll”)]
静态外部IntPtr SetWindowText(IntPtr HWND,字符串文本);
委托bool EnumWindowsProc(IntPtr hWnd、IntPtr lParam);
//------------------------------------------------------------------
静态void Main(字符串[]参数)
{
//测试未打包的应用程序,如:
//进程启动(,);
Process.Start(“打包的nw app.js”);
Timer Timer=new Timer(){Interval=100,Enabled=true};
timer.appeased+=新的ElapsedEventHandler(OnTimedEvent);
Console.ReadLine();
}
//------------------------------------------------------------------
静态void OnTimedEvent(对象源,ElapsedEventArgs e)
{
//找到我们的目标窗口(将“nw工具窗口”更改为您的窗口标题)
IEnumerable windows=FindWindowsWithText(“nw工具窗口”);
foreach(windows中的var窗口)
{
//将WS_EX_TOOLWINDOW应用于当前样式
SetWindowLong(窗口,GWL_EXSTYLE,(GetWindowLong(窗口,GWL_EXSTYLE)| WS_EX_工具窗口)和~WS_EX_APPWINDOW);
//将标题更改为更改后的标志
SetWindowText(窗口,“nw工具窗口隐藏”);
}
}
//------------------------------------------------------------------
静态字符串GetWindowText(IntPtr hwnd)
{
int size=GetWindowTextLength(hwnd);
如果(大小>0)
{
var builder=新的StringBuilder(大小+1);
GetWindowText(hwnd、builder、builder.Capacity);
返回builder.ToString();
}
返回字符串。空;
}
//------------------------------------------------------------------
静态IEnumerable FindWindows(EnumWindowsProc筛选器)
{
IntPtr found=IntPtr.Zero;
列表窗口=新列表();
枚举窗口(委托(IntPtr wnd、IntPtr param)
{
if(过滤器(wnd,参数))
{
windows.Add(wnd);
}
返回true;
},IntPtr.Zero);
返回窗口;
}
//------------------------------------------------------------
静态IEnumerable FindWithText(字符串titleText)
{
返回FindWindows(委托(IntPtr wnd,IntPtr param)
{
返回GetWindowText(wnd).Contains(titleText);
});
}
}
}
查找标题的代码取自。如果您只对主窗口感兴趣,则可以去掉上面的大部分代码,只需使用FindWindow
和SetWindowLong
。您可能还应该隐藏控制台或窗口(如果您使用表单或WPF进行此操作),因此有足够的信息说明如何实现这一点
第二种方法有点老套,不过,我宁愿选择
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);
});
}
}
}