Guest User

Untitled

a guest
May 23rd, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. private static void SingleInstanceCallback(object sender, InstanceCallbackEventArgs args)
  2. {
  3. if (args == null || _mainFrm == null) return;
  4. Action<bool> d = (bool x) =>
  5. {
  6. _mainFrm.ApendArgs(args.CommandLineArgs);
  7. _mainFrm.Activate(x);
  8. };
  9. _mainFrm.Invoke(d, true);
  10. }
  11.  
  12. static Mutex Mutex;
  13. static NamedPipeManager NamedPipeManager;
  14. static Form1 main;
  15.  
  16. Mutex = new Mutex(true, "MyApplicationName", out bool Is);
  17. if (!Is)
  18. {
  19. NamedPipeManager.Write("1");
  20. Application.Exit();
  21. return;
  22. }
  23. NamedPipeManager = new NamedPipeManager();
  24. NamedPipeManager.ReceiveString += NamedPipeManager_ReceiveString;
  25. NamedPipeManager.Start();
  26.  
  27. private static void NamedPipeManager_ReceiveString(string obj)
  28. {
  29. if (main.WindowState == FormWindowState.Minimized)
  30. main.WindowState = FormWindowState.Normal;
  31. main.Activate();
  32. }
  33.  
  34. private void NamedPipeManager_ReceiveString(string obj)
  35. {
  36. main.WindowState = FormWindowState.Minimized;
  37. main.WindowState = FormWindowState.Normal;
  38. main.Activate();
  39. }
  40.  
  41. [DllImport("user32.dll")]
  42. [return: MarshalAs(UnmanagedType.Bool)]
  43. static extern bool SetForegroundWindow(IntPtr hWnd);
  44.  
  45. [DllImport("user32.dll")]
  46. static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
  47.  
  48. const int ShowWindow_Restore = 9;
  49.  
  50. static void Main()
  51. {
  52. Process this_process = Process.GetCurrentProcess();
  53.  
  54. //найти все процессы с таким же именем
  55. Process[] other_processes =
  56. Process.GetProcessesByName(this_process.ProcessName).Where(pr => pr.Id != this_process.Id).ToArray();
  57.  
  58. foreach (var pr in other_processes)
  59. {
  60. pr.WaitForInputIdle(1000); //на случай, если процесс еще не загрузился
  61.  
  62. //берем первый процесс с окном, активируем его и выходим
  63. IntPtr hWnd = pr.MainWindowHandle;
  64. if (hWnd == IntPtr.Zero) continue;
  65. ShowWindow(hWnd, ShowWindow_Restore);
  66. SetForegroundWindow(hWnd);
  67. Environment.Exit(0);
  68. }
  69. //если ничего не найдено, продолжаем работу...
  70. }
  71.  
  72. [DllImport("user32.dll")]
  73. private static extern IntPtr GetForegroundWindow();
  74.  
  75. //[DllImport("user32.dll")]
  76. //public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
  77.  
  78. [DllImport("User32")]
  79. private static extern int SetForegroundWindow(IntPtr hwnd);
  80.  
  81. [DllImport("user32.dll")]
  82. private static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId);
  83.  
  84. [DllImport("user32.dll")]
  85. private static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach);
  86.  
  87. public static void xBringToFront2(this Form form) {
  88.  
  89. var currentForegroundWindow = GetForegroundWindow();
  90. var thisWindowThreadId = GetWindowThreadProcessId(form.Handle, IntPtr.Zero);
  91. var currentForegroundWindowThreadId = GetWindowThreadProcessId(currentForegroundWindow, IntPtr.Zero);
  92. AttachThreadInput(currentForegroundWindowThreadId, thisWindowThreadId, true);
  93. //SetWindowPos(form.Handle, new IntPtr(0), 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_SHOWWINDOW);
  94.  
  95. // !!! активирует окно и устанавливает фокус ввода
  96. form.Activate(); // или (нет разницы): SetForegroundWindow(form.Handle);
  97. AttachThreadInput(currentForegroundWindowThreadId, thisWindowThreadId, false);
  98. //form.Show();
  99. //form.Activate();
  100.  
  101. // !!! выносит окно на передний план
  102. form.TopMost = true;
  103. form.TopMost = false;
  104. }
Add Comment
Please, Sign In to add comment