只运行一个窗体 并在一次点击时 显示到最前
发现用
SetForegroundWindow 并不是稳定的有效
最后使用 SetWindowPos
贴码了
- public const int HWND_TOPMOST = -1;
- public const int HWND_NOTOPMOST = -2;
- protected override void OnStartup(StartupEventArgs e)
- {
- bool isNewInstance;
- base.OnStartup(e);
- Mutex mutex = new Mutex(true, "Single", out isNewInstance);
- if (isNewInstance != true)
- {
- IntPtr intPtr = FindWindowW(null, "Single");
- if (intPtr != IntPtr.Zero)
- {
- SetWindowPos(intPtr, HWND_TOPMOST, 0, 0, 0, 0, 1 | 2);
- SetWindowPos(intPtr, HWND_NOTOPMOST, 0, 0, 0, 0, 1 | 2);
- SetForegroundWindow(intPtr);
- }
- Shutdown();
- }
- }
- [DllImport("User32", CharSet = CharSet.Unicode)]
- static extern IntPtr FindWindowW(String lpClassName, String lpWindowName);
- [DllImport("User32", CharSet = CharSet.Unicode)]
- static extern Boolean SetForegroundWindow(IntPtr hWnd);
- [DllImport("user32.dll", CharSet = CharSet.Auto)]
- private static extern int SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int y, int Width, int Height, int flags);
- [DllImport("user32.dll")]
- public static extern IntPtr SetFocus(IntPtr hWnd);
代码:
https://download.csdn.net/download/q465162770/12003540
原文地址:https://www.cnblogs.com/lonelyxmas/p/12075430.html
时间: 2024-10-01 21:16:31