2013-03-19 65 views
0

首先,我想解释“第二次启动”:我使用SingleInstanceController approach可以调用我的应用程序的EXE文件并接受参数。最小化WinForms应用程序在第二次启动时恢复为正常

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

该应用程序设置为以MinimizedWindowState开头,并且仅在用户单击要恢复的托盘图标时才会返回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),以及多文本框来显示一些活动,最调试/信息目的。

与窗体上的控件的交互是否会导致还原?

回答

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; 
    } 
} 
+0

真棒!之前有什么奇怪的是,它只是*恢复窗口状态的第二次启动。在手动最小化窗口后,第3,第4 ...第n次启动使窗口最小化。 – MartinHN 2013-03-20 07:45:56

相关问题