0

我有一个使用 Microsoft 提供的教程设置为 SingleInstance 的 C# windows 窗体,该教程在此处找到:Windows 窗体单实例应用程序 (CSWinFormSingleInstanceApp)。我遇到的问题是获取在第一个参数之后添加的任何其他参数。

程序.cs:

[STAThread]
static void Main(string[] args)
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    SingleInstanceAppStarter.Start((args.Length == 0
        ? new frm_remPhotoViewer(string.Empty)
        : new frm_remPhotoViewer(args[0])),
        StartNewInstance);
}

static void StartNewInstance(object sender, StartupNextInstanceEventArgs e)
{
    FormCollection forms = Application.OpenForms;
    //MessageBox.Show(e.CommandLine[1]);
    if (forms["frm_remPhotoViewer"] != null
        && forms["frm_remPhotoViewer"].WindowState == FormWindowState.Minimized)
    {
        forms["frm_remPhotoViewer"].WindowState = FormWindowState.Normal;
        forms["frm_remPhotoViewer"].Activate();
    }
    else if (forms["frm_remPhotoViewer"] == null)
    {
        frm_remPhotoViewer f = new frm_remPhotoViewer("HELP ME!!!");
        f.ShowDialog();
    }
}

SingleInstanceAppStarter.cs:

static SingleInstanceApp app = null;

// Construct SingleInstanceApp object, and invoke its run method.
public static void Start(Form f, StartupNextInstanceEventHandler handler)
{
    if (app == null && f != null)
        app = new SingleInstanceApp(f);

    // Wire up StartupNextInstance event handler.
    app.StartupNextInstance += handler;
    app.Run(Environment.GetCommandLineArgs());
}

SingleInstanceAppHelper.cs:

public SingleInstanceApp()
{
}

public SingleInstanceApp(Form f)
{
    // Set IsSingleInstance property to true to make the application 
    base.IsSingleInstance = true;
    // Set MainForm of the application.
    this.MainForm = f;
}

被注释掉的行是我要添加到活动表单的文本。

4

2 回答 2

1

我可能不完全理解你在第一个参数之后获得额外参数的意思......但认为你指的是命令行参数

void Main(string[] args)

您正在创建您的表单。

new frm_remPhotoViewer(string.Empty) 
or
new frm_remPhotoViewer(args[0])

但似乎想要 args[0]、args[1] 等的可能性...

为什么不将表单的构造函数参数从排除单个字符串设置为接受字符串数组

public YourFormConstructor( string someParm )
to
public YourFormConstructor( string[] someParmArray )

然后,在表单的构造函数中,您可以对数组的长度进行测试并在那里做任何您需要的事情。例如:

public YourFormConstructor( string[] someParmArray )
{
   if( someParmArray.Length == 0 )
      form.SomeProperty = string.Empty;
   else
   {
      foreach( string s in someParmArray )
      {
         // do something based on each string "s" provided..
         if( s.StartsWith( "-aCommandLineArgumentFlag1" ))
           blah...

         if( s.StartsWith( "-aDiffCommandLineArgumentFlag" ))
           blah...
      }
   }
}
于 2013-09-29T10:50:40.917 回答
-1
 static class Program
    {

        static MainForm mainForm;
        [STAThread]
        static void Main(params string[] Arguments)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            mainForm = new MainForm(Arguments);
            SingleInstanceApplication.Run(mainForm, NewInstanceHandler);

        }

        public static void NewInstanceHandler(object sender, StartupNextInstanceEventArgs e)
        {
            mainForm.FileName(e.CommandLine[1]);

            e.BringToForeground = false;
        }

        public class SingleInstanceApplication : WindowsFormsApplicationBase
        {
            private SingleInstanceApplication()
            {
                base.IsSingleInstance = true;

            }

            public static void Run(RibbonForm f, StartupNextInstanceEventHandler startupHandler)
            {
                SingleInstanceApplication app = new SingleInstanceApplication();
                app.MainForm= f;
                if (f.DialogResult != DialogResult.Cancel)
                {
                    app.StartupNextInstance += startupHandler;
                    app.Run(Environment.GetCommandLineArgs());
                }
            }



        }
//add this to MainForm
public void FileName(string args)
 {  }
于 2014-02-14T21:23:11.497 回答