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:
Post a Comment