Busan IT/제어 UI(C#)

슬라이더 박스

newind2000 2015. 6. 15. 12:33

================================ Outline ====================================

슬라이더 박스

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


슬라이더박스[1].pdf


/*** 코드 ***/

 

<form1.cs>


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace mook_FormSlidMag
{
    
    public partial class Form1 : Form
    {
        public static Point FormPt;         //Form1 위치
        public static Point FormPoint01;    //Form2 위치
        public static bool flag01;         //Form2가 열려 있는지를 판단
        public static bool flag02;         //Form3이 열려 있는지를 판단
        public static bool flag03;         //Form3이 붙어 있는지를 판단

        Form2 frm2 = new Form2();
        Form3 frm3 = new Form3();



        public Form1()
        {
            InitializeComponent();
        }

        private void btnShow_Click(object sender, EventArgs e)
        {
            if (flag01 == false//폼2가 닫혀 있을 때 실행
            {
                this.btnShow.Text = "슬라이드 닫힘";
                FormPoint01.X = this.Location.X;
                FormPoint01.Y = this.Location.Y + 30;
                this.TopMost = true;
                frm2.Visible = true;
                frm2.SlidingForm();
            }
            else //폼 2가 열려 있을 때 실행
            {
                this.btnShow.Text = "슬라이드 열림";
                FormPoint01.X = this.Location.X + 300;
                FormPoint01.Y = this.Location.Y + 30;
                frm2.SlidingForm();
            }
        }


        private void btnMag_Click(object sender, EventArgs e)
        {
            if(flag02 == false//폼3가 닫혀 있을 때 실행
            {
                this.btnMag.Text = "폼 붙이기 닫기";
                frm3.Show();
                flag02 = true;
                flag03 = true;
                frm3.Location = new Point(this.Location.X, this.Location.Y + 310);
            }
            else //폼3가 열려 있을 때 실행  
            {
                this.btnMag.Text = "폼 붙이기 열기";
                frm3.Hide();
                flag02 = false;
                flag03 = false;
            }
        }

        private void Form1_LocationChanged(object sender, EventArgs e)
        {
            FormPt = this.Location;
            if (flag01 == true)
            {
                frm2.Left = this.Left + 300;
                frm2.Top = this.Top + 30;
            }
            else
            {
                frm2.Left = this.Left;
                frm2.Top = this.Top + 30;
            }
            if (flag02 == true && flag03 == true)
            {
                frm3.Location = new Point(this.Location.X, this.Location.Y + 310);
            }
        }
    }
}


<form2.cs>


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace mook_FormSlidMag
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }


        private void Form2_Load(object sender, EventArgs e)
        {
            SlidingForm();
        }
        public void SlidingForm()
        {
            Location = Form1.FormPoint01;
            timer.Start();
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            if (Form1.flag01 == false)
            {
                Location = new Point(Location.X + 10, Location.Y);
                if(Location.X == Form1.FormPoint01.X + 300)
                {
                    timer.Stop();
                    Form1.flag01 = true;
                }
            }
            else
            {
                Location = new Point(Location.X - 10, Location.Y);
                if(Location.X == Form1.FormPoint01.X - 300)
                {
                    timer.Stop();
                    Form1.flag01 = false;
                }
            }
        }

    }
}


<Form3.cs>

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace mook_FormSlidMag
{
    public partial class Form3 : Form
    {
        Point ptMouseCurrentPos;    //마우스 클릭 좌표 지정
        Point ptMouseNewPos;        //이동시 마우스 좌표
        Point ptFormCurrentPos;     //폼 위치 좌표 지정
        Point ptFormNewPos;         //이동시 폼 위치 좌표
        bool bFormMouseDown = false;
        public Form3()
        {
            InitializeComponent();
        }

        private void Form3_Load(object sender, EventArgs e)
        {

        }

        private void Form3_MouseDown(object sender, MouseEventArgs e)
        {
            if(e.Button == MouseButtons.Left)
            {
                bFormMouseDown = true;
                ptMouseCurrentPos = Control.MousePosition; //마우스 클릭 좌표
                ptFormCurrentPos = this.Location;
            }
        }

        private void Form3_MouseMove(object sender, MouseEventArgs e)
        {
            if(bFormMouseDown == true)
            {
                ptMouseNewPos = Control.MousePosition;
                ptFormNewPos.X = ptMouseNewPos.X -
                ptMouseCurrentPos.X + ptFormCurrentPos.X;   //마우스 이동 시 가로 좌표
                ptFormNewPos.Y = ptMouseNewPos.Y -
                ptMouseCurrentPos.Y + ptFormCurrentPos.Y;    //마우스 이동 시 세로 좌표
                this.Location = ptFormNewPos;
                ptFormCurrentPos = ptFormNewPos;
                ptMouseCurrentPos = ptMouseNewPos;

            }
        }

        private void Form3_MouseUp(object sender, MouseEventArgs e)
        {
            if(e.Button == MouseButtons.Left)
            {
                bFormMouseDown = false;
                if(this.Location.X <= Form1.FormPt.X + 30 && this.Location.X >= Form1.FormPt.X + 30)
                {
                    if(this.Location.Y <= Form1.FormPt.Y + 340 && this.Location.Y >= Form1.FormPt.Y + 280)
                    {
                        this.Location = new Point(Form1.FormPt.X, Form1.FormPt.Y + 310);
                        Form1.flag03 = true;
                    }
                    else
                    {
                        Form1.flag03 = false;
                    }                    
                }
                else
                {
                    Form1.flag03 = false;
                }
            }
        }
    }
}


결과물 

 

 

 

 

반응형