2015-07-20 17 views
1

我有點奇怪的問題。表單應用程序 - 輸出到命令提示符而不創建其他控制檯

我已經創建了一個窗體應用程序,使用菜單,選項,按鈕等。此外,我實現可能性打開和關閉一些選項使用的參數和啓動從命令提示符應用程序。現在我想對額外的「幫助」參數實施反應,我希望它能夠顯示關於所有可能參數和一些示例的信息。

有沒有辦法表現出一定的輸出來安慰我目前的運行,而無需創建額外的控制檯?或者僅僅爲了顯示新的MessageBox和所有參數的描述就更簡單了?

謝謝!

+0

一般情況下。你已經有了一個winforms應用程序。爲什麼你想混合用戶界面類型,並突然打開一個控制檯,顯示其中的信息,以及如何使用winforms應用程序?在你的winforms應用中實現一個「幫助」工具欄按鈕。我會這樣做,我不會按照選項來自定義commandform-args的winforms應用程序的行爲。我會或多或少地將其全部交互或使用設置模式對其進行自定義。 – icbytes

回答

1

如果沒有重要的理由你會使用控制檯 - 我只會使用MessageBox。

混合控制檯和Windows窗體不是好主意。

如果你真的要做到這一點 - 有在kernel32.dll AttachConsole功能。您可以使用它像這樣:

的Program.cs文件:

using System; 
using System.Windows.Forms; 
using System.Runtime.InteropServices; 


namespace Test 
{ 
    static class Program 
    { 
     [DllImport("kernel32.dll")] 
     static extern bool AttachConsole(int dwProcessId); 
     private const int ATTACH_PARENT_PROCESS = -1; 

     [STAThread] 
     static void Main() 
     { 
      AttachConsole(ATTACH_PARENT_PROCESS); 
      Console.WriteLine("This will show on console."); 

      Application.EnableVisualStyles(); 
      Application.SetCompatibleTextRenderingDefault(false); 

      Application.Run(new Form1()); 
     } 
    } 
} 
+0

感謝您的回覆!不幸的是,這並不是我所問的:我想從控制檯運行我的應用程序,然後輸出到相同的控制檯一些信息。無論如何,再次感謝您的回答! –

+1

就是這樣。它將附加控制檯,這是您的應用程序的父進程,而不是創建新的控制檯。 – Kamil

+0

啊,我的錯誤,我被AttachConsole搞糊塗了,我正在嘗試使用AllocConsole()。謝謝! –

1

這是我使用添加控制檯應用程序時,我需要它:

#region Console support 
[System.Runtime.InteropServices.DllImport("Kernel32")] 
public static extern void AllocConsole(); 

[System.Runtime.InteropServices.DllImport("Kernel32")] 
public static extern void FreeConsole(); 
#endregion 

當我們需要打開它時,我們可以撥打電子郵件AllocConsole(),同樣我們也可以撥打FreeConsole()

同時,我創建/使用下面的寫入與顏色控制檯:

/// <summary> 
/// Writes to the Console. Does not terminate line, subsequent write is on right of same line. 
/// </summary> 
/// <param name="color">The color that you want to write to the line with.</param> 
/// <param name="text">The text that you want to write to the console.</param> 
public static void ColoredConsoleWrite(ConsoleColor color, string text) 
{ 
    ConsoleColor originalColor = Console.ForegroundColor; 
    Console.ForegroundColor = color; 
    Console.Write(text); 
    Console.ForegroundColor = originalColor; 
} 

/// <summary> 
/// Writes to the Console. Terminates line, subsequent write goes to new line. 
/// </summary> 
/// <param name="color">The color that you want to write to the line with.</param> 
/// <param name="text">The text that you want to write to the console.</param> 
public static void ColoredConsoleWriteLine(ConsoleColor color, string text) 
{ 
    ConsoleColor originalColor = Console.ForegroundColor; 
    Console.ForegroundColor = color; 
    Console.WriteLine(text); 
    Console.ForegroundColor = originalColor; 
} 

實例: 「!GOOD」

#region User Check 
Console.Write("User: {0} ... ", Environment.UserName); 
if (validUser(Environment.UserName).Equals(false)) 
{ 
    ColoredConsoleWrite(ConsoleColor.Red, "BAD!"); 
    Console.WriteLine(" - Making like a tree, and getting out of here!"); 
    Environment.Exit(0); 
} 
ColoredConsoleWrite(ConsoleColor.Green, "GOOD!"); Console.WriteLine(" - Continue on!"); 
#endregion 

有效用戶輸出在綠色的文字是:

User: Chase.Ring ... GOOD! - Continue on!

無效用戶輸出 「不好!」在紅色文本中:

User: Not.Chase.Ring ... BAD! - Making like a tree, and getting out of here!

相關問題