퇴근5분전

 버튼 -> 누르고 있으면 이벤트 발생 ~

 

클릭과는 다른 동작을 함.

 

 

# 차이는 다음과 같음.

 

  void jsfW_Button1_MouseUp(object sender, MouseEventArgs e)
        {
            jsfW_Button1.ForeColor = Color.Black;
        }

  

// 버튼을 누른체 1초후 발생 -> Click Event 는 건너뜀.

  void jsfW_Button1_MousePressing(object sender, EventArgs e)
        {     

             jsfW_Button1.ForeColor = Color.Red;
             label1.Text += "Press" + Environment.NewLine;       
        }

   void jsfW_Button1_MouseDown(object sender, MouseEventArgs e)
        {
            jsfW_Button1.ForeColor = Color.Black;
        }

  

  void jsfW_Button1_Click(object sender, EventArgs e)
        {
            label1.Text += "클릭"+ Environment.NewLine;
        }

 

 

#  소스

 

  public class JSFW_Button : Button
    {
        bool isMDown = false;
        TimeSpan PressingTimeSpan = TimeSpan.MinValue;
        bool IsPressed = false;

        protected override void OnMouseDown(MouseEventArgs mevent)
        {
            base.OnMouseDown(mevent);
            if (mevent.Button == System.Windows.Forms.MouseButtons.Left)
            {
                isMDown = true;
                PressingTimeSpan = new TimeSpan(DateTime.Now.Ticks);
                Action pressingAction = PressChecking;
                pressingAction.BeginInvoke(ir => pressingAction.EndInvoke(ir), pressingAction);
            }
        }

        void PressChecking()
        {
            IsPressed = false;
            while (isMDown)
            {
                Debug.Write(DateTime.Now.Subtract(PressingTimeSpan).Millisecond + Environment.NewLine);
                if( TimeSpan.FromTicks(  DateTime.Now.Ticks ).Subtract(PressingTimeSpan).TotalMilliseconds > 1000 )
                {
                    Invoke( new Action<EventArgs>( OnMousePressing), EventArgs.Empty);
                    break; // Thread 종료!
                }
                System.Threading.Thread.Sleep(200);
            }
        }

        protected override void OnClick(EventArgs e)
        {
            if (IsPressed == false)
            {
                base.OnClick(e);
            }
            IsPressed = false;
        }

        protected override void OnMouseUp(MouseEventArgs mevent)
        {
            isMDown = false;
            base.OnMouseUp(mevent);
        }
        
        public event EventHandler MousePressing = null;

        protected void OnMousePressing(EventArgs e)
        {
            if (MousePressing != null)
            {
                IsPressed = true;
                MousePressing(this, e);
            }
        }
    }

'# 4) .Net ( Vs 2010 ) > C#' 카테고리의 다른 글

GDI+] 이미지 회전!  (0) 2012.08.13
2010 WinForm ] 컨트롤 리사이징 예제...  (0) 2012.07.20
.NET ] 멀티 랭귀지 지원 ...  (0) 2012.03.06
ArrayList.Sort 하기...  (4) 2011.07.29
[IPC] Event 추가 ~~  (0) 2011.05.14