Guest User

Untitled

a guest
May 23rd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.39 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. private void NamedPipeManager_ReceiveString(string obj)
  13. {
  14. main.WindowState = FormWindowState.Minimized;
  15. main.WindowState = FormWindowState.Normal;
  16. main.Activate();
  17. }
  18.  
  19. using System;
  20. using System.IO;
  21. using System.Linq;
  22. using System.Collections.Generic;
  23. using System.ComponentModel;
  24. using System.Text;
  25. using System.Windows.Forms;
  26. using System.Diagnostics;
  27. using System.Runtime.InteropServices;
  28.  
  29. namespace WindowsFormsTest1
  30. {
  31. public partial class Form1 : Form
  32. {
  33. [DllImport("user32.dll")]
  34. [return: MarshalAs(UnmanagedType.Bool)]
  35. static extern bool SetForegroundWindow(IntPtr hWnd);
  36.  
  37. [DllImport("user32.dll")]
  38. static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
  39.  
  40. const int ShowWindow_Restore = 9;
  41.  
  42. [DllImport("user32.dll")]
  43. public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, ref COPYDATASTRUCT lParam);
  44.  
  45. [StructLayout(LayoutKind.Sequential)]
  46. public struct COPYDATASTRUCT
  47. {
  48. public IntPtr dwData;
  49. public int cbData;
  50. public IntPtr lpData;
  51. }
  52.  
  53. const uint WM_COPYDATA = 0x004A;
  54.  
  55. public Form1()
  56. {
  57. InitializeComponent();
  58.  
  59. Process this_process = Process.GetCurrentProcess();
  60.  
  61. //найти все процессы с таким же именем
  62. Process[] other_processes =
  63. Process.GetProcessesByName(this_process.ProcessName).Where(pr => pr.Id != this_process.Id).ToArray();
  64.  
  65. foreach (var pr in other_processes)
  66. {
  67. pr.WaitForInputIdle(1000); //на случай, если процесс еще не загрузился
  68.  
  69. //берем первый процесс с окном
  70. IntPtr hWnd = pr.MainWindowHandle;
  71. if (hWnd == IntPtr.Zero) continue;
  72.  
  73. //отправляем командную строку
  74. string command_line = "/activate";
  75. var cds = new COPYDATASTRUCT();
  76. cds.dwData = (IntPtr)1;
  77. cds.cbData = (command_line.Length + 1) * 2;
  78. cds.lpData = Marshal.StringToHGlobalUni(command_line);
  79. SendMessage(hWnd, WM_COPYDATA, IntPtr.Zero, ref cds);
  80. Marshal.FreeHGlobal(cds.lpData);
  81.  
  82. //активируем окно и выходим
  83. ShowWindow(hWnd, ShowWindow_Restore);
  84. SetForegroundWindow(hWnd);
  85. Environment.Exit(0);
  86. }
  87. //если ничего не найдено, продолжаем работу
  88. }
  89.  
  90. protected override void WndProc(ref Message m)
  91. {
  92. if (m.Msg == WM_COPYDATA)
  93. {
  94. COPYDATASTRUCT data = new COPYDATASTRUCT();
  95. data = (COPYDATASTRUCT)Marshal.PtrToStructure(m.LParam, data.GetType());
  96. textBox1.Text = Marshal.PtrToStringUni(data.lpData);
  97. }
  98. base.WndProc(ref m);
  99. }
  100.  
  101. private void Form1_Load(object sender, EventArgs e)
  102. {
  103. textBox1.Focus();
  104. textBox1.Select();
  105. }
  106.  
  107. }
  108.  
  109. }
  110.  
  111. static Mutex Mutex;
  112. static NamedPipeManager NamedPipeManager;
  113. static Form1 main;
  114.  
  115. Mutex = new Mutex(true, "MyApplicationName", out bool Is);
  116. if (!Is)
  117. {
  118. NamedPipeManager.Write("1");
  119. Application.Exit();
  120. return;
  121. }
  122. NamedPipeManager = new NamedPipeManager();
  123. NamedPipeManager.ReceiveString += NamedPipeManager_ReceiveString;
  124. NamedPipeManager.Start();
  125.  
  126. private static void NamedPipeManager_ReceiveString(string obj)
  127. {
  128. if (main.WindowState == FormWindowState.Minimized)
  129. main.WindowState = FormWindowState.Normal;
  130. main.Activate();
  131. }
  132.  
  133. [DllImport("user32.dll")]
  134. private static extern IntPtr GetForegroundWindow();
  135.  
  136. [DllImport("user32.dll")]
  137. private static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId);
  138.  
  139. [DllImport("user32.dll")]
  140. private static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach);
  141.  
  142. public static void xActivateAndBringToFront(this Form form) {
  143.  
  144. // activate window
  145. var currentForegroundWindow = GetForegroundWindow();
  146. var thisWindowThreadId = GetWindowThreadProcessId(form.Handle, IntPtr.Zero);
  147. var currentForegroundWindowThreadId = GetWindowThreadProcessId(currentForegroundWindow, IntPtr.Zero);
  148. AttachThreadInput(currentForegroundWindowThreadId, thisWindowThreadId, true);
  149. form.Activate(); // or: SetForegroundWindow(form.Handle);
  150. AttachThreadInput(currentForegroundWindowThreadId, thisWindowThreadId, false);
  151.  
  152. // set window to front
  153. form.TopMost = true;
  154. form.TopMost = false;
  155. }
  156.  
  157. Application.UserAppDataRegistry.SetValue(CORE_PROCESS_ID, Process.GetCurrentProcess().Id);
  158.  
  159. int processID = (int)Application.UserAppDataRegistry.GetValue(CORE_PROCESS_ID);
  160. bool b = AllowSetForegroundWindow(processID);
  161. // .. сообщение ядру ..
  162. // .. выход ..
Add Comment
Please, Sign In to add comment