0

首先,我想解释一下“第二次启动”:我使用该SingleInstanceController方法可以调用我的应用程序的EXE文件,并接受参数。

这样,其他应用程序或用户可以告诉应用程序采取特定操作。

该应用程序设置为以 开头WindowStateMinimized并且只有当用户单击托盘图标时,它才会恢复为Normal

但我看到的是,我第一次启动应用程序时它保持最小化。然后当我第二次调用EXE文件时,它恢复到正常的窗口状态。

我没有改变窗口状态的代码。

我怀疑这是因为其他东西正在触发恢复。

我的代码SingleInstanceController如下所示:

public class SingleInstanceController : WindowsFormsApplicationBase
{
    public SingleInstanceController()
    {
        IsSingleInstance = true;

        StartupNextInstance += this_StartupNextInstance;
    }

    void this_StartupNextInstance(object sender, StartupNextInstanceEventArgs e)
    {
        Form1 form = MainForm as Form1;
        string command = e.CommandLine[1];

        switch (command.ToLowerInvariant())
        {
            case "makecall":
                string phoneNumber = e.CommandLine[2];
                PhoneAppHelper.MakePhoneCall(phoneNumber);
                break;
            default:
                System.Windows.Forms.MessageBox.Show("Argument not supported");
                break;
        }
    }

    protected override void OnCreateMainForm()
    {
        MainForm = new Form1();
    }
}

在我的表单上,我有一个列表框来显示连接的设备 (USB),还有一个多行文本框来显示一些活动,主要用于调试/信息目的。

与表单上的控件的交互是否会导致还原?

4

1 回答 1

1

是的,这是 WindowsFormsApplicationBase.OnStartupNextInstance() 的默认行为。您可以通过覆盖方法而不是使用事件来简单地解决这个问题。请注意,当您有要显示的消息时,您可能仍然希望发生这种情况。所以让它看起来像这样:

protected override void OnStartupNextInstance(StartupNextInstanceEventArgs e) {
    //...
    switch (command.ToLowerInvariant()) {
        // etc..
        default:
            base.OnStartupNextInstance(e);   // Brings it to the front
            System.Windows.Forms.MessageBox.Show("Argument not supported");
            break;
    }
}
于 2013-03-19T23:34:31.977 回答