KeyGo/KeyGo/AppControl.cs
筱傑 4193bbe512 1. 增加 版本信息显示(在标题栏)
2. 增加 托盘右键菜单,在右键菜单中可退出应用
3. 修改 默认点击关闭按钮将缩小到托盘
2021-04-28 22:17:00 +08:00

160 lines
5.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;
namespace KeyGo
{
public static class AppControl
{
/// <summary>
/// 获取本应用程序当前正在运行的进程若不存在则返回null
/// </summary>
/// <returns>当前正在运行的进程</returns>
public static Process GetCurrentRunningInstance()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(current.ProcessName);
//遍历与当前进程名称相同的进程列表
foreach (Process process in processes)
{
//如果实例已经存在则忽略当前进程
if (process.Id != current.Id)
{
//保证要打开的进程同已经存在的进程来自同一文件路径
if (Assembly.GetExecutingAssembly().Location.Replace('/', '\\') == current.MainModule.FileName)
{
//返回已经存在的进程
return process;
}
}
}
return null;
}
/// <summary>
/// 显示指定实例窗体
/// </summary>
/// <param name="instance">The instance.</param>
public static void ShowWindow(Process instance)
{
if (instance != null && instance.MainWindowHandle != IntPtr.Zero)
{
if (IsIconic(instance.MainWindowHandle))
ShowWindowAsync(instance.MainWindowHandle, (int)CmdShow.Restore);
SetForegroundWindow(instance.MainWindowHandle);
}
}
/// <summary>
/// 隐藏指定实例窗体
/// </summary>
/// <param name="instance">The instance.</param>
public static void MinimizeWindow(Process instance)
{
if (instance != null && instance.MainWindowHandle != IntPtr.Zero)
ShowWindowAsync(instance.MainWindowHandle, (int)CmdShow.Minimize);
}
public static bool IsForegroundWindow(Process instance)
{
return GetForegroundWindow() == instance.MainWindowHandle;
}
private enum CmdShow : int
{
/// <summary>
/// Minimizes a window, even if the thread that owns the window is not responding.
/// This flag should only be used when minimizing windows from a different thread.
/// </summary>
ForceMinimize = 11,
/// <summary>
/// Hides the window and activates another window.
/// </summary>
Hide = 0,
/// <summary>
/// Maximizes the specified window.
/// </summary>
Maximize = 3,
/// <summary>
/// Minimizes the specified window and activates the next top-level window in the Z order.
/// </summary>
Minimize = 6,
/// <summary>
/// Activates and displays the window.
/// If the window is minimized or maximized,
/// the system restores it to its original size and position.
/// An application should specify this flag when restoring a minimized window.
/// </summary>
Restore = 9,
/// <summary>
/// Activates the window and displays it in its current size and position.
/// </summary>
Show = 5,
/// <summary>
/// Sets the show state based on the SW_ value specified in the STARTUPINFO
/// structure passed to the CreateProcess function by the program that started
/// the application.
/// </summary>
ShowDefault = 10,
/// <summary>
/// Activates the window and displays it as a maximized window.
/// </summary>
ShowMaximized = 3,
/// <summary>
/// Activates the window and displays it as a minimized window.
/// </summary>
ShowMinimized = 2,
/// <summary>
/// Displays the window as a minimized window.
/// This value is similar to SW_SHOWMINIMIZED,
/// except the window is not activated.
/// </summary>
ShowMinnoActive = 7,
/// <summary>
/// Displays the window in its current size and position.
/// This value is similar to SW_SHOW, except that the window is not activated.
/// </summary>
ShowNA = 8,
/// <summary>
/// Displays a window in its most recent size and position.
/// This value is similar to SW_SHOWNORMAL, except that the window is not activated.
/// </summary>
ShowNoactivate = 4,
/// <summary>
/// Activates and displays a window.
/// If the window is minimized or maximized,
/// the system restores it to its original size and position.
/// An application should specify this flag when displaying the window for the first time.
/// </summary>
ShowNormal = 1,
}
[DllImport("User32.dll")]
private static extern bool ShowWindowAsync(System.IntPtr hWnd, int cmdShow);
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(System.IntPtr hWnd);
[DllImport("User32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
private static extern bool IsZoomed(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern bool IsIconic(IntPtr hWnd);
}
}