简体   繁体   English

如何关闭viber主窗口

[英]How to close viber main window

I'm developing a tiny launcher. 我正在开发一个小型发射器。 Its main idea is to fix the lack of functionality in Viber for Windows. 它的主要思想是修复Viber for Windows中缺乏功能的问题。 I want it to make start Viber minimized to tray only. 我希望它能让Viber最小化到托盘。 Normally, when Viber is starting, it appears a Viber main window on desktop and an icon - in system tray. 通常,当Viber启动时,它会在桌面上显示一个Viber主窗口,并在系统托盘中显示一个图标。 All the time I should close this obsolete window manually. 我总是手动关闭这个过时的窗口。 So, I have written a few lines of code, but I found that it still couldn't close the window: 所以,我写了几行代码,但我发现它仍然无法关闭窗口:

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();
    }
}

But whatever I tried (Close, Dispose) - it does not work. 但无论我尝试什么(关闭,处置) - 它都行不通。 Method Kill does not fit, because it kills all. 方法杀死不适合,因为它会杀死所有人。 But the only thing I need is to close Viber main window and leave the process in the System Tray. 但我唯一需要的是关闭Viber主窗口并将进程留在系统托盘中。

There is also another way: to start Viber minimized at once: 还有另一种方法:立即启动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();
    }
}

In such a case, we receive a minimized window on the TaskPane and an icon in the SystemTray. 在这种情况下,我们会在TaskPane上收到一个最小化的窗口,并在SystemTray中收到一个图标。 But in this case I have absolutely no idea how to get rid of the icon (how to close minimized window) on the TaskPane. 但在这种情况下,我完全不知道如何摆脱TaskPane上的图标(如何关闭最小化窗口)。

I shall appreciate any help/ ideas in finding a solution for this problem. 我将非常感谢为此问题寻找解决方案的任何帮助/想法。

Using Pinvoke, you can try getting the handle for the actual window if you know what the window caption will be. 使用Pinvoke,如果你知道窗口标题是什么,你可以尝试获取实际窗口的句柄。

First, import these functions: 首先,导入这些功能:

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

And you may want to declare the WM_CLOSE constant: 并且您可能想要声明WM_CLOSE常量:

const UInt32 WM_CLOSE = 0x0010;

Then the code to close the window (but keep the process running the in background): 然后关闭窗口的代码(但保持进程在后台运行):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM