如何在Windows窗体中找到鼠标移动速度?

时间:2016-03-21 07:20:45

标签: c# mouseevent windows-forms-designer onmousemove

如何在鼠标事件中的Windows窗体控件中找到鼠标移动速度。基于鼠标移动速度,我需要移动控件。就像用鼠标左键移动鼠标一样,基于鼠标速度我需要移动我的控件吗?

2 个答案:

答案 0 :(得分:0)

我会这样做:
第一次移动鼠标时启动计时器并保存起始位置。 如果它继续移动,看看移动是否在一条直线上,如果不是,则停止计时器然后应用公式来计算行进的总距离并将其除以所花费的时间。 如果有多条“直线”,则可以使用平均值(sum / cant)。

答案 1 :(得分:0)

也许微软的这个代码示例可以帮助您:

using System;
using System.Windows.Forms;
using System.Reflection;

namespace SystemInfoBrowser
{
    public partial class SystemInfoBrowserForm : System.Windows.Forms.Form
    {
        private System.Windows.Forms.ListBox listBox1;
        private System.Windows.Forms.TextBox textBox1;

    public SystemInfoBrowserForm()
    {
        this.SuspendLayout();
        InitForm();

        // Add each property of the SystemInformation class to the list box.
        Type t = typeof(System.Windows.Forms.SystemInformation);
        PropertyInfo[] pi = t.GetProperties();
        for (int i = 0; i < pi.Length; i++)
            listBox1.Items.Add(pi[i].Name);
        textBox1.Text = "The SystemInformation class has " + pi.Length.ToString() + " properties.\r\n";

        // Configure the list item selected handler for the list box to invoke a 
        // method that displays the value of each property.
        listBox1.SelectedIndexChanged += new EventHandler(listBox1_SelectedIndexChanged);
        this.ResumeLayout(false);
    }

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        // Return if no list item is selected.
        if (listBox1.SelectedIndex == -1) return;
        // Get the property name from the list item.
        string propname = listBox1.Text;

        if (propname == "PowerStatus")
        {
            // Cycle and display the values of each property of the PowerStatus property.
            textBox1.Text += "\r\nThe value of the PowerStatus property is:";
            Type t = typeof(System.Windows.Forms.PowerStatus);
            PropertyInfo[] pi = t.GetProperties();
            for (int i = 0; i < pi.Length; i++)
            {
                object propval = pi[i].GetValue(SystemInformation.PowerStatus, null);
                textBox1.Text += "\r\n    PowerStatus." + pi[i].Name + " is: " + propval.ToString();
            }
        }
        else
        {
            // Display the value of the selected property of the SystemInformation type.
            Type t = typeof(System.Windows.Forms.SystemInformation);
            PropertyInfo[] pi = t.GetProperties();
            PropertyInfo prop = null;
            for (int i = 0; i < pi.Length; i++)
                if (pi[i].Name == propname)
                {
                    prop = pi[i];
                    break;
                }
            object propval = prop.GetValue(null, null);
            textBox1.Text += "\r\nThe value of the " + propname + " property is: " + propval.ToString();
        }
    }

    private void InitForm()
    {
        // Initialize the form settings
        this.listBox1 = new System.Windows.Forms.ListBox();
        this.textBox1 = new System.Windows.Forms.TextBox();
        this.listBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
            | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right)));
        this.listBox1.Location = new System.Drawing.Point(8, 16);
        this.listBox1.Size = new System.Drawing.Size(172, 496);
        this.listBox1.TabIndex = 0;
        this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
            | System.Windows.Forms.AnchorStyles.Right)));
        this.textBox1.Location = new System.Drawing.Point(188, 16);
        this.textBox1.Multiline = true;
        this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
        this.textBox1.Size = new System.Drawing.Size(420, 496);
        this.textBox1.TabIndex = 1;
        this.ClientSize = new System.Drawing.Size(616, 525);
        this.Controls.Add(this.textBox1);
        this.Controls.Add(this.listBox1);
        this.Text = "Select a SystemInformation property to get the value of";
    }
}

}

它来自Microsoft并使用SystemInformation类向您显示一些属性,它还会显示MouseSpeed,它从1到20到达,默认情况下应设置为10。您只需要为此属性提取代码部分,就应该进行设置。

enter image description here