3_c#/Winform

3 C#) Timer를 이용한 Sliding Menu 만들기

Mi:sAng 2023. 7. 27. 23:38

[Timer]
    :windows forms timer는 일정한 간격마다 이벤트를 발생시키는 구성 요소이다.
     간격의 길이는 값이 밀리초 단위인 Interval 속성에 의해 정의된다.
     Tick이벤트가 Interval에 설정한 간격마다 발생한다.
     Timer의 주요 메서드는 start 및 stop이고, 이는 타이머를 켜고 끕니다.


[사용법]
    1.폼에 Timer를 추가
    2. 타이머에대한 interval 설정
        이 속성은 프로시저가 다시 실행되기까지 남은 시간을 결정한다.
    3. Tick 이벤트에 적절한 코드를 작성한다
    4.Enabled 속성을 true로 설정하여 타이머 시작한다. 
    5.적절한 시간에 Enabled 속성을 false로 설정하여 프로시저가 
       다시 실행되지 않도록 중지합니다.
        Interval을 0으로 해도 타이머가 중지되지 않는다

 

[예제]

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;
// 레퍼런스 : https://luckygg.tistory.com/340
// panel property ::  Name:panelSideMenu, Dock:Left, Size(width):200
// button :: Name:button1, Dock:Top, Size: 200-50, FlatStyle:Flat, Text:버튼1
// checkBox :: Name:checkBoxHide, DocK:Bottom, Appearance:Buttom, FlatStyle:Flat, Text:<
// timer :: Name:timerSliding, Interval:10


namespace ExampleSlidingMenu
{
    public partial class Form1 : Form
    {   //슬라이딩 메뉴의 최대, 최소 폭 크기
        const int MAX_SLIDING_WIDTH = 200;
        const int MIN_SLIDING_WIDTH = 50;

        //슬라이딩 메뉴가 보이거나 접히는 속도 설정 : 몇 크기씩 작게 할 것이냐
        const int STEP_SLIDING = 10;
        
        //최초 슬라이딩 메뉴 크기이자. 현재 슬라이딩 메뉴의 크기를 나타내는 변수 
        int _posSliding = 200;



        public Form1()
        {
            InitializeComponent();
        }

        private void checkBoxHide_CheckedChanged(object sender, EventArgs e)// checkbox가 눌렸을 때
        {
            if (checkBoxHide.Checked == true)
            { // 체크 박스 메뉴가 눌린 경우 
                button1.Text = "1";
                button2.Text = "2";
                checkBoxHide.Text = ">";


            }
            else {
                button1.Text = "버튼1";
                button2.Text = "버튼2";
                checkBoxHide.Text = "<";
            }
            timerSliding.Start();
        }

        private void timerSliding_Tick(object sender, EventArgs e)
            //Timer Class 는 지정한 시간 간격마다 Tick 이벤트를 발생시키는 Class 이다 
            //Timer는 특정 시간 간격ㅁ다ㅏ 이벤트를 발생시키기 때문에 그 이벤트에 원하는 method를 넣어두면 일정 간격마다 원하는 행동을 하도록 만들 수 있다
        {
            if (checkBoxHide.Checked == true)
            {
                _posSliding -= STEP_SLIDING; // 크기를 줄이는 과정 
                if (_posSliding <= MIN_SLIDING_WIDTH) // 슬라이딩 메뉴의 최소 크기 보다 작아지면 
                {
                    timerSliding.Stop();

                }
            }
            else {
                _posSliding += STEP_SLIDING; //크기를 늘리는 과정
                if (_posSliding >=MAX_SLIDING_WIDTH) { // 슬라이딩 메뉴의 최대 크기보다 커지면
                    timerSliding.Stop();
                
                }
            
            }
            panelSideMenu.Width = _posSliding; //늘리거나 줄인 크기를  panelSideMenu의 폭으로 넣는 과정 

        }
    }
}

 

 

클릭하지 않고 커서만 올려두었을 때 MouseOverBackColor 색이 구현됨

반대로 클릭을 하면 MouseDownBackColor 색이 구현된다

 

참조
 https://learn.microsoft.com/ko-kr/dotnet/desktop/winforms/controls/run-procedures-at-set-intervals-with-wf-timer-component?view=netframeworkdesktop-4.8 

https://luckygg.tistory.com/340