28 Mayıs 2018 Pazartesi

Uzun Süreli Fare Tıklaması ile Farklı bir Event Tetiklemek

Uzun süreli fare tıklaması ile farklı bir event tetiklemek için ne yapmak gerekir?

Bir ihtiyaçtan dolayı dokunmatik kullanılan bir ortamda fare klik (yani dokunma) işleminin daha uzun süre yapılarak farklı bir event'i tetiklemesini istedik. Haliyle önce bu konuda yapılmış bir çalışma var mı diye bir bakınıyor insan; evet bu konuda epey yazılıp çizilmiş fakat açıkçası ben "tamam bunu alıp kullanırım" diyebileceğim bir koda rastlamadım.

Örnekler ya Timer ile, ya Task ile veya Thread ile yapılmış. O kadar kastırmaya ne gerek var. Bir yazılımı multitask ortamına sokmadan işin içinden çıkabiliyorsanız bu bir başarıdır bence. Bu konuda meşhur iki resim vardır hani, TEORİDE OLAN ve GERÇEKTE OLAN diye. Şuraya bırakalım:

TEORİDE OLAN (multi threading)

GERÇEKTE OLAN (thread conflict)

İşte bundan dolayı basit işlemler için mümkün mertebe çoklu işlem kodlamasına girmemek mantıklıdır.
Zaten biraz kafa yorunca basit bir çözüm de geliştirdim. Faydalanılması için Internet'teki diğer soru başlıklarının altına da yazdım. Buraya da ekleyerek kalıcı hale getirmek istedim:


//https://ersinkecis.blogspot.com.tr/
using System.Diagnostics;
using System.Windows.Forms;

namespace LongClick
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        #region MouseDown and buttonUp Events
        private DateTime sw;
        private bool buttonUp = false;
        private const int holdButtonDuration = 2000; //milliseconds
        private void btnTest_MouseDown(object sender, MouseEventArgs e)
        {
            //buttonUp = false;
            //Stopwatch sw = new Stopwatch(); //yonetime ihtiyac duymayan task islemi!
            //sw.Start();
            //while (e.Button == MouseButtons.Left && e.Clicks == 1 && (buttonUp == false && sw.ElapsedMilliseconds < holdButtonDuration))
            //    Application.DoEvents();
            //if (sw.ElapsedMilliseconds < holdButtonDuration)
            //    btnTest_ShortClick(sender, e);
            //else
            //    btnTest_LongClick(sender, e);

            buttonUp = false;
            sw = DateTime.Now; //tasksiz
            while (e.Button == MouseButtons.Left && e.Clicks == 1 && (buttonUp == false && (DateTime.Now - sw).TotalMilliseconds < holdButtonDuration))
                Application.DoEvents();
            if ((DateTime.Now - sw).TotalMilliseconds < holdButtonDuration)
                Test_ShortClick();
            else
                Test_LongClick();
        }
        private void btnTest_buttonUp(object sender, MouseEventArgs e)
        {
            buttonUp = true;
        }
        #endregion

        #region New Click Events
        private void btnTest_ShortClick(object sender, MouseEventArgs e)
        {
            chkShortClick.Checked = !chkShortClick.Checked;
        }
        private void btnTest_LongClick(object sender, MouseEventArgs e)
        {
            chkLongClick.Checked = !chkLongClick.Checked;
        }
        #endregion
    }
}


Hiç yorum yok:

Yorum Gönder