■ 프린터 해상도 설정하기

------------------------------------------------------------------------------------------------------------------------


TestProject.zip


MainForm.cs

 

 

using System;

using System.Drawing;

using System.Drawing.Drawing2D;

using System.Drawing.Printing;

using System.Windows.Forms;

 

namespace TestProject

{

    /// <summary>

    /// 메인 폼

    /// </summary>

    public partial class MainForm : Form

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

 

            #region 이벤트를 설정한다.

 

            Load                                         += Form_Load;

            this.printerComboBox.SelectedIndexChanged    += printerComboBox_SelectedIndexChanged;

            this.resolutionComboBox.SelectedIndexChanged += resolutionComboBox_SelectedIndexChanged;

            this.printButton.Click                       += printButton_Click;

            this.printDocument.PrintPage                 += printDocument_PrintPage;

 

            #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)

        {

            foreach(string printer in PrinterSettings.InstalledPrinters)

            {

                this.printerComboBox.Items.Add(printer);

            }

        }

 

        #endregion

        #region 프린터 콤보 박스 선택 인덱스 변경시 처리하기 - printerComboBox_SelectedIndexChanged(sender, e)

 

        /// <summary>

        /// 프린터 콤보 박스 선택 인덱스 변경시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void printerComboBox_SelectedIndexChanged(object sender, EventArgs e)

        {

            this.printDocument.PrinterSettings.PrinterName = this.printerComboBox.Text;

 

            this.resolutionComboBox.Items.Clear();

 

            foreach(PrinterResolution printerResolution in this.printDocument.DefaultPageSettings.PrinterSettings.PrinterResolutions)

            {

                resolutionComboBox.Items.Add(printerResolution.ToString());

            }

 

            this.printButton.Enabled = ((this.printerComboBox.SelectedIndex > -1) && (this.resolutionComboBox.SelectedIndex > -1));

        }

 

        #endregion

        #region 해상도 콤보 박스 선택 인덱스 변경시 처리하기 - resolutionComboBox_SelectedIndexChanged(sender, e)

 

        /// <summary>

        /// 해상도 콤보 박스 선택 인덱스 변경시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void resolutionComboBox_SelectedIndexChanged(object sender, EventArgs e)

        {

            this.printButton.Enabled = ((this.printerComboBox.SelectedIndex > -1) && (this.resolutionComboBox.SelectedIndex > -1));

        }

 

        #endregion

        #region 인쇄하기 버튼 클릭시 처리하기 - printButton_Click(sender, e)

 

        /// <summary>

        /// 인쇄하기 버튼 클릭시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void printButton_Click(object sender, EventArgs e)

        {

            this.printDocument.PrinterSettings.PrinterName = this.printerComboBox.Text;

 

            this.printDocument.DefaultPageSettings.PrinterResolution =

                this.printDocument.DefaultPageSettings.PrinterSettings.PrinterResolutions[resolutionComboBox.SelectedIndex];

 

            this.printDocument.Print();

        }

 

        #endregion

        #region 인쇄 문서 페이지 인쇄시 처리하기 - printDocument_PrintPage(sender, e)

 

        /// <summary>

        /// 인쇄 문서 페이지 인쇄시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void printDocument_PrintPage(object sender, PrintPageEventArgs e)

        {

            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

 

            e.Graphics.TranslateTransform(1, 1);

 

            e.Graphics.ScaleTransform(100, 100, MatrixOrder.Append);

 

            e.Graphics.TranslateTransform(e.MarginBounds.X, e.MarginBounds.Y, MatrixOrder.Append);

 

            DrawImage(e.Graphics);

        }

 

        #endregion

 

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

 

        #region 이미지 그리기 - DrawImage(graphics)

 

        /// <summary>

        /// 이미지 그리기

        /// </summary>

        /// <param name="graphics">그래픽스</param>

        private void DrawImage(Graphics graphics)

        {

            using(Pen pen = new Pen(Color.Black, 0))

            {

                graphics.FillEllipse(Brushes.Yellow, -1, -1, 2, 2);

                graphics.DrawEllipse(pen, -1, -1, 2, 2);

 

                graphics.FillEllipse(Brushes.LightGreen, -0.5f, -0.5f, 0.3f, 0.5f);

                graphics.DrawEllipse(pen, -0.5f, -0.5f, 0.3f, 0.5f);

                graphics.FillEllipse(Brushes.Black, -0.4f, -0.4f, 0.2f, 0.3f);

 

                graphics.FillEllipse(Brushes.LightGreen, 0.2f, -0.5f, 0.3f, 0.5f);

                graphics.DrawEllipse(pen, 0.2f, -0.5f, 0.3f, 0.5f);

                graphics.FillEllipse(Brushes.Black, 0.3f, -0.4f, 0.2f, 0.3f);

 

                graphics.FillEllipse(Brushes.LightBlue, -0.2f, -0.1f, 0.4f, 0.6f);

                graphics.DrawEllipse(pen, -0.2f, -0.1f, 0.4f, 0.6f);

 

                graphics.DrawArc(pen, -0.75f, -0.75f, 1.5f, 1.5f, 20, 120);

            }

        }

 

        #endregion

    }

}

 

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요