How to change the caption of the Console application
The title is important for all kind of projects. We can change the title of windows application or web application easily. But doing this in console is little bit tricky.
In this article, we can see how we can handle it in the console application.
The title is important for all kind of projects.
In the web application we are using html tag
In the windows application we can do it in the following manner
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
this.Text = "This Is My Title";
}
}
How can we change the caption of the console application?.
We can create the console application. Its very easy. But designing console application is an art.
To Change the simple caption we have to do the following. kernel32.dll is playing big role on this.
Create simple ConsoleHandler class as follows.
using System;
using System.Text;
using System.Runtime.InteropServices;
public class ConsoleHandler
{
[DllImport("kernel32.dll", EntryPoint="SetConsoleTitle", CharSet=CharSet.Auto)]
public static extern bool SetCaption( string caption );
[DllImport("kernel32.dll", EntryPoint="GetConsoleTitle", CharSet=CharSet.Auto)]
public static extern ulong GetCaption( [Out] StringBuilder caption, ulong maxChars );
public static string GetCaption()
{
StringBuilder sb = new StringBuilder(256);
GetCaption(sb, (ulong)sb.Capacity);
return sb.ToString();
}
}
You can change the caption of the console in the following way in your console application
class MyClass
{
static void Main(string[] args)
{
ConsoleHandler.SetCaption("This is my changed caption");
Console.WriteLine("My changed Caption is : {0}", ConsoleHandler.GetCaption());
}
}
Sample output is attached as image.
Thanks
Nathan
Hello Nathan,
It doesn't seems tricky. Please show it with some images so that reader should understand at each step exactly what is happening.
Hope you understand.