WinForm中如何嵌入控制台

WinForm嵌入控制台样例,用C#编写一些后台小工具时,我们往往希望既能使用winform界面组件,又能有一个控制台窗口用于显示日志信息。本例子就是一个WinForm嵌入控制台的最小样例。

关键代码如下:

完整代码下载:https://download.csdn.net/download/usoa/88200918

    [System.Runtime.InteropServices.DllImport("kernel32.dll", SetLastError = true)]
        [return: System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)]
        static extern bool AllocConsole();//开启

        [System.Runtime.InteropServices.DllImport("Kernel32")]
        public static extern void FreeConsole();//关闭

        [System.Runtime.InteropServices.DllImport("Kernel32")]
        public static extern bool AttachConsole(int dwProcessId);//附加

        [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "FindWindow")]
        extern static IntPtr FindWindow(string? lpClassName, string? lpWindowName);//找出运行的窗口   

        [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "FindWindow")]
        private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

        [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "GetSystemMenu")]
        extern static IntPtr GetSystemMenu(IntPtr hWnd, IntPtr bRevert); //取出窗口运行的菜单   

        [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "RemoveMenu")]
        extern static IntPtr RemoveMenu(IntPtr hMenu, uint uPosition, uint uFlags); //灰掉按钮

        [System.Runtime.InteropServices.DllImport("User32.dll ", EntryPoint = "SetParent")]
        private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);//设置父窗体

        [System.Runtime.InteropServices.DllImport("user32.dll ", EntryPoint = "ShowWindow")]
        public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);//显示窗口

        [System.Runtime.InteropServices.DllImport("user32.dll ", EntryPoint = "SetWindowPos")]
        private static extern int SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int y, int Width, int Height, int flags);

        //控制台窗口句柄
        IntPtr windowHandle;

        void AddConsole()
        {
            if (AllocConsole())
            {
                //通过程序名找窗口,两种方式,推荐第二种
                //windowHandle = FindWindow(null, Process.GetCurrentProcess().MainModule.FileName);
                windowHandle = FindWindow(null, Environment.ProcessPath);
                if (windowHandle == IntPtr.Zero)
                {
                    //通过控制台TITLE找窗口
                    Console.Title = "ConsoleTest";//定义窗口标题再通过标题找到控制台窗口
                    Thread.Sleep(100);
                    windowHandle = FindWindow(null, Console.Title);
                }

                //绑定窗口到panel容器
                if (windowHandle != IntPtr.Zero)
                {
                    SetParent(windowHandle, panel1.Handle);
                    IntPtr closeMenu = GetSystemMenu(windowHandle, IntPtr.Zero);
                    uint SC_CLOSE = 0xF060;
                    RemoveMenu(closeMenu, SC_CLOSE, 0x0);//屏蔽关闭按钮 

                    Console.WindowWidth = 100;
                    Console.SetWindowPosition(0, 0);
                    Console.ForegroundColor = ConsoleColor.Green;
                    ShowWindow(windowHandle, 3);
                }
            }
            else
            {
                MessageBox.Show("请注意控制台窗口未正常打开,请检查环境!", "环境缺失");
            }

        }

 完整代码下载:https://download.csdn.net/download/usoa/88200918

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

24K老游

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

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

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

打赏作者

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

抵扣说明:

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

余额充值