1

我正在开发一个小型发射器。它的主要思想是修复 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();
    }
}

但是无论我尝试了什么(关闭,处置) - 它都不起作用。方法 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 上的图标(如何关闭最小化窗口)。

我将感谢任何帮助/想法找到解决这个问题的方法。

4

1 回答 1

0

使用 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);
于 2016-12-26T14:06:04.507 回答