Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static void SingleInstanceCallback(object sender, InstanceCallbackEventArgs args)
- {
- if (args == null || _mainFrm == null) return;
- Action<bool> d = (bool x) =>
- {
- _mainFrm.ApendArgs(args.CommandLineArgs);
- _mainFrm.Activate(x);
- };
- _mainFrm.Invoke(d, true);
- }
- private void NamedPipeManager_ReceiveString(string obj)
- {
- main.WindowState = FormWindowState.Minimized;
- main.WindowState = FormWindowState.Normal;
- main.Activate();
- }
- using System;
- using System.IO;
- using System.Linq;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Text;
- using System.Windows.Forms;
- using System.Diagnostics;
- using System.Runtime.InteropServices;
- namespace WindowsFormsTest1
- {
- public partial class Form1 : Form
- {
- [DllImport("user32.dll")]
- [return: MarshalAs(UnmanagedType.Bool)]
- static extern bool SetForegroundWindow(IntPtr hWnd);
- [DllImport("user32.dll")]
- static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
- const int ShowWindow_Restore = 9;
- [DllImport("user32.dll")]
- public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, ref COPYDATASTRUCT lParam);
- [StructLayout(LayoutKind.Sequential)]
- public struct COPYDATASTRUCT
- {
- public IntPtr dwData;
- public int cbData;
- public IntPtr lpData;
- }
- const uint WM_COPYDATA = 0x004A;
- public Form1()
- {
- InitializeComponent();
- Process this_process = Process.GetCurrentProcess();
- //найти все процессы с таким же именем
- Process[] other_processes =
- Process.GetProcessesByName(this_process.ProcessName).Where(pr => pr.Id != this_process.Id).ToArray();
- foreach (var pr in other_processes)
- {
- pr.WaitForInputIdle(1000); //на случай, если процесс еще не загрузился
- //берем первый процесс с окном
- IntPtr hWnd = pr.MainWindowHandle;
- if (hWnd == IntPtr.Zero) continue;
- //отправляем командную строку
- string command_line = "/activate";
- var cds = new COPYDATASTRUCT();
- cds.dwData = (IntPtr)1;
- cds.cbData = (command_line.Length + 1) * 2;
- cds.lpData = Marshal.StringToHGlobalUni(command_line);
- SendMessage(hWnd, WM_COPYDATA, IntPtr.Zero, ref cds);
- Marshal.FreeHGlobal(cds.lpData);
- //активируем окно и выходим
- ShowWindow(hWnd, ShowWindow_Restore);
- SetForegroundWindow(hWnd);
- Environment.Exit(0);
- }
- //если ничего не найдено, продолжаем работу
- }
- protected override void WndProc(ref Message m)
- {
- if (m.Msg == WM_COPYDATA)
- {
- COPYDATASTRUCT data = new COPYDATASTRUCT();
- data = (COPYDATASTRUCT)Marshal.PtrToStructure(m.LParam, data.GetType());
- textBox1.Text = Marshal.PtrToStringUni(data.lpData);
- }
- base.WndProc(ref m);
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- textBox1.Focus();
- textBox1.Select();
- }
- }
- }
- static Mutex Mutex;
- static NamedPipeManager NamedPipeManager;
- static Form1 main;
- Mutex = new Mutex(true, "MyApplicationName", out bool Is);
- if (!Is)
- {
- NamedPipeManager.Write("1");
- Application.Exit();
- return;
- }
- NamedPipeManager = new NamedPipeManager();
- NamedPipeManager.ReceiveString += NamedPipeManager_ReceiveString;
- NamedPipeManager.Start();
- private static void NamedPipeManager_ReceiveString(string obj)
- {
- if (main.WindowState == FormWindowState.Minimized)
- main.WindowState = FormWindowState.Normal;
- main.Activate();
- }
- [DllImport("user32.dll")]
- private static extern IntPtr GetForegroundWindow();
- [DllImport("user32.dll")]
- private static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId);
- [DllImport("user32.dll")]
- private static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach);
- public static void xActivateAndBringToFront(this Form form) {
- // activate window
- var currentForegroundWindow = GetForegroundWindow();
- var thisWindowThreadId = GetWindowThreadProcessId(form.Handle, IntPtr.Zero);
- var currentForegroundWindowThreadId = GetWindowThreadProcessId(currentForegroundWindow, IntPtr.Zero);
- AttachThreadInput(currentForegroundWindowThreadId, thisWindowThreadId, true);
- form.Activate(); // or: SetForegroundWindow(form.Handle);
- AttachThreadInput(currentForegroundWindowThreadId, thisWindowThreadId, false);
- // set window to front
- form.TopMost = true;
- form.TopMost = false;
- }
- Application.UserAppDataRegistry.SetValue(CORE_PROCESS_ID, Process.GetCurrentProcess().Id);
- int processID = (int)Application.UserAppDataRegistry.GetValue(CORE_PROCESS_ID);
- bool b = AllowSetForegroundWindow(processID);
- // .. сообщение ядру ..
- // .. выход ..
Add Comment
Please, Sign In to add comment