C# 控制台面板(最小化,隐藏操作)

在用控制台面板写入一些后台程序,在用于项目中时,会需要隐藏控制面板的操作。毕竟放一个控制台窗口在前台不仅容易被用户操作误关闭,对用户来说,这个窗口也不友好。这时,就需要将窗口进行隐藏。

1.引用 user32.dll 扩展文件

(1)直接从网上下载
(2)从C盘windows\system32目录下copy出来,放到待运行的Debug/Release 目录下

介于user32.dll 为非托管DLL,需要用到 DllImportAttribute 属性 对从非托管 DLL 导出的函数进行调用。
DllImportAttribute 属性(需要引用命名空间 System.Runtime.InteropServices)

2.功能模块如下

class Program
    {
        [DllImport("User32.dll", EntryPoint = "ShowWindow")]   //
        private static extern bool ShowWindow(IntPtr hWnd, int type);

        [DllImport("User32.dll", EntryPoint = "FindWindow")]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        static void Main(string[] args)
        {
            GetHide(); 
            Console.ReadKey();
        }

        /// <summary>
        /// 隐藏控制台窗口
        /// </summary>
        private static void GetHide()
        {
            Console.Title = "TestStartHide";            //当前窗口的标题
            IntPtr ParenthWnd = new IntPtr(0);
            IntPtr et = new IntPtr(0);
            ParenthWnd = FindWindow(null, "TestStartHide");       //根据Title 找到对应窗口的句柄,进行操作
            ShowWindow(ParenthWnd, 0);//隐藏本dos窗体, 0: 后台执行;1:正常启动;2:最小化到任务栏;3:最大化
        }
    }
									 				2019-04-19 留下此篇随笔,仅供参考。
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
要将 C# 控制台应用最小化到托盘,需要使用 NotifyIcon 类。这个类可以在系统托盘区域中显示一个图标,并提供与该图标相关的上下文菜单和事件。下面是一个示例代码: ```csharp using System; using System.Drawing; using System.Windows.Forms; class Program { static NotifyIcon notifyIcon; static void Main(string[] args) { // 创建一个 NotifyIcon 对象 notifyIcon = new NotifyIcon(); notifyIcon.Icon = new Icon("icon.ico"); notifyIcon.Text = "控制台应用程序"; // 添加右键菜单 ContextMenu menu = new ContextMenu(); menu.MenuItems.Add(new MenuItem("退出", OnExit)); notifyIcon.ContextMenu = menu; // 显示 NotifyIcon notifyIcon.Visible = true; // 等待用户输入 Console.ReadLine(); // 隐藏控制台窗口 IntPtr hWnd = NativeMethods.GetConsoleWindow(); NativeMethods.ShowWindow(hWnd, NativeMethods.SW_HIDE); // 显示 NotifyIcon 提示 notifyIcon.ShowBalloonTip(1000, "控制台应用程序", "已最小化到托盘。", ToolTipIcon.Info); } static void OnExit(object sender, EventArgs e) { // 退出应用程序 notifyIcon.Visible = false; Environment.Exit(0); } } class NativeMethods { public const int SW_HIDE = 0; [System.Runtime.InteropServices.DllImport("kernel32.dll")] public static extern IntPtr GetConsoleWindow(); [System.Runtime.InteropServices.DllImport("user32.dll")] public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); } ``` 在这个示例代码中,我们创建了一个 NotifyIcon 对象,并设置它的图标和文本。我们还添加了一个右键菜单,当用户选择“退出”时,调用 OnExit 方法退出应用程序。 在控制台应用程序等待用户输入后,我们使用 NativeMethods.ShowWindow 将控制台窗口隐藏,并调用 notifyIcon.ShowBalloonTip 方法显示一个提示。这样,控制台应用程序就会最小化到托盘了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MelanceXin

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值