C# e那么我将得到哪个控制台?据我所知,你会得到最近的控制台在z顺序,不一定是一个链接到你想要的应用程序。另外,谢谢你的帮助。在任何情况下,我可能需要使用GetConsoleWindow(),尽管我不希望如此。@Zeos6您将绝对不会获得“最近的”,但根据MS

C# e那么我将得到哪个控制台?据我所知,你会得到最近的控制台在z顺序,不一定是一个链接到你想要的应用程序。另外,谢谢你的帮助。在任何情况下,我可能需要使用GetConsoleWindow(),尽管我不希望如此。@Zeos6您将绝对不会获得“最近的”,但根据MS,c#,winforms,winforms-interop,C#,Winforms,Winforms Interop,e那么我将得到哪个控制台?据我所知,你会得到最近的控制台在z顺序,不一定是一个链接到你想要的应用程序。另外,谢谢你的帮助。在任何情况下,我可能需要使用GetConsoleWindow(),尽管我不希望如此。@Zeos6您将绝对不会获得“最近的”,但根据MS,与您的应用程序关联的控制台(请参阅)。。。这个API调用只考虑进程上下文,而不考虑任何屏幕坐标。太好了!非常感谢你的帮助。 using System; using System.Collections.Generic; using Syste


e那么我将得到哪个控制台?据我所知,你会得到最近的控制台在z顺序,不一定是一个链接到你想要的应用程序。另外,谢谢你的帮助。在任何情况下,我可能需要使用GetConsoleWindow(),尽管我不希望如此。@Zeos6您将绝对不会获得“最近的”,但根据MS,与您的应用程序关联的控制台(请参阅)。。。这个API调用只考虑进程上下文,而不考虑任何屏幕坐标。太好了!非常感谢你的帮助。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Diagnostics;
using System.Runtime.InteropServices;

namespace bsa_working
{
    public partial class Form1 : Form
    {
        static bool console_on = false;

        public Form1()
        {
            InitializeComponent();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (ViewConsole.Checked)
            {
                Win32.AllocConsole();
                ConsoleProperties.ConsoleMain();

                // Set console flag to true
                console_on = true;  // will be used later
            }
            else
                Win32.FreeConsole();
        }
    }

    public class Win32
    {
        [DllImport("kernel32.dll")]
        public static extern Boolean AllocConsole();
        [DllImport("kernel32.dll")]
        public static extern Boolean FreeConsole();
    }

    public class ConsoleProperties
    {
        [DllImport("user32.dll")]
        static extern bool EnableMenuItem(IntPtr hMenu, uint uIDEnableItem, uint uEnable);

        [DllImport("user32.dll")]
        static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);

        [DllImport("user32.dll")]
        static extern IntPtr RemoveMenu(IntPtr hMenu, uint nPosition, uint wFlags);

        internal const uint SC_CLOSE = 0xF060;
        internal const uint MF_GRAYED = 0x00000001;
        internal const uint MF_BYCOMMAND = 0x00000000;

        public static void ConsoleMain()
        {
            IntPtr hMenu = Process.GetCurrentProcess().MainWindowHandle;
            IntPtr hSystemMenu = GetSystemMenu(hMenu, false);

            EnableMenuItem(hSystemMenu, SC_CLOSE, MF_GRAYED);
            RemoveMenu(hSystemMenu, SC_CLOSE, MF_BYCOMMAND);

            // Set console title
            Console.Title = "Test Console";

            // Set console surface foreground and background color
            Console.BackgroundColor = ConsoleColor.DarkBlue;
            Console.ForegroundColor = ConsoleColor.White;
            Console.Clear();
        }
    }
}