첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
본 블로그는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 블로그 콘텐츠 향상을 위해 쓰여집니다.

728x90
반응형
728x170

TestProject.zip
다운로드

▶ MainForm.cs

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

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

        #region Field

        /// <summary>
        /// 픽처 박스 리스트
        /// </summary>
        private List<PictureBox> pictureBoxList = new List<PictureBox>();

        /// <summary>
        /// 픽처 마진
        /// </summary>
        private const int PICTURE_MARGIN = 8;

        /// <summary>
        /// 플레이스 홀더 픽처 박스
        /// </summary>
        private PictureBox placeHolderPictureBox;

        /// <summary>
        /// 클릭 인덱스
        /// </summary>
        private int indexClicked = -1;

        /// <summary>
        /// 드래그 픽처 박스
        /// </summary>
        private PictureBox dragPictureBox = null;

        /// <summary>
        /// 드래그 오프셋 포인트
        /// </summary>
        private Point dragOffsetPoint;

        #endregion

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

        #region 생성자 - MainForm()

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

            Load                             += Form_Load;
            this.moveLeftMenuItem.Click      += moveLeftMenuItem_Click;
            this.moveRightMenuItem.Click     += moveRightMenuItem_Click;
            this.deletePictureMenuItem.Click += deletePictureMenuItem_Click;
            this.addPictureMenuItem.Click    += addPictureMenuItem_Click;
            this.pictureBoxPanel.MouseDown   += pictureBoxPanel_MouseDown;
        }

        #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.placeHolderPictureBox = new PictureBox();

            this.placeHolderPictureBox.Location = new Point(PICTURE_MARGIN, PICTURE_MARGIN);
            this.placeHolderPictureBox.Size     = new Size(0, 0);
            this.placeHolderPictureBox.Visible  = true;

            this.pictureBoxPanel.Controls.Add(placeHolderPictureBox);
        }

        #endregion
        #region 왼쪽으로 이동 메뉴 항목 클릭시 처리하기 - moveLeftMenuItem_Click(sender, e)

        /// <summary>
        /// 왼쪽으로 이동 메뉴 항목 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void moveLeftMenuItem_Click(object sender, EventArgs e)
        {
            PictureBox pictureBox = this.pictureBoxList[this.indexClicked];

            this.pictureBoxList.RemoveAt(this.indexClicked);

            this.pictureBoxList.Insert(this.indexClicked - 1, pictureBox);

            ArrangePictureBox();
        }

        #endregion
        #region 오른쪽으로 이동 메뉴 항목 클릭시 처리하기 - moveRightMenuItem_Click(sender, e)

        /// <summary>
        /// 오른쪽으로 이동 메뉴 항목 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void moveRightMenuItem_Click(object sender, EventArgs e)
        {
            PictureBox pictureBox = this.pictureBoxList[this.indexClicked];

            this.pictureBoxList.RemoveAt(this.indexClicked);

            this.pictureBoxList.Insert(this.indexClicked + 1, pictureBox);

            ArrangePictureBox();
        }

        #endregion
        #region 그림 삭제 메뉴 항목 클릭시 처리하기 - deletePictureMenuItem_Click(sender, e)

        /// <summary>
        /// 그림 삭제 메뉴 항목 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void deletePictureMenuItem_Click(object sender, EventArgs e)
        {
            if(MessageBox.Show("이 그림을 삭제하시겠습니까?", "확인", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                this.pictureBoxPanel.Controls.Remove(this.pictureBoxList[indexClicked]);

                this.pictureBoxList.RemoveAt(this.indexClicked);

                ArrangePictureBox();
            }
        }

        #endregion
        #region 그림 추가 메뉴 항목 클릭시 처리하기 - addPictureMenuItem_Click(sender, e)

        /// <summary>
        /// 그림 추가 메뉴 항목 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void addPictureMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if(this.openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    int i = 0;

                    foreach(string filePath in openFileDialog.FileNames)
                    {
                        Bitmap bitmap = new Bitmap(filePath);

                        PictureBox pictureBox = new PictureBox();

                        pictureBox.SizeMode    = PictureBoxSizeMode.AutoSize;
                        pictureBox.Image       = bitmap;
                        pictureBox.Visible     = true;
                        pictureBox.BorderStyle = BorderStyle.Fixed3D;

                        pictureBox.MouseDown += pictureBox_MouseDown;

                        this.pictureBoxPanel.Controls.Add(pictureBox);

                        this.pictureBoxList.Insert(this.indexClicked + i, pictureBox);

                        i++;
                    }

                    ArrangePictureBox();
                }
            }
            catch(Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
        }

        #endregion
        #region 픽처 박스 패널 마우스 DOWN 처리하기 - pictureBoxPanel_MouseDown(sender, e)

        /// <summary>
        /// 픽처 박스 패널 마우스 DOWN 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void pictureBoxPanel_MouseDown(object sender, MouseEventArgs e)
        {
            if(e.Button != MouseButtons.Right)
            {
                return;
            }

            ShowContextMenu(e.Location);
        }

        #endregion
        #region 픽처 박스 패널 마우스 이동시 처리하기 - pictureBoxPanel_MouseMove(sender, e)

        /// <summary>
        /// 픽처 박스 패널 마우스 이동시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void pictureBoxPanel_MouseMove(object sender, MouseEventArgs e)
        {
            int x = e.Location.X + dragOffsetPoint.X;
            int y = e.Location.Y + dragOffsetPoint.Y;

            this.dragPictureBox.Location = new Point(x, y);
        }

        #endregion
        #region 픽처 박스 패널 마우스 UP 처리하기 - pictureBoxPanel_MouseUp(sender, e)

        /// <summary>
        /// 픽처 박스 패널 마우스 UP 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void pictureBoxPanel_MouseUp(object sender, MouseEventArgs e)
        {
            this.dragPictureBox = null;

            this.pictureBoxPanel.MouseMove -= pictureBoxPanel_MouseMove;
            this.pictureBoxPanel.MouseUp   -= pictureBoxPanel_MouseUp;

            OrderPictureBoxList();
        }

        #endregion
        #region 픽처 박스 마우스 DOWN 처리하기 - pictureBox_MouseDown(sender, e)

        /// <summary>
        /// 픽처 박스 마우스 DOWN 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void pictureBox_MouseDown(object sender, MouseEventArgs e)
        {
            PictureBox pictureBox = sender as PictureBox;

            if(e.Button == MouseButtons.Left)
            {
                this.dragPictureBox = pictureBox;

                int deltaX = -e.Location.X;
                int deltaY = -e.Location.Y;

                this.dragOffsetPoint = new Point(deltaX, deltaY);

                this.pictureBoxPanel.Controls.SetChildIndex(pictureBox, 0);

                this.dragPictureBox.Capture = false;

                this.pictureBoxPanel.Capture = true;

                this.pictureBoxPanel.MouseMove += pictureBoxPanel_MouseMove;
                this.pictureBoxPanel.MouseUp   += pictureBoxPanel_MouseUp;
            }
            else
            {
                Point screenPoint = pictureBox.PointToScreen(e.Location);

                Point parentPoint = this.pictureBoxPanel.PointToClient(screenPoint);

                ShowContextMenu(new Point(parentPoint.X, parentPoint.Y));
            }
        }

        #endregion

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

        #region 픽처 박스 배열하기 - ArrangePictureBox()

        /// <summary>
        /// 픽처 박스 배열하기
        /// </summary>
        private void ArrangePictureBox()
        {
            int maximumY = 0;

            int x = PICTURE_MARGIN;
            int y = PICTURE_MARGIN;

            foreach(PictureBox pictureBox in this.pictureBoxList)
            {
                pictureBox.Location = new Point(x, y);

                x += pictureBox.Width + PICTURE_MARGIN;

                if(maximumY < pictureBox.Height)
                {
                    maximumY = pictureBox.Height;
                }
            }

            y = maximumY + 2 * PICTURE_MARGIN;

            this.placeHolderPictureBox.Location = new Point(x, y);
        }

        #endregion
        #region 픽처 박스 리스트 정렬하기 - OrderPictureBoxList()

        /// <summary>
        /// 픽처 박스 리스트 정렬하기
        /// </summary>
        private void OrderPictureBoxList()
        {
            this.pictureBoxList.Sort((p1, p2) => p1.Location.X.CompareTo(p2.Location.X));

            ArrangePictureBox();
        }

        #endregion
        #region 컨텍스트 메뉴 표시하기 - ShowContextMenu(point)

        /// <summary>
        /// 컨텍스트 메뉴 표시하기
        /// </summary>
        /// <param name="point">포인트</param>
        private void ShowContextMenu(Point point)
        {
            bool clickedOnPicture = false;

            this.indexClicked = this.pictureBoxList.Count;

            int x = point.X + this.pictureBoxPanel.HorizontalScroll.Value;

            for(int i = 0; i < this.pictureBoxList.Count; i++)
            {
                x -= PICTURE_MARGIN;

                if(x < 0)
                {
                    this.indexClicked = i;

                    break;
                }   

                x -= this.pictureBoxList[i].Width;

                if(x < 0)
                {
                    this.indexClicked = i;

                    clickedOnPicture = true;

                    break;
                }
            }

            this.moveLeftMenuItem.Enabled      = (clickedOnPicture && (this.indexClicked > 0));
            this.moveRightMenuItem.Enabled     = (clickedOnPicture && (this.indexClicked < this.pictureBoxList.Count - 1));
            this.deletePictureMenuItem.Enabled =  clickedOnPicture;
            this.addPictureMenuItem.Enabled    = !clickedOnPicture;

            this.contextMenuStrip.Show(this.pictureBoxPanel, point);
        }

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

댓글을 달아 주세요