我在开发一个小型发射器。它的主要想法是解决Viber for Windows中缺乏功能的问题。我要它使开始Viber最小化,只托盘。通常,当Viber启动时,它会出现在桌面上的Viber主窗口和系统托盘中的图标。我应该一直手动关闭这个过时的窗口。因此,我编写了几行代码,但我发现它仍然无法关闭窗口:
using System;
using System.Diagnostics;
class ViberStrt {
static void Main() {
Process newProc = Process.Start("c:\\Users\\Dmytro\\AppData\\Local\\Viber\\Viber.exe");
Console.WriteLine("New process has started");
//newProc.CloseMainWindow();
newProc.WaitForExit();
newProc.Close();
newProc.Dispose();
Console.WriteLine("Process has finished");
//newProc.Kill();
}
}
但是不管我做了什么(接近,处理),它都不起作用。方法杀人不合适,因为它杀死了所有的人。但我唯一需要的是关闭Viber主窗口,并将进程留在系统托盘中。
还有另一种方法:立即将Viber最小化:
using System;
using System.Diagnostics;
class LaunchViber
{
void OpenWithStartInfo()
{
ProcessStartInfo startInfo = new ProcessStartInfo("c:\\Users\\Dmytro\\AppData\\Local\\Viber\\Viber.exe");
startInfo.WindowStyle = ProcessWindowStyle.Minimized;
Process.Start(startInfo);
}
static void Main()
{
//Process newProc = Process.Start("c:\\Users\\Dmytro\\AppData\\Local\\Viber\\Viber.exe");
LaunchViber newProc = new LaunchViber();
newProc.OpenWithStartInfo();
}
}
在这种情况下,我们在TaskPane上接收一个最小化的窗口,在SystemTray中接收一个图标。但是在这种情况下,我完全不知道如何去掉TaskPane上的图标(如何关闭最小化窗口)。
我将感谢为这个问题找到解决办法的任何帮助/想法。
发布于 2016-12-26 14:06:04
使用Pinvoke,如果您知道窗口标题是什么,就可以尝试获取实际窗口的句柄。
首先,导入这些功能:
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
您可能需要声明WM_CLOSE
常量:
const UInt32 WM_CLOSE = 0x0010;
然后是关闭窗口的代码(但要保持进程在后台运行):
var startInfo = new ProcessStartInfo(@"c:\Users\Dmytro\AppData\Local\Viber\Viber.exe");
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
var newProc = Process.Start(startInfo);
var name = "Viber +381112223344";
var windowPtr = FindWindowByCaption(IntPtr.Zero, name);
while (windowPtr == IntPtr.Zero)
{
windowPtr = FindWindowByCaption(IntPtr.Zero, name);
}
System.Threading.Thread.Sleep(100);
SendMessage(windowPtr, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
https://stackoverflow.com/questions/41332231
复制相似问题