첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

728x90
반응형

TestProject.zip
다운로드

▶ MainForm.cs

using Microsoft.Win32.SafeHandles;
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace TestProject
{
    /// <summary>
    /// 메인 폼
    /// </summary>
    public partial class MainForm : Form
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Import
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Private

        #region 표준 핸들 구하기 - GetStdHandle(standardHandle)

        /// <summary>
        /// 표준 핸들 구하기
        /// </summary>
        /// <param name="standardHandle">표준 핸들</param>
        /// <returns>표준 핸들</returns>
        [DllImport("kernel32.dll", EntryPoint = "GetStdHandle", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] 
        private static extern IntPtr GetStdHandle(int standardHandle);

        #endregion
        #region 콘솔 할당하기 - AllocConsole()

        /// <summary>
        /// 콘솔 할당하기
        /// </summary>
        /// <returns>처리 결과</returns>
        [DllImport("kernel32.dll", EntryPoint = "AllocConsole", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        private static extern int AllocConsole();

        #endregion
        #region 콘솔 창 숨기기 - FreeConsole()
        
        /// <summary>
        /// 콘솔 창 숨기기
        /// </summary>
        /// <returns>처리 결과</returns>
        [DllImport("kernel32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool FreeConsole();

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region Field

        /// <summary>
        /// STANDARD_OUTPUT_HANDLE
        /// </summary>
        private const int STANDARD_OUTPUT_HANDLE = -11;

        /// <summary>
        /// MY_CODE_PAGE
        /// </summary>
        private const int MY_CODE_PAGE = 437;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainForm()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainForm()
        {
            InitializeComponent();

            this.testButton.Click += testButton_Click;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 테스트 버튼 클릭시 처리하기 - testButton_Click(sender, e)

        /// <summary>
        /// 테스트 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void testButton_Click(object sender, EventArgs e)
        {
            try
            {
                this.testButton.Enabled = false;

                ShowConsole();

                for(int i = 0; i < 10; i++)
                {
                    Console.WriteLine("테스트");

                    Thread.Sleep(100);
                }
            }
            finally
            {
                HideConsole();

                this.testButton.Enabled = true;
            }
        }

        #endregion

        #region 콘솔 보여주기 - ShowConsole()

        /// <summary>
        /// 콘솔 보여주기
        /// </summary>
        private void ShowConsole()
        {
            AllocConsole();

            IntPtr standardHandle = GetStdHandle(STANDARD_OUTPUT_HANDLE);

            SafeFileHandle standardSafeFileHandle = new SafeFileHandle(standardHandle, true);

            FileStream fileStream = new FileStream(standardSafeFileHandle, FileAccess.Write);

            Encoding encoding = Encoding.Default;

            StreamWriter streamWriter = new StreamWriter(fileStream, encoding);

            streamWriter.AutoFlush = true;

            Console.SetOut(streamWriter);
        }

        #endregion
        #region 콘솔 숨기기 - HideConsole()

        /// <summary>
        /// 콘솔 숨기기
        /// </summary>
        private void HideConsole()
        {
            FreeConsole();
        }

        #endregion
    }
}
728x90
반응형
Posted by 사용자 icodebroker

댓글을 달아 주세요