WPF Application single instance
for WPF if you want to have a single instance (for one session) you can use this code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
public class ApplicationSingleInstance : Application { protected override void OnStartup(StartupEventArgs e) { var currentSessionID = Process.GetCurrentProcess().SessionId; var currentProcessId = Process.GetCurrentProcess().Id; var exists = System.Diagnostics.Process.GetProcessesByName(System.IO.Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetEntryAssembly().Location)); var result = exists.Where(p => p.SessionId == currentSessionID && p.Id != currentProcessId) .OrderBy(p => p.Id) .ToList(); if (result.Count > 0) { #if MESSAGE string s = ""; int i = 0; foreach (var item in result) { s += "[" + i + "] handle window = " + result[i].MainWindowHandle + " Process ID = " + result[i].Id + "\n"; i++; } MessageBox.Show(s); #endif UnMinimizeClass.Reactivate(result.First()); Environment.Exit(0); return; } base.OnStartup(e); } } public static class UnMinimizeClass { #region Imports [DllImport("user32.dll")] static extern bool AllowSetForegroundWindow(int dwProcessId); [DllImport("user32.dll")] private static extern bool SetForegroundWindow(IntPtr hWnd); [DllImport("user32.dll")] public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); [DllImport("user32.dll", SetLastError = true)] static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, long uFlags); [DllImport("user32.dll", SetLastError = true)] static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll", SetLastError = true)] static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow); [DllImport("user32.dll", SetLastError = true)] static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount); [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] static extern int GetWindowTextLength(IntPtr hWnd); [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount); [DllImport("user32.dll")] public static extern int EnumWindows(CallBackPtr callPtr, int lPar); [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] static extern bool IsWindowVisible(IntPtr hWnd); [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] static extern bool IsIconic(IntPtr hWnd); [DllImport("user32.dll")] static extern bool IsZoomed(IntPtr hWnd); public delegate bool CallBackPtr(IntPtr hwnd, int lParam); #endregion #region const private const int SW_RESTORE = 9; private const int SW_HIDE = 0; private const int SW_MINIMIZE = 6; private const int SW_MAXIMIZE = 3; private const int SW_SHOW = 5; static readonly IntPtr HWND_TOPMOST = new IntPtr(-1); static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2); static readonly IntPtr HWND_TOP = new IntPtr(0); static readonly IntPtr HWND_BOTTOM = new IntPtr(1); private static class SWP { public static readonly int NOSIZE = 0x0001, NOMOVE = 0x0002, NOZORDER = 0x0004, NOREDRAW = 0x0008, NOACTIVATE = 0x0010, DRAWFRAME = 0x0020, FRAMECHANGED = 0x0020, SHOWWINDOW = 0x0040, HIDEWINDOW = 0x0080, NOCOPYBITS = 0x0100, NOOWNERZORDER = 0x0200, NOREPOSITION = 0x0200, NOSENDCHANGING = 0x0400, DEFERERASE = 0x2000, ASYNCWINDOWPOS = 0x4000; } #endregion #region private functions private static IntPtr GetWindowHandle(uint processId) { IntPtr result = IntPtr.Zero; UnMinimizeClass.EnumWindows((hCurWnd, param) => { uint dwProcessID = 0; GetWindowThreadProcessId(hCurWnd, out dwProcessID); if (dwProcessID == processId && IsWindowVisible(hCurWnd)) { result = hCurWnd; return false; } return true; }, 0); return result; } private static string GetWindowClassName(IntPtr hWnd) { StringBuilder ClassName = new StringBuilder(256); int nRet = GetClassName(hWnd, ClassName, ClassName.Capacity); return nRet != 0 ? ClassName.ToString() : null; } private static string GetWindowTitle(IntPtr hWnd) { // Allocate correct string length first int length = GetWindowTextLength(hWnd); StringBuilder sb = new StringBuilder(length + 1); GetWindowText(hWnd, sb, sb.Capacity); return sb.ToString(); } private static int GetWindowStat(IntPtr hWnd) { if (!IsWindowVisible(hWnd)) return SW_HIDE; else if (IsIconic(hWnd)) return SW_MINIMIZE; else if (IsZoomed(hWnd)) return SW_MAXIMIZE; else return SW_SHOW; } #endregion #region public Reactivate public static void Reactivate(Process process) { IntPtr handle = process.MainWindowHandle; if (process.MainWindowHandle.ToInt32() == 0) { handle = GetWindowHandle((uint)process.Id); #if MESSAGE MessageBox.Show("pointeur a zero = " + handle + " window handle = " + FindWindow(null, "XXX")); #endif } Reactivate(handle, process.Id); } public static void Reactivate(IntPtr handle, int processId) { int stat = GetWindowStat(handle); if (stat == SW_MAXIMIZE) { SetForegroundWindow(handle); SetWindowPos(handle, (IntPtr)HWND_TOP, 0, 0, 0, 0, SWP.NOMOVE | SWP.NOSIZE | SWP.SHOWWINDOW); } else if (stat == SW_HIDE) { ShowWindow(handle, SW_SHOW); SetForegroundWindow(handle); SetWindowPos(handle, (IntPtr)HWND_TOP, 0, 0, 0, 0, SWP.NOMOVE | SWP.NOSIZE | SWP.SHOWWINDOW); } else { ShowWindow(handle, (int)SW_RESTORE); SetForegroundWindow(handle); SetWindowPos(handle, (IntPtr)HWND_TOP, 0, 0, 0, 0, SWP.NOMOVE | SWP.NOSIZE | SWP.SHOWWINDOW); } } #endregion } |
and hinerite your App class from ApplicationSingleInstance
don’t forget to change in App Xaml like this
1 2 3 4 5 |
<local:ApplicationSingleInstance x:Class="XX.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:XX" ... |
if your namespace is XX
Categories: Non classé
Commentaires récents