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

TestProject.zip
다운로드

▶ MainForm.cs

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Windows.Forms;

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

        #region Field

        /// <summary>
        /// 메타 파일
        /// </summary>
        private Metafile metafile;

        /// <summary>
        /// 그래픽스
        /// </summary>
        private Graphics graphics;

        /// <summary>
        /// 마지막 레코드 인덱스
        /// </summary>
        private int lastRecordIndex;

        #endregion

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

        #region 생성자 - MainForm()

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

            #region 이벤트를 설정한다.

            Load                                           += Form_Load;
            this.checkAllButton.Click                      += checkAllButton_Click;
            this.uncheckAllButton.Click                    += uncheckAllButton_Click;
            this.recordCheckedListBox.SelectedIndexChanged += recordCheckedListBox_SelectedIndexChanged;

            #endregion
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private
        //////////////////////////////////////////////////////////////////////////////// Event

        #region 폼 로드시 처리하기 - Form_Load(sender, e)

        /// <summary>
        /// 폼 로드시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void Form_Load(object sender, EventArgs e)
        {
            this.recordCheckedListBox.CheckOnClick = true;

            this.canvasPictureBox.SizeMode = PictureBoxSizeMode.Zoom;

            this.metafile = Metafile.FromFile("volleyball.wmf") as Metafile;
            
            using(Graphics graphics = CreateGraphics())
            {
                graphics.EnumerateMetafile(this.metafile, new PointF(0, 0), enumerateMetafileProc1);
            }

            for(int i = 0; i < this.recordCheckedListBox.Items.Count; i++)
            {
                this.recordCheckedListBox.SetItemChecked(i, true);
            }

            DrawMetaFile();
        }

        #endregion
        #region 모두 체크하기 버튼 클릭시 처리하기 - checkAllButton_Click(sender, e)

        /// <summary>
        /// 모두 체크하기 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void checkAllButton_Click(object sender, EventArgs e)
        {
            for(int i = 0; i < this.recordCheckedListBox.Items.Count; i++)
            {
                this.recordCheckedListBox.SetItemChecked(i, true);
            }

            DrawMetaFile();
        }

        #endregion
        #region 모두 체크 취소하기 버튼 클릭시 처리하기 - uncheckAllButton_Click(sender, e)

        /// <summary>
        /// 모두 체크 취소하기 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void uncheckAllButton_Click(object sender, EventArgs e)
        {
            for(int i = 0; i < this.recordCheckedListBox.Items.Count; i++)
            {
                this.recordCheckedListBox.SetItemChecked(i, false);
            }

            DrawMetaFile();
        }

        #endregion
        #region 레코드 체크 리스트 박스 선택 인덱스 변경시 처리하기 - recordCheckedListBox_SelectedIndexChanged(sender, e)

        /// <summary>
        /// 레코드 체크 리스트 박스 선택 인덱스 변경시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void recordCheckedListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            DrawMetaFile();
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////// Function

        #region 메타 파일 열거하기 프로시저 1 - enumerateMetafileProc1(recordType, flag, dataSize, dataHandle, playRecordCallback)

        /// <summary>
        /// 메타 파일 열거하기 프로시저 1
        /// </summary>
        /// <param name="recordType">레코드 타입</param>
        /// <param name="flag">플래그</param>
        /// <param name="dataSize">데이터 크기</param>
        /// <param name="dataHandle">데이터 핸들</param>
        /// <param name="playRecordCallback">레코드 재생하기 콜백</param>
        /// <returns>처리 결과</returns>
        private bool enumerateMetafileProc1(EmfPlusRecordType recordType, int flag, int dataSize, IntPtr dataHandle, PlayRecordCallback playRecordCallback)
        {
            this.recordCheckedListBox.Items.Add(recordType.ToString());

            return true;
        }

        #endregion
        #region 메타 파일 그리기 - DrawMetaFile()

        /// <summary>
        /// 메타 파일 그리기
        /// </summary>
        private void DrawMetaFile()
        {
            int    width  = this.metafile.Width;
            int    height = this.metafile.Height;
            Bitmap bitmap = new Bitmap(width, height);

            this.lastRecordIndex = -1;

            using(this.graphics = Graphics.FromImage(bitmap))
            {
                Rectangle rectangle = new Rectangle(0, 0, width, height);

                this.graphics.EnumerateMetafile(this.metafile, rectangle, enumerateMetafileProc2);
            }

            this.canvasPictureBox.Image = bitmap;
        }

        #endregion
        #region 메타 파일 열거하기 프로시저 2 - enumerateMetafileProc2(recordType, flag, dataSize, dataHandle, playRecordCallback)

        /// <summary>
        /// 메타 파일 열거하기 프로시저 2
        /// </summary>
        /// <param name="recordType">레코드 타입</param>
        /// <param name="flag">플래그</param>
        /// <param name="dataSize">데이터 크기</param>
        /// <param name="dataHandle">데이터 핸들</param>
        /// <param name="playRecordCallback">레코드 재생하기 콜백</param>
        /// <returns>처리 결과</returns>
        private bool enumerateMetafileProc2(EmfPlusRecordType recordType, int flag, int dataSize, IntPtr dataHandle, PlayRecordCallback playRecordCallback)
        {
            this.lastRecordIndex++;

            if(!this.recordCheckedListBox.GetItemChecked(this.lastRecordIndex))
            {
                return true;
            }

            byte[] dataArray = null;

            if(!dataHandle.Equals(IntPtr.Zero))
            {
                dataArray = new byte[dataSize];

                Marshal.Copy(dataHandle, dataArray, 0, dataSize);
            }

            this.metafile.PlayRecord(recordType, flag, dataSize, dataArray);

            return true;
        }

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

댓글을 달아 주세요