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

728x90
반응형

TestProject.zip
0.12MB

▶ MainForm.cs

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Media;
using System.Windows.Forms;

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

        #region Field

        /// <summary>
        /// 사운드 플레이어
        /// </summary>
        private SoundPlayer soundPlayer = new SoundPlayer();


        /// <summary>
        /// 위치 설정 가능 여부
        /// </summary>
        bool canSetLocation = true;

        /// <summary>
        /// 폼 위치
        /// </summary>
        private Point formLocation;

        /// <summary>
        /// 폼 크기
        /// </summary>
        private Size  formSize;


        /// <summary>
        /// 이미지 저장 가능 여부
        /// </summary>
        bool canSaveImage = false;

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

        /// <summary>
        /// 비트맵
        /// </summary>
        private Bitmap bitmap = null;

        #endregion

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

        #region 생성자 - MainForm()

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

            LocationChanged += Form_LocationChanged;
            KeyPress        += Form_KeyPress;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region 폼 위치 변경시 처리하기 - Form_LocationChanged(sender, e)

        /// <summary>
        /// 폼 위치 변경시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void Form_LocationChanged(object sender, EventArgs e)
        {
            if(this.canSetLocation == true)
            {
                this.formLocation = Location;
                this.formSize     = Size;
            }
        }

        #endregion
        #region 폼 키 PRESS시 처리하기 - Form_KeyPress(sender, e)

        /// <summary>
        /// 폼 키 PRESS시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void Form_KeyPress(object sender, KeyPressEventArgs e)
        {
            if(e.KeyChar == 'c' || e.KeyChar == 'C')
            {
                this.canSetLocation = false;
                this.canSaveImage   = true;

                FormBorderStyle = FormBorderStyle.None;
                Location        = new Point(0, 0);
                Size            = Screen.PrimaryScreen.Bounds.Size;
                Opacity         = 0.0;

                Rectangle rectangle = Screen.PrimaryScreen.Bounds;

                this.bitmap = new Bitmap(rectangle.Width, rectangle.Height);

                this.graphics = Graphics.FromImage(this.bitmap);

                this.graphics.CopyFromScreen(PointToScreen(new Point(0,0)), new Point(0, 0), rectangle.Size);

                this.pictureBox.Image = this.bitmap;

                this.soundPlayer.SoundLocation = @"..\..\wav\capture.wav";

                this.soundPlayer.Play();

                FormBorderStyle = FormBorderStyle.FixedSingle;
                Location        = formLocation;
                Size            = formSize;
                Opacity         = 100.0;
                
                this.canSetLocation = true;
            }
            else if(e.KeyChar == 'e' || e.KeyChar == 'E')
            {
                this.soundPlayer.SoundLocation = @"..\..\wav\ereser.wav";

                this.soundPlayer.Play();

                this.canSaveImage = false;

                this.pictureBox.Image = null;
            }
            else if(e.KeyChar == 's' || e.KeyChar == 'S')
            {
                if(this.canSaveImage == true)
                {
                    using(SaveFileDialog saveFileDialog = new SaveFileDialog())
                    {
                        saveFileDialog.OverwritePrompt = true;
                        saveFileDialog.FileName        = "capture";
                        saveFileDialog.Filter          = "Image file(*.png)|*.png";

                        DialogResult dialogResult = saveFileDialog.ShowDialog();

                        if(dialogResult == DialogResult.OK)
                        {
                            this.bitmap.Save(saveFileDialog.FileName, ImageFormat.Png);
                        }
                    }
                }
                else
                {
                    MessageBox.Show("캡처한 화면이 없습니다.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

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

댓글을 달아 주세요