2012-11-06 131 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; 
    } 
} 
相關問題