WstępW tym tutorialu pokazuje w jaki sposób wyświetlić konsolę w języku programowania C# w aplikacjach okienkowych (App Form).
RozwiązanieBy wyświetlić konsolę podczas włączania programu, to w klasie "Program" dodajemy na samym początku:
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool AllocConsole();
Jeśli chcemy by konsola wyświetlała się podczas uruchamiania danej formy, czy po przyciśnięciu przyciska, to wstawiamy powyższy kod do klasy danej formy.
Jeśli już to zrobiliśmy to wystarczy że uruchomimy naszą konsolę kodem:
AllocConsole();
A tak wygląda całość:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing;
namespace moj_program
{
static class Program
{
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool AllocConsole();
[STAThread]
static void Main(string[] args)
{
AllocConsole();
Console.Title = "Gothic++ Console";
Console.WriteLine("tutorial z http://pieuchowski.2ap.pl");
Console.WriteLine("Autor: Łukasz Pietuchowski");
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmMain());
}
}
}
Tutorial pochodzi z
http://www.pietuchowski.2ap.pl/article-14.html