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 に答える