c# 隐藏 控制台应用程序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace HideConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Title = "HideConsole";
            IntPtr hwnd = FindWindow(null, Console.Title);
            if (hwnd != IntPtr.Zero)
                ShowWindow(hwnd, CmdShow.SW_HIDE);
            //Process p = new Process();
            /////cmd 名称
            //p.StartInfo.FileName = Process.GetCurrentProcess().MainModule.ModuleName;
            //p.StartInfo.CreateNoWindow = true;
            //p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            Console.WriteLine("控制台应用程序隐藏DOS界面");

            Console.ReadKey();
        }

        #region WinAPI
        /// <summary>
        /// Sets the specified window‘s show state.
        /// </summary>
        /// <param name="hWnd">A handle to the window.</param>
        /// <param name="nCmdShow">Controls how the window is to be shown. This parameter is ignored the first time an application calls ShowWindow, if the program that launched the application provides a STARTUPINFO structure. Otherwise, the first time ShowWindow is called, the value should be the value obtained by the WinMain function in its nCmdShow parameter. In subsequent calls</param>
        /// <returns></returns>
        [DllImport("User32.dll", EntryPoint = "ShowWindow")]
        private static extern bool ShowWindow(IntPtr hWnd, CmdShow nCmdShow);
        /// <summary>
        /// Retrieves a handle to the top-level window whose class name and window name match the specified strings. This function does not search child windows. This function does not perform a case-sensitive search.
        /// </summary>
        /// <param name="lpClassName">The class name or a class atom created by a previous call to the RegisterClass or RegisterClassEx function. The atom must be in the low-order word of lpClassName; the high-order word must be zero.
        /// If lpClassName points to a string, it specifies the window class name. The class name can be any name registered with RegisterClass or RegisterClassEx, or any of the predefined control-class names.
        /// If lpClassName is NULL, it finds any window whose title matches the lpWindowName parameter.</param>
        /// <param name="lpWindowName">The window name (the window‘s title). If this parameter is NULL, all window names match.</param>
        /// <returns>If the function succeeds, the return value is a handle to the window that has the specified class name and window name.
        ///If the function fails, the return value is NULL.</returns>
        [DllImport("user32.dll")]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        #endregion
    }

    /// <summary>
    /// http://msdn.microsoft.com/en-us/library/ms633548(VS.85).aspx
    /// </summary>
    public enum CmdShow : int
    {
        /// <summary>
        /// Hides the window and activates another window.
        /// </summary>
        SW_HIDE = 0,
        /// <summary>
        /// Activates and displays a window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when displaying the window for the first time.
        /// </summary>
        SW_SHOWNORMAL = 1,
        /// <summary>
        /// Activates the window and displays it as a minimized window.
        /// </summary>
        SW_SHOWMINIMIZED = 2,
        /// <summary>
        /// Activates the window and displays it as a maximized window.
        /// </summary>
        SW_SHOWMAXIMIZED = 3,
        /// <summary>
        /// Displays a window in its most recent size and position. This value is similar to SW_SHOWNORMAL, except that the window is not activated.
        /// </summary>
        SW_SHOWNOACTIVATE = 4,
        /// <summary>
        /// Activates the window and displays it in its current size and position.
        /// </summary>
        SW_SHOW = 5,
        /// <summary>
        /// Minimizes the specified window and activates the next top-level window in the Z order.
        /// </summary>
        SW_MINIMIZE = 6,
        /// <summary>
        /// Displays the window as a minimized window. This value is similar to SW_SHOWMINIMIZED, except the window is not activated.
        /// </summary>
        SW_SHOWMINNOACTIVE = 7,
        /// <summary>
        /// Displays the window in its current size and position. This value is similar to SW_SHOW, except that the window is not activated.
        /// </summary>
        SW_SHOWNA = 8,
        /// <summary>
        /// Activates and displays the window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when restoring a minimized window.
        /// </summary>
        SW_RESTORE = 9,
        /// <summary>
        /// Sets the show state based on the SW_ value specified in the STARTUPINFO structure passed to the CreateProcess function by the program that started the application.
        /// </summary>
        SW_SHOWDEFAULT = 10,
        /// <summary>
        /// Minimizes a window, even if the thread that owns the window is not responding. This flag should only be used when minimizing windows from a different thread.
        /// </summary>
        SW_FORCEMINIMIZE = 11
    }
}
时间: 2024-10-10 19:05:20

c# 隐藏 控制台应用程序的相关文章

在控制台程序中隐藏控制台窗口

大家都知道,当编写一个win32 console application时,当运行此类程序的时候默认情况下会有一个类似DOS窗口的console窗口,但是有的时候我们只想在程序中运行一段功能代码,不希望显示这个console窗口,让代码执行完毕之后程序自动退出. 下面就介绍一下,如何隐藏win32 console application的console窗口 因为此种方法是通过设置编译器的链接开关来实现,所以让我们来看一下编译器的链接开关选项(也就是linker选项). 首先我们来看一下linke

.net 中通过控制台启动调用已经存在的应用程序,如何隐藏控制台本身

.net 中通过控制台启动调用已经存在的应用程序,如何隐藏控制台本身? [DllImport("User32.dll", EntryPoint = "FindWindow")] private static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("User32.dll", EntryPoint = "ShowWindow

如何创建 C# 控制台应用程序

[转] 如何:创建 C# 控制台应用程序 本主题旨在生成最简单形式的 C# 程序(控制台应用程序)熟悉 Visual Studio 2008 开发环境.由于控制台应用程序是在命令行执行其所有的输入和输出,因此对于快速测试语言功能和编写命令行实用工具,它们是理想的选择. 在本节中,将学习到: 如何创建新的控制台应用程序. 如何在代码编辑器中使用书签. 如何查看解决方案资源管理器. 如何使代码保持良好的格式. 如何使用 IntelliSense 更快更准确地输入代码. 如何生成并运行应用程序. 在该

隐藏控制台窗口的方法

在我们做程序的过程中,往往都需要用到控制台的程序来做实验或者用作后台的一些小应用的时候我们通常能够用到控制台程序,而,很多时候我们不需要去展现他得窗口,如何去隐藏他得窗口呢?很简单…… 首先先看一段简单的程序,这段程序就是我们用来去隐藏窗口的方法了! #region 隐藏窗口 [DllImport("user32.dll", EntryPoint = "ShowWindow", SetLastError = true)] private static extern

隐藏控制台console application窗口

  如题,在windows上新建控制台应用程序的时候,怎么才能将console 黑窗口隐藏起来.   第一种方法: ::SetConsoleTitle(_T("test_exe")); HWND hwnd=::FindWindow(_T(""),_T("test_exe")); ::ShowWindow(hwnd,SW_HIDE); 不行,试了,不清楚为什么失败-. 窗口句柄抓到了,就是不明白为什么隐藏不了......⊙﹏⊙b汗 两种方法抓到的句

双缓冲解决控制台应用程序输出“闪屏”(C/C++,Windows)

使用 C 语言编写游戏的小伙伴们想必起初都要遇到这样的问题,在不断清屏输出数据的过程中,控制台中的输出内容会不断地闪屏.出现这个问题的原因是程序对数据处理花掉的时间影响到了数据显示,或许你可以使用局部覆盖更新方法(减少更新数据量)来缓解闪屏,但是这种方法并不适用于所有场合,尤其是更新数据本身就非常大的场合. 本文将讲述解决控制台应用程序输出闪屏的终级解决方法——双缓冲. 问题呈现 下面的代码演示了在高速不断清屏输出数据的过程的闪屏问题,特邀您一试: 1 2 3 4 5 6 7 8 9 10 11

C#取得控制台应用程序的根目录方法 判断文件夹是否存在,不存在就创建

取得控制台应用程序的根目录方法1:Environment.CurrentDirectory 取得或设置当前工作目录的完整限定路径2:AppDomain.CurrentDomain.BaseDirectory 获取基目录,它由程序集冲突解决程序用来探测程序集  取得WinForm应用程序的根目录方法1:Environment.CurrentDirectory.ToString();//获取或设置当前工作目录的完全限定路径2:Application.StartupPath.ToString();//

Windows 隐藏控制台

#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" ) 在程序中加上如上语句,可以隐藏控制台窗口. 用VC编写的程序,运行的时候需要C/C++运行库的支持. 当我们运行一个程序时,连接器会首先寻找应用程序的启动函数. 以下内容为引用,因小A水平有限,不懂编译器连接开关,未能证实.但结果有效,内容应该是合理的. 引用: 如果你建立了一个console

如何编写一个编译c#控制台应用程序的批处理程序

如何编写一个编译c#控制台应用程序的批处理程序 2011-03-22 18:14 dc毒蘑菇 | 浏览 579 次 最近在网上看了一个教程,是学C#的,但是我的机子上装不上vs,所以想写一个批处理来编译,因为每次都要我更改目录,然后复制路径,再编译,输出,特别的浪费时间,所以特来求助网友,希望帮帮忙 分享到: 2011-03-22 19:17 #快乐假期,智慧随行# 提问者采纳 不知道你有没有使用过ANT,你可以创建ANT脚本来构建你的应用程序.如果不是很了解,也不愿意编写的话,我介绍你一款可视