본문 바로가기

Programing (프로그래밍)/WPF & C# (C Sharp)

[자작] WPF & C# - TouchTotalCount ( 터치 수 확인 프로그램 )

728x90
반응형

CK_TouchTotalCount.zip
다운로드

 

-프로그램명 : TouchTotalCount
-언어 : WPF
-목적 : 터치 디바이스에서 터치 가능여부와 수를 확인하는데 있다.
-기능 : 터치 카운트, 가능수 무제한

<Window x:Name="win1" x:Class="TouchCount.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp10"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800" TouchMove="win1_TouchMove" TouchLeave="win1_TouchLeave">
    <Grid x:Name="grid01" TouchDown="grid01_TouchDown" Background="#FFD8D8D8">
        <Label x:Name="lblTotal" Content="Label" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" FontWeight="Bold"/>
        <Label x:Name="lbl" Content="Label" HorizontalAlignment="Left" Margin="10,41,0,0" VerticalAlignment="Top" FontWeight="Bold"/>
        <Canvas x:Name="canvas01"/>
    </Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Drawing;
using System.Windows.Media.Animation;

namespace TouchCount
{
    /// <summary>
    /// MainWindow.xaml에 대한 상호 작용 논리
    /// </summary>
    /// 

    public partial class MainWindow : Window
    {

        Dictionary<int, Ellipse> ellipses;   // Ellipse ID 부여

        public MainWindow()
        {
            InitializeComponent();

            // 시작 시 자식 수 카운트
            baseChildrenNum = canvas01.Children.Count;

            // 터치 카운트
            touchCount();

            ellipses = new Dictionary<int, Ellipse>();
        }
        

        int numTotaltouch;   // 총 touch
        int numEll;   // ELL 수량
        int baseChildrenNum;   // 기본 숫자

        // label 에 터치 카운트 하기
        void touchCount()
        {
            numEll = canvas01.Children.Count - baseChildrenNum;
            lbl.Content = "Touch Count : " + (numEll);
            if (numTotaltouch < numEll) numTotaltouch = numEll;
            lblTotal.Content = "Touch Total Count  : " + (numTotaltouch);
        }

        // 컬러값
        private System.Windows.Media.Color[] circleColors =    // Secondary colors
        {
            Colors.Red,
            Colors.Black,
            Colors.LawnGreen,
            Colors.Blue,
            Colors.Cyan,
            Colors.Magenta,
            Colors.Blue,
            Colors.BlueViolet,
            Colors.Brown,
            Colors.BurlyWood,
            Colors.Chartreuse,
            Colors.Coral,
            Colors.Tomato,
            Colors.Teal,
            Colors.SteelBlue,
            Colors.SpringGreen,
            Colors.SlateGray,
            Colors.SlateBlue,
            Colors.Purple,
            Colors.GreenYellow,
            Colors.Green,
            Colors.Firebrick,
            Colors.DarkOrchid,
            Colors.Crimson,
            Colors.Chartreuse,
            Colors.DarkViolet,
            Colors.DeepPink,
            Colors.Violet,
            Colors.YellowGreen,
            Colors.Yellow
        };

        private void grid01_TouchDown(object sender, TouchEventArgs e)
        {
            Ellipse ellipse = new Ellipse();

            int id = e.TouchDevice.Id;
            TouchPoint loc = e.GetTouchPoint(canvas01);

            // Ellipse Shape 동일 ID 가 남아 있으면 기존것 삭제
            Ellipse oldcircle;
            if (ellipses.TryGetValue(id, out oldcircle))
            {
                ellipses.Remove(id);
            }

            // Ellipse 생성
            Ellipse circle = new Ellipse();
            circle.Width = 50;   // 사이즈
            circle.Height = 50;

            int coloridx = ellipses.Count;   // 컬러 매칭 설정
            if (coloridx >= circleColors.Length) coloridx = 0;
            circle.Fill = new SolidColorBrush(circleColors[coloridx]);
            circle.Fill = new SolidColorBrush(Colors.LightSlateGray);   // 한색상으로 변경

            canvas01.Children.Add(circle);   // 적용

            circle.SetValue(Canvas.TopProperty, loc.Position.Y - (circle.ActualHeight / 2));
            circle.SetValue(Canvas.LeftProperty, loc.Position.X - (circle.ActualWidth / 2));

            // 더블애니메이션 (페이드 인 페이드 아웃)
            DoubleAnimation dba1 = new DoubleAnimation();  // 애니메이션 생성
            dba1.From = 50;   // start 값
            dba1.To = 55;   // end 값
            dba1.Duration = new Duration(TimeSpan.FromSeconds(0.2));  // 1.5초 동안 실행
            dba1.AutoReverse = true;  // 되돌이기
            dba1.RepeatBehavior = RepeatBehavior.Forever;  // 무한 반복

            // 애니메이션 실행
            circle.BeginAnimation(WidthProperty, dba1);   // 변경할 속성값, 대상애니매이션
            circle.BeginAnimation(HeightProperty, dba1);   // 변경할 속성값, 대상애니매이션


            // Ellipse를 딕셔너리에 입력
            ellipses.Add(id, circle);

            touchCount();
        }

        private void win1_TouchMove(object sender, TouchEventArgs e)
        {
            int id = e.TouchDevice.Id;
            TouchPoint loc = e.GetTouchPoint(canvas01);

            // circle
            Ellipse circle;
            if (ellipses.TryGetValue(id, out circle))
            {
                circle.SetValue(Canvas.TopProperty, loc.Position.Y - (circle.ActualHeight/2));
                circle.SetValue(Canvas.LeftProperty, loc.Position.X - (circle.ActualWidth/2));
            }
        }

        // 터치 제거시
        private void win1_TouchLeave(object sender, TouchEventArgs e)
        {
            int id = e.TouchDevice.Id;

            Ellipse circle;
            if (ellipses.TryGetValue(id, out circle))
            {
                ellipses.Remove(id);
                canvas01.Children.Remove(circle);
            }

            // 터치 카운트
            touchCount();

        }
    }
}
728x90
반응형