9L%D)JT7@C(WMNQG`{X1IZA.png
    Program:

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Diagnostics;
    4. using System.Linq;
    5. using System.Runtime.InteropServices;
    6. using System.Threading;
    7. using System.Windows.Forms;
    8. namespace SASPGCalcBookTool
    9. {
    10. public static class Program
    11. {
    12. public static int Msp { get; set; }
    13. [DllImport("user32.dll")]
    14. private static extern bool SetForegroundWindow(IntPtr hWnd);
    15. [DllImport("user32.dll")]
    16. private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
    17. [DllImport("user32.dll")]
    18. private static extern bool IsIconic(IntPtr hWnd);
    19. private const int SW_RESTORE = 9;
    20. /// <summary>
    21. /// c# 进程窗口显示并前置
    22. /// </summary>
    23. public static void RaiseOtherProcess()
    24. {
    25. Process proc = Process.GetCurrentProcess();
    26. Process.GetProcesses();
    27. foreach (Process otherProc in Process.GetProcessesByName(proc.ProcessName))
    28. {
    29. //ignore "this" process
    30. if (proc.Id != otherProc.Id)
    31. {
    32. // Found a "same named process".
    33. // Assume it is the one we want brought to the foreground.
    34. // Use the Win32 API to bring it to the foreground.
    35. IntPtr hWnd = otherProc.MainWindowHandle;
    36. if (IsIconic(hWnd))
    37. {
    38. ShowWindowAsync(hWnd, 9);
    39. }
    40. SetForegroundWindow(hWnd);
    41. break;
    42. }
    43. }
    44. }
    45. /// <summary>
    46. /// 应用程序的主入口点。
    47. /// </summary>
    48. [STAThread]
    49. static void Main()
    50. {
    51. Application.EnableVisualStyles();
    52. Application.SetCompatibleTextRenderingDefault(false);
    53. bool createdNew;
    54. Mutex instance = new Mutex(true, "MainFrm", out createdNew);
    55. if (createdNew)
    56. {
    57. Application.Run(new MainFrm());
    58. instance.ReleaseMutex();
    59. //如果需要窗体重启,并出现问题,将这句改为
    60. //instance.Dispose();
    61. }
    62. else
    63. {
    64. RaiseOtherProcess();
    65. Application.Exit();
    66. }
    67. }
    68. }
    69. }

    主窗体:

    1. using SASPGCalcBookTool.SubFrms;
    2. using System;
    3. using System.Collections.Generic;
    4. using System.ComponentModel;
    5. using System.Data;
    6. using System.Drawing;
    7. using System.Linq;
    8. using System.Text;
    9. using System.Windows.Forms;
    10. namespace SASPGCalcBookTool
    11. {
    12. public partial class MainFrm : Form
    13. {
    14. public static Form Mdifrm { set; get; }
    15. public static Form Sdfrm { set; get; }
    16. public static Form Hdfrm { set; get; }
    17. public MainFrm()
    18. {
    19. InitializeComponent();
    20. }
    21. private void toolStripMenuItem2_Click(object sender, EventArgs e)
    22. {
    23. if (Mdifrm == null || Mdifrm.IsDisposed)
    24. {
    25. Mdifrm = new MainDataIn
    26. {
    27. MdiParent = this
    28. };
    29. Mdifrm.Show();
    30. }
    31. else
    32. {
    33. if (Mdifrm.WindowState == FormWindowState.Minimized)
    34. {
    35. Mdifrm.WindowState = FormWindowState.Normal;
    36. }
    37. }
    38. Mdifrm.Activate();
    39. }
    40. private void toolStripMenuItem3_Click(object sender, EventArgs e)
    41. {
    42. if (Sdfrm == null || Sdfrm.IsDisposed)
    43. {
    44. Sdfrm = new ShellData
    45. {
    46. MdiParent = this
    47. };
    48. Sdfrm.Show();
    49. }
    50. else
    51. {
    52. if (Sdfrm.WindowState == FormWindowState.Minimized)
    53. {
    54. Sdfrm.WindowState = FormWindowState.Normal;
    55. }
    56. }
    57. Sdfrm.Activate();
    58. }
    59. }
    60. }

    子窗体1:

    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Data;
    5. using System.Drawing;
    6. using System.Linq;
    7. using System.Text;
    8. using System.Text.RegularExpressions;
    9. using System.Windows.Forms;
    10. namespace SASPGCalcBookTool.SubFrms
    11. {
    12. public partial class MainDataIn : Form
    13. {
    14. public int Msp { get; set; }
    15. public MainDataIn()
    16. {
    17. InitializeComponent();
    18. }
    19. private void MainDataIn_Load(object sender, EventArgs e)
    20. {
    21. MainShellPart.SelectedIndex = 0;
    22. }
    23. private void MainShellPart_SelectedIndexChanged(object sender, EventArgs e)
    24. {
    25. Program.Msp= int.Parse(MainShellPart.Text);
    26. if (MainFrm.Sdfrm == null || MainFrm.Sdfrm.IsDisposed)
    27. {
    28. return;
    29. }
    30. DataGridView ShlPartData = (DataGridView)MainFrm.Sdfrm.Controls["ShlPartData"];
    31. int rown = int.Parse(MainShellPart.Text), n = ShlPartData.Rows.Count;
    32. if (rown > n)
    33. {
    34. for (int i = n + 1; i <= rown; i++)
    35. {
    36. int index = ShlPartData.Rows.Add();
    37. ShlPartData.Rows[index].Cells[0].Value = "筒体段" + i;
    38. }
    39. }
    40. else if (rown < n)
    41. {
    42. for (int i = n; i > rown; i--)
    43. {
    44. ShlPartData.Rows.RemoveAt(i-1);
    45. }
    46. }
    47. }
    48. }
    49. }

    子窗体2:

    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Data;
    5. using System.Drawing;
    6. using System.Linq;
    7. using System.Text;
    8. using System.Windows.Forms;
    9. using SASPGCalcBookTool.SubFrms;
    10. namespace SASPGCalcBookTool.SubFrms
    11. {
    12. public partial class ShellData : Form
    13. {
    14. public ShellData()
    15. {
    16. InitializeComponent();
    17. }
    18. private void ShellData_Load(object sender, EventArgs e)
    19. {
    20. for (int i = 1; i <= Program.Msp; i++)
    21. {
    22. int index = ShlPartData.Rows.Add();
    23. ShlPartData.Rows[index].Cells[0].Value = "筒体段" + i;
    24. }
    25. }
    26. public void _ChangShlPartDataRow(int rown)
    27. {
    28. int n = ShlPartData.Rows.Count;
    29. if (rown > n)
    30. {
    31. for (int i = n+1; i <= rown; i++)
    32. {
    33. int index = ShlPartData.Rows.Add();
    34. ShlPartData.Rows[index].Cells[0].Value = "筒体段" + i;
    35. }
    36. }
    37. else if (rown < n)
    38. {
    39. for (int i = n; i > rown; i--)
    40. {
    41. ShlPartData.Rows.RemoveAt(i);
    42. }
    43. }
    44. }
    45. }
    46. }