2012-11-06 94 views
2

我正在研究主要是Windows窗体应用程序的迁移工具。我想要做的是提供将应用程序作为一种命令行实用程序运行的能力,其中可以传入参数并且迁移发生完全无效的GUI。这似乎直线前进足够和切入点,我的应用程序是这样的:将Windows应用程序作为命令行应用程序运行

[STAThread] 
    static void Main(string[] args) 
    { 
     if (args.Length == 0) 
     { 
      Application.EnableVisualStyles(); 
      Application.SetCompatibleTextRenderingDefault(false); 
      Application.Run(new MainWindow()); 
     } 
     else 
     { 
      //Command Line Mode 
      Console.WriteLine("In Command Line Mode"); 
      Console.ReadLine(); 
     } 
    } 

我遇到的问题是,当执行传递到其他阻断的文本没有在命令回信用户提示这是有问题的,因为我想在命令提示符下更新用户,因为各种执行完成。我可以轻松编写一个独立的控制台应用程序,但我希望能够提供一个单一的工具,允许给定场景的不同类型的条目。我期望做到这一点,如果是的话,它是如何实现的?

谢谢!

+1

检查http://stackoverflow.com/questions/277771/how-to- run-a-winform-from-console-application – VladT

回答

2

AllocConsole功能造成这种情况的通常模式是将逻辑写入到一个类库,你要么调用从可视UI或命令行应用程序。例如,如果在UI上接受了“宽度”,“高度”和“深度”,然后计算了体积,则可以将计算放入类库中。

所以,你要么一个控制台应用程序接受三个参数,或者有三个输入表单应用程序,并在这两种情况下,他们做出同样的电话...

var volumeCalculations = new VolumeCalculations(); 
var volume = volumeCalculations.GetVolume(width, height, depth); 

控制台应用程序是非常薄,表单应用程序非常薄弱,因为他们所做的只是将输入传递给类库。

0

这里是完整的可运行示例。

csc RunnableForm.cs RunnableForm.Designer.cs 

RunnableForm.cs:与编译

using System; 
using System.Linq; 
using System.Windows.Forms; 

namespace Test 
{ 
    public partial class RunnableForm : Form 
    { 
     public RunnableForm() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      MessageBox.Show("bang!"); 
     } 

     [STAThread] 
     static void Main() 
     { 

      string[] args = Environment.GetCommandLineArgs(); 
      // We'll always have one argument (the program's exe is args[0]) 
      if (args.Length == 1) 
      { 
       // Run windows forms app 
       Application.Run(new RunnableForm()); 
      } 
      else 
      { 
       Console.WriteLine("We'll run as a console app now"); 
       Console.WriteLine("Arguments: {0}", String.Join(",", args.Skip(1))); 
       Console.Write("Enter a string: "); 
       string str = Console.ReadLine(); 
       Console.WriteLine("You entered: {0}", str); 
       Console.WriteLine("Bye."); 
      } 
     } 
    } 
} 

RunnableForm.Designer.cs:

namespace Test 
{ 
    partial class RunnableForm 
    { 
     /// <summary> 
     /// Required designer variable. 
     /// </summary> 
     private System.ComponentModel.IContainer components = null; 

     /// <summary> 
     /// Clean up any resources being used. 
     /// </summary> 
     /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> 
     protected override void Dispose(bool disposing) 
     { 
      if (disposing && (components != null)) 
      { 
       components.Dispose(); 
      } 
      base.Dispose(disposing); 
     } 

     #region Windows Form Designer generated code 

     /// <summary> 
     /// Required method for Designer support - do not modify 
     /// the contents of this method with the code editor. 
     /// </summary> 
     private void InitializeComponent() 
     { 
      this.button1 = new System.Windows.Forms.Button(); 
      this.SuspendLayout(); 
      // 
      // button1 
      // 
      this.button1.Location = new System.Drawing.Point(42, 42); 
      this.button1.Name = "button1"; 
      this.button1.Size = new System.Drawing.Size(153, 66); 
      this.button1.TabIndex = 0; 
      this.button1.Text = "button1"; 
      this.button1.UseVisualStyleBackColor = true; 
      this.button1.Click += new System.EventHandler(this.button1_Click); 
      // 
      // RunnableForm 
      // 
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
      this.ClientSize = new System.Drawing.Size(284, 261); 
      this.Controls.Add(this.button1); 
      this.Name = "RunnableForm"; 
      this.Text = "RunnableForm"; 
      this.ResumeLayout(false); 

     } 

     #endregion 

     private System.Windows.Forms.Button button1; 
    } 
} 
相关问题