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 Name
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


Attachments

Comments

Author: Nirav Lalan10 Dec 2015 Member Level: Gold   Points : 0

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.

Author: Nathan10 Dec 2015 Member Level: Gold   Points : 0

Hello Nirav Lalan,

Thanks for you suggestion. I have attached the sample output .



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: