Hide/Show Console window in .Net 1.1 Console application

In my previous post about how to hide console window in .Net 2.0 and higher Console application, some questions was how to do this in .Net 1.1 because in .net 1.1 there's not had the Console.Title property.
The way to do is the same, and we need revise some code with the FindWindow function to get the handle of console window. When we get it's handle, it's mean we can hide/show it.

please check the revised code below:


class MyClass
{
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

[STAThread]
static void Main(string[] args)
{
if (args.Length == 0)
{
// Hide console
HideConsole();
// Display manage UI
Application.Run(new mainForm());
}
}
static void HideConsole()
{
System.Diagnostics.Process p = System.Diagnostics.Process.GetCurrentProcess();
IntPtr hWnd = FindWindow(null, p.MainWindowTitle);
if(hWnd != IntPtr.Zero)
{
ShowWindow(hWnd, 0); // 0 = SW_HIDE
}
}
static void ShowConsole()
{
System.Diagnostics.Process p = System.Diagnostics.Process.GetCurrentProcess();
IntPtr hWnd = FindWindow(null, p.MainWindowTitle);
if (hWnd != IntPtr.Zero)
{
ShowWindow(hWnd, 1); //1 = SW_SHOWNORMA
}
}
}

If you have other better ways to hide/show console, i'm very happy and glad to read your comments.

0 nhận xét:

 

Coding experience share Copyright © 2010 | Designed by Ipietoon for Free Blogger Template