ApplicationAPI.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. //************************************************************************
  2. // author: yuzhengyang
  3. // date: 2018.3.27 - 2018.3.27
  4. // desc: 工具描述
  5. // Copyright (c) yuzhengyang. All rights reserved.
  6. //************************************************************************
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Diagnostics;
  10. using System.Linq;
  11. using System.Runtime.InteropServices;
  12. using System.Text;
  13. namespace Azylee.Core.WindowsUtils.APIUtils
  14. {
  15. /// <summary>
  16. /// 应用程序API
  17. /// </summary>
  18. public static class ApplicationAPI
  19. {
  20. #region 常量
  21. private const int SW_RESTORE = 9;
  22. #endregion
  23. #region dll方法声明
  24. [DllImport("user32.dll")]
  25. private static extern bool SetForegroundWindow(IntPtr hWnd);
  26. [DllImport("user32.dll")]
  27. private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
  28. [DllImport("user32.dll")]
  29. private static extern bool IsIconic(IntPtr hWnd);
  30. #endregion
  31. /// <summary>
  32. /// 唤起进程窗口(搭配 AppUnique.IsUnique() 食用更佳)
  33. /// -测试无法唤起隐藏窗口,仅能唤起常规窗口
  34. /// </summary>
  35. public static void Raise(Process process, bool all = false)
  36. {
  37. Process.GetProcesses();
  38. foreach (Process otherProc in Process.GetProcessesByName(process.ProcessName))
  39. {
  40. //ignore "this" process
  41. if (process.Id != otherProc.Id)
  42. {
  43. // Found a "same named process".
  44. // Assume it is the one we want brought to the foreground.
  45. // Use the Win32 API to bring it to the foreground.
  46. IntPtr hWnd = otherProc.MainWindowHandle;
  47. if (IsIconic(hWnd))
  48. {
  49. ShowWindowAsync(hWnd, 9);
  50. }
  51. SetForegroundWindow(hWnd);
  52. if (!all) break;//搜索并唤起一个程序则终止
  53. }
  54. }
  55. }
  56. }
  57. }