read handle from reg and show the window

pull/2447/head
2dust 2022-07-01 20:50:47 +08:00
parent f7e3eb180b
commit 2b40e87eb9
3 changed files with 49 additions and 6 deletions

View File

@ -1138,6 +1138,12 @@ namespace v2rayN.Forms
ShowInTaskbar = false;
SetVisibleCore(false);
//write Handle to reg
if (IsHandleCreated)
{
Utils.RegWriteValue(Global.MyRegPath, Utils.WindowHwndKey, Convert.ToString((long)Handle));
}
}
#endregion

View File

@ -1,18 +1,13 @@
using System;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;
using v2rayN.Forms;
using v2rayN.Properties;
using v2rayN.Tool;
namespace v2rayN
{
static class Program
{
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool SetProcessDPIAware();
/// <summary>
/// 应用程序的主入口点。
/// </summary>
@ -21,7 +16,7 @@ namespace v2rayN
{
if (Environment.OSVersion.Version.Major >= 6)
{
SetProcessDPIAware();
Utils.SetProcessDPIAware();
}
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
@ -47,6 +42,22 @@ namespace v2rayN
}
else
{
try
{
//read handle from reg and show the window
long.TryParse(Utils.RegReadValue(Global.MyRegPath, Utils.WindowHwndKey, ""), out long llong);
if (llong > 0)
{
var hwnd = (IntPtr)llong;
if (Utils.IsWindow(hwnd))
{
Utils.ShowWindow(hwnd, 4);
Utils.SwitchToThisWindow(hwnd, true);
return;
}
}
}
catch { }
UI.ShowWarning($"v2rayN is already running(v2rayN已经运行)");
}
}

View File

@ -24,6 +24,7 @@ using System.Web;
using log4net;
using System.Linq;
using System.Security.Cryptography;
using System.Runtime.InteropServices;
namespace v2rayN
{
@ -1111,5 +1112,30 @@ namespace v2rayN
#endregion
#region Windows API
public static string WindowHwndKey
{
get
{
return $"WindowHwnd_{GetMD5(StartupPath())}";
}
}
[DllImport("user32.dll")]
public static extern bool SetProcessDPIAware();
[DllImport("user32.dll")]
public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
[DllImport("user32.dll")]
public static extern int SwitchToThisWindow(IntPtr hwnd, bool fUnknown);
[DllImport("user32.dll")]
public static extern bool IsWindow(IntPtr hwnd);
#endregion
}
}