首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何关闭viber主窗口

如何关闭viber主窗口
EN

Stack Overflow用户
提问于 2016-12-26 13:49:04
回答 1查看 1.2K关注 0票数 1

我在开发一个小型发射器。它的主要想法是解决Viber for Windows中缺乏功能的问题。我要它使开始Viber最小化,只托盘。通常,当Viber启动时,它会出现在桌面上的Viber主窗口和系统托盘中的图标。我应该一直手动关闭这个过时的窗口。因此,我编写了几行代码,但我发现它仍然无法关闭窗口:

代码语言:javascript
运行
复制
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最小化:

代码语言:javascript
运行
复制
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上的图标(如何关闭最小化窗口)。

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-12-26 14:06:04

使用Pinvoke,如果您知道窗口标题是什么,就可以尝试获取实际窗口的句柄。

首先,导入这些功能:

代码语言:javascript
运行
复制
[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常量:

代码语言:javascript
运行
复制
const UInt32 WM_CLOSE = 0x0010;

然后是关闭窗口的代码(但要保持进程在后台运行):

代码语言:javascript
运行
复制
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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41332231

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档