첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
다운로드

▶ Program.cs

using System;
using System.Runtime.InteropServices;
using System.Threading;

namespace TestProject
{
    /// <summary>
    /// 프로그램
    /// </summary>
    class Program
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Import
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Private

        #region 콘솔 윈도우 구하기 - GetConsoleWindow()

        /// <summary>
        /// 콘솔 윈도우 구하기
        /// </summary>
        /// <returns>콘솔 윈도우 핸들</returns>
        [DllImport("kernel32.dll")]
        private static extern IntPtr GetConsoleWindow();

        #endregion
        #region 시스템 메뉴 구하기 - GetSystemMenu(windowHandle, revert)

        /// <summary>
        /// 시스템 메뉴 구하기
        /// </summary>
        /// <param name="windowHandle">윈도우 핸들</param>
        /// <param name="revert">메뉴 복사 핸들 여부</param>
        /// <returns>시스템 메뉴 핸들</returns>
        [DllImport("user32.dll")]
        private static extern IntPtr GetSystemMenu(IntPtr windowHandle, bool revert);

        #endregion
        #region 메뉴 항목 이용 가능 여부 설정하기 - EnableMenuItem(menuHandle, menuItemID, enabled)

        /// <summary>
        /// 메뉴 항목 이용 가능 여부 설정하기
        /// </summary>
        /// <param name="menuHandle">메뉴 핸들</param>
        /// <param name="menuItemID">메뉴 항목 ID</param>
        /// <param name="enabled">이용 가능 여부</param>
        /// <returns>처리 결과</returns>
        [DllImport("user32.dll")]
        private static extern bool EnableMenuItem(IntPtr menuHandle, uint menuItemID, uint enabled);

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// SC_CLOSE
        /// </summary>
        private const uint SC_CLOSE = 0xf060;

        /// <summary>
        /// MF_ENABLED
        /// </summary>
        private const uint MF_ENABLED = 0x00000000;

        /// <summary>
        /// MF_GRAYED
        /// </summary>
        private const uint MF_GRAYED = 0x00000001;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Private

        #region 프로그램 시작하기 - Main()

        /// <summary>
        /// 프로그램 시작하기
        /// </summary>
        private static void Main()
        {
            Console.Title = "콘솔(Console) 닫기 버튼 비활성화 하기";

            IntPtr consoleWindowHandle = GetConsoleWindow();

            SetCloseButtonEnabled(consoleWindowHandle, false);

            Console.WriteLine("닫기 버튼을 비활성화 했습니다.");

            Thread.Sleep(3000);

            SetCloseButtonEnabled(consoleWindowHandle, true);

            Console.WriteLine("닫기 버튼을 활성화 했습니다.");

            Console.WriteLine("프로그램을 종료하기 위해 아무 키나 눌러 주시기 바랍니다.");

            Console.ReadKey(true);
        }

        #endregion

        #region 닫기 버튼 이용 가능 여부 설정하기 - SetCloseButtonEnabled(windowHandle, enabled)

        /// <summary>
        /// 닫기 버튼 이용 가능 여부 설정하기
        /// </summary>
        /// <param name="windowHandle">윈도우 핸들</param>
        /// <param name="enabled">이용 가능 여부</param>
        private static void SetCloseButtonEnabled(IntPtr windowHandle, bool enabled)
        {
            IntPtr systemMenuHandle = GetSystemMenu(windowHandle, false);

            EnableMenuItem(systemMenuHandle, SC_CLOSE, (uint)(MF_ENABLED | (enabled ? MF_ENABLED : MF_GRAYED)));
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요