windowns窗体应用项目:

    1. using System.Threading;
    2. using System.Diagnostics;
    3. using System.Runtime.InteropServices;
    4. //以下所有代码放在窗体的Program.cs里面
    5. [DllImport("user32.dll")]
    6. private static extern bool SetForegroundWindow(IntPtr hWnd);
    7. [DllImport("user32.dll")]
    8. private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
    9. [DllImport("user32.dll")]
    10. private static extern bool IsIconic(IntPtr hWnd);
    11. private const int SW_RESTORE = 9;
    12. /// <summary>
    13. /// c# 进程窗口显示并前置
    14. /// </summary>
    15. public static void RaiseOtherProcess()
    16. {
    17. Process proc = Process.GetCurrentProcess();
    18. Process.GetProcesses();
    19. foreach (Process otherProc in Process.GetProcessesByName(proc.ProcessName))
    20. {
    21. //ignore "this" process
    22. if (proc.Id != otherProc.Id)
    23. {
    24. // Found a "same named process".
    25. // Assume it is the one we want brought to the foreground.
    26. // Use the Win32 API to bring it to the foreground.
    27. IntPtr hWnd = otherProc.MainWindowHandle;
    28. if (IsIconic(hWnd))
    29. {
    30. ShowWindowAsync(hWnd, 9);
    31. }
    32. SetForegroundWindow(hWnd);
    33. break;
    34. }
    35. }
    36. }
    37. static void Main()
    38. {
    39. Application.EnableVisualStyles();
    40. Application.SetCompatibleTextRenderingDefault(false);
    41. bool createdNew;
    42. Mutex instance = new Mutex(true, "Regfrm", out createdNew);
    43. if (createdNew)
    44. {
    45. Application.Run(new Regfrm());
    46. instance.ReleaseMutex();
    47. //如果需要窗体重启,并出现问题,将这句改为
    48. //instance.Dispose();
    49. }
    50. else
    51. {
    52. RaiseOtherProcess();
    53. Application.Exit();
    54. }
    55. }

    =====================================================
    windowns窗体应用插件项目:

    1. public MainFrm Frm;//这一句放在class里面
    2. private void Button1_Click(object sender, RibbonControlEventArgs e)
    3. {
    4. //以下代码放在插件控件的事件里面
    5. if (Frm == null || Frm.IsDisposed)
    6. {
    7. Frm = new MainFrm();
    8. //ZwSoft.ZwCAD.ApplicationServices.Application.ShowModelessDialog(Frm);
    9. Frm.Show();
    10. }
    11. else
    12. {
    13. if (Frm.WindowState == FormWindowState.Minimized)
    14. {
    15. Frm.WindowState = FormWindowState.Normal;
    16. }
    17. Frm.Activate();
    18. }
    19. //以上代码放在插件控件的事件里面
    20. }