1

デバッグ用のコンソールを起動できる Windows フォーム アプリを作成しています。コンソールの閉じるボタンで Windows フォーム アプリをシャットダウンできないように、コンソールの閉じるボタンを無効にしたいと考えています。テスト コードのスケルトンを作成しましたが、動作します。コードは以下のとおりです。

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();
        }
    }
}

コードは正常に動作します:

  1. コードをコンパイルして初めて実行すると、コンソールの X はグレー表示されませんが、Windows フォーム アプリではグレー表示されます。ただし、コードを閉じて再度実行すると、コードは正常に機能します。つまり、コンソールの X はグレー表示され、Windows フォーム アプリは本来あるべき状態になっています。これを修正できる理由と方法はありますか?

  2. コンソールが勝利フォームの背後に表示されることがあります。コンソールが常に一番上になるようにする方法はありますか?

余談ですが、コンソールを WinForm の特定の場所に固定する方法はありますか? アプリ?サイズを設定できるので、特定の場所に固定できれば、フォーム上にその場所を作成できます。

4

1 に答える 1

1

IntPtr hMenu = Process.GetCurrentProcess().MainWindowHandle;これを機能させるには、代わりにコンソールウィンドウのウィンドウハンドルを使用するように変更する必要があります(これはを呼び出すことで取得できますGetConsoleWindow())。

上部に表示するには、たとえばSetForegroundWindowコンソールウィンドウハンドルを使用できます。

ピン留めについては、これが可能かどうかは本当にわかりません。

于 2011-11-10T19:26:58.707 に答える