C#记录点击之间的时间

时间:2014-10-20 05:24:28

标签: c#

我在C#中有代码,用于在运行时记录时间和鼠标光标坐标。我想要它做的是在点击一个正方形(nextTaskBttn_Click)和一个圆圈之间花时间。 (TestForm_MouseClick)所以我希望它记录时间然后减去以获得点击之间的时间。我不确定这样做的最佳或正确方法是什么。有人有什么建议吗?

这是代码:

namespace FittsLawExperiment
{
    public partial class TestForm : Form
    {
        private Random r = new Random();
        private int Count = 0;
        private String fileData = String.Empty;
        private List<SizeDistanceDirectionClass> myList = new List<SizeDistanceDirectionClass>();
        Point globalPoint = new Point();
        int globalDiameter = 0;
        //int globalPointX = 0; test
        //int globalPointY = 0;

    public TestForm()
    {
        InitializeComponent();

        FormBorderStyle = FormBorderStyle.None;
        WindowState = FormWindowState.Normal;//change to Maximize

        //variables for size, distance and direction
        int s1 = 30;
        int s2 = 50;
        int s3 = 70;
        int d1 = 50;
        int d2 = 100;
        int l = 0;
        int r = 1;
        //set into groups of three into temps
        SizeDistanceDirectionClass temp1 = new SizeDistanceDirectionClass(s1, d1, l);
        SizeDistanceDirectionClass temp2 = new SizeDistanceDirectionClass(s2, d1, l);
        SizeDistanceDirectionClass temp3 = new SizeDistanceDirectionClass(s3, d1, l);
        SizeDistanceDirectionClass temp4 = new SizeDistanceDirectionClass(s1, d2, l);
        SizeDistanceDirectionClass temp5 = new SizeDistanceDirectionClass(s2, d2, l);
        SizeDistanceDirectionClass temp6 = new SizeDistanceDirectionClass(s3, d2, l);
        SizeDistanceDirectionClass temp7 = new SizeDistanceDirectionClass(s1, d1, r);
        SizeDistanceDirectionClass temp8 = new SizeDistanceDirectionClass(s2, d1, r);
        SizeDistanceDirectionClass temp9 = new SizeDistanceDirectionClass(s3, d1, r);
        SizeDistanceDirectionClass temp10 = new SizeDistanceDirectionClass(s1, d2, r);
        SizeDistanceDirectionClass temp11 = new SizeDistanceDirectionClass(s2, d2, r);
        SizeDistanceDirectionClass temp12 = new SizeDistanceDirectionClass(s3, d2, r);

        myList.Add(temp1);//added to myList
        myList.Add(temp2);
        myList.Add(temp3);
        myList.Add(temp4);
        myList.Add(temp5);
        myList.Add(temp6);
        myList.Add(temp7);
        myList.Add(temp8);
        myList.Add(temp9);
        myList.Add(temp10);
        myList.Add(temp11);
        myList.Add(temp12);
    }

    /// <summary>
    /// To Draw Circle 
    /// </summary>
    private void DrawCircle(Point position, int diameter)
    {
        globalPoint = position;
        //globalPointX = position.X; test
        //globalPointY = position.Y;
        globalDiameter = diameter;

        System.Drawing.Graphics graphics = this.CreateGraphics();
        System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(position.X, position.Y, diameter, diameter);
        graphics.FillEllipse(Brushes.Blue, rectangle); 
    }

    /// <summary>
    /// To Draw A circle for a task
    /// This button needs to clicked on to generate each circle
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void nextTaskBttn_Click(object sender, EventArgs e)
    {
        //int display = Count + 1;
        //label1.Text = "Times clicked: " + display.ToString();
        nextTaskBttn.Visible = false;
        int groupIndex = r.Next(12);

        while (myList[groupIndex].Count == 10)
        {
            groupIndex = r.Next(12);
        }

        //Get the button location 
        Point buttonLoc = nextTaskBttn.Location;

        if (myList[groupIndex].Direction == 1) //If r = 0 -> left
        {
            DrawCircle(new Point(buttonLoc.X - myList[groupIndex].Distance, buttonLoc.Y), myList[groupIndex].Size);//right
        }
        else
        {
            DrawCircle(new Point(buttonLoc.X + myList[groupIndex].Distance, buttonLoc.Y), myList[groupIndex].Size);//left
        }

        myList[groupIndex].Count++;

        //Listen to clicks in Form
        //MouseClick += TestForm_MouseClick;
        nextTaskBttn.Visible = false;
        //DateTime dt1 = DateTime.Now.ToString("h:mm:ss");//trying to record one time here
        recordClick();

    }

    void TestForm_MouseClick(object sender, MouseEventArgs e)
    {
        if (Validateposition())
        {
            //Hide circles
            this.Invalidate();
            //Show nextTask Button
            nextTaskBttn.Visible = true;
            //String circleTime = DateTime.Now.ToString("h:mm:ss");//I'm trying to record one time here
            recordClick();

            int display = Count + 1;
            label1.Text = "Times clicked: " + display.ToString();

            Count++;
            if (Count == 120)
            {
                nextTaskBttn.Visible = false;
                MessageBox.Show("You have completed the test. Thank you!");
                //this.Close();
            }
        }
    }

    private Boolean Validateposition()// if Cursor.Position > globalPoint + globalDiameter/2
    {
        //if (Cursor.Position > globalPoint + (globalDiameter / 2))
        //{
        //    return false;
        //}
        return true;
    }

    private void recordClick()
    {
        //TimeSpan span = circleTime - boxTime; //I want to get the time between clicks. Not sure a good way to do this.
        String currentTime = DateTime.Now.ToString("h:mm:ss tt");
        String currentPosition = Cursor.Position.ToString();
        fileData = fileData + currentPosition + " " + currentTime + Environment.NewLine;
    }

1 个答案:

答案 0 :(得分:2)

在.NET中,已经存在一个完全相同的类:StopWatch