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

■ 배경 이미지 움직이기

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


TestProject.zip


MainForm.cs

 

 

using System;

using System.Drawing;

using System.Drawing.Drawing2D;

using System.Windows.Forms;

 

namespace TestProject

{

    /// <summary>

    /// 메인 폼

    /// </summary>

    public partial class MainForm : Form

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region Field

 

        /// <summary>

        /// 그라디언트 시작

        /// </summary>

        private float gradientStart = 0;

 

        /// <summary>

        /// 델타

        /// </summary>

        private float delta = 5f;

 

        #endregion

 

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

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

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

 

            #region 이벤트를 설정한다.

 

            this.canvasPictureBox.Paint += canvasPictureBox_Paint;

            this.timer.Tick             += timer_Tick;

 

            #endregion

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Private

        //////////////////////////////////////////////////////////////////////////////// Event

 

        #region 캔버스 픽처 박스 페인트시 처리하기 - canvasPictureBox_Paint(sender, e)

 

        /// <summary>

        /// 캔버스 픽처 박스 페인트시 처리하기

        /// </summary>

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

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

        private void canvasPictureBox_Paint(object sender, PaintEventArgs e)

        {

            int width = this.canvasPictureBox.ClientSize.Width;

 

            SetBackgroundImage(e.Graphics, this.gradientStart, this.gradientStart + width);

 

            this.gradientStart += this.delta;

 

            if(this.gradientStart >= width)

            {

                this.gradientStart = 0;

            }

 

            using(Font font = new Font("Times New Roman", 32, FontStyle.Bold))

            {

                using(StringFormat stringFormat = new StringFormat())

                {

                    stringFormat.Alignment     = StringAlignment.Center;

                    stringFormat.LineAlignment = StringAlignment.Center;

 

                    e.Graphics.DrawString

                    (

                        "Moving Gradient",

                        font,

                        Brushes.Black,

                        this.canvasPictureBox.ClientSize.Width  / 2,

                        this.canvasPictureBox.ClientSize.Height / 2,

                        stringFormat

                    );

                }

            }

        }

 

        #endregion

        #region 타이머 틱 처리하기 - timer_Tick(sender, e)

 

        /// <summary>

        /// 타이머 틱 처리하기

        /// </summary>

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

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

        private void timer_Tick(object sender, EventArgs e)

        {

            this.canvasPictureBox.Refresh();

        }

 

        #endregion

 

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

 

        #region 배경 이미지 설정하기 - SetBackgroundImage(graphics, minimumX, maximumX)

 

        /// <summary>

        /// 배경 이미지 설정하기

        /// </summary>

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

        /// <param name="minimumX">최소 X</param>

        /// <param name="maximumX">최대 X</param>

        private void SetBackgroundImage(Graphics graphics, float minimumX, float maximumX)

        {

            using(LinearGradientBrush brush = new LinearGradientBrush(new PointF(minimumX, 0), new PointF(maximumX, 0),

                Color.Red, Color.Red))

            {

                brush.WrapMode = WrapMode.Tile;

 

                ColorBlend colorBlend = new ColorBlend();

 

                colorBlend.Colors    = new Color[] { Color.Red, Color.White, Color.Red };

                colorBlend.Positions = new float[] { 0, 0.5f, 1 };

 

                brush.InterpolationColors = colorBlend;

 

                graphics.FillRectangle(brush, this.canvasPictureBox.ClientRectangle);

            }

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker

댓글을 달아 주세요