亲宝软件园·资讯

展开

基于C#实现鼠标设置功能

芝麻粒儿 人气:0

实践过程

效果

代码

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

    [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SwapMouseButton")]
    public extern static int SwapMouseButton(int bSwap);
    [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SystemParametersInfo")]
    public static extern bool SystemParametersInfo(uint uiAction, uint uiParam, ref int pvParam, uint fWinIni);
    [System.Runtime.InteropServices.DllImport("user32.dll")]
    public extern static int GetSystemMetrics(int nIndes);
    [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SetDoubleClickTime")]
    public extern static int SetDoubleClickTime(int wCount);
    [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "GetDoubleClickTime")]
    public extern static int GetDoubleClickTime();

    const int SM_SWAPBUTTON = 23;//如左右鼠标键已经交换,则为TRUE
    const int SPI_SETMOUSE = 4;//设置鼠标的移动速度
    const int SPI_GETMOUSESPEED = 112;//检索当前鼠标速度
    const uint SPIF_UPDATEINIFILE = 0x0001;//更新win.ini和(或)注册表中的用户配置文件
    const uint SPIF_SENDWININICHANGE = 0x0002;//该消息告诉应用程序已经改变了用户配置设置
    int aMouseinfo=0;
    

    private void Form1_Load(object sender, EventArgs e)
    {
        if (GetSystemMetrics(SM_SWAPBUTTON) == 0)//如果鼠标的左右键没有切换
        {
            pictureBox1.Image = null;//清空图片
            pictureBox1.Image = Properties.Resources.鼠标左键;//显示图片
            checkBox1.Checked = false;//设置复选框为不选中状态
        }
        else//如果鼠标左右切换
        {
            pictureBox1.Image = null;//清空图片
            pictureBox1.Image = Properties.Resources.鼠标右键;//显示图片
            checkBox1.Checked = true;//设置复选框为选中状态
        }
        int tem_n = 0;
        switch (Convert.ToInt32(DoubleClickTime_Get()))//获取鼠标的双击速度
        {
            case 900: tem_n = 0; break;
            case 830: tem_n = 1; break;
            case 760: tem_n = 2; break;
            case 690: tem_n = 3; break;
            case 620: tem_n = 4; break;
            case 550: tem_n = 5; break;
            case 480: tem_n = 6; break;
            case 410: tem_n = 7; break;
            case 340: tem_n = 8; break;
            case 270: tem_n = 9; break;
            case 200: tem_n = 10; break;
        }
        trackBar1.Value = tem_n;//显示鼠标的双击速度
        SystemParametersInfo(SPI_GETMOUSESPEED, 0, ref aMouseinfo, 0);//获取当前鼠标的移动速度
        trackBar2.Value = aMouseinfo;//显示当前鼠标的移动速度
    }

    public string DoubleClickTime_Get()
    {
        return GetDoubleClickTime().ToString();//获取鼠标的双击速度
    }

    public void DoubleClickTime_Set(int MouseDoubleClickTime)
    {
        SetDoubleClickTime(MouseDoubleClickTime);//设置获取鼠标的双击速度
    }

    private void checkBox1_MouseUp(object sender, MouseEventArgs e)
    {
        if (((CheckBox)sender).Checked == true)//如果为选中状态
        {
            pictureBox1.Image = null;//清空图片
            pictureBox1.Image = Properties.Resources.鼠标右键;//显示图片
            SwapMouseButton(1);//切换鼠标左右键
        }
        else//如果不为选中状态
        {
            if (((CheckBox)sender).Checked == false)
            {
                pictureBox1.Image = null;//清空图片
                pictureBox1.Image = Properties.Resources.鼠标左键;//显示图片
                SwapMouseButton(0);//恢复,设置左键为主键
            }
        }
    }

    private void trackBar1_Scroll(object sender, EventArgs e)
    {
        int tem_n = 0;
        switch (((TrackBar)sender).Value)//记录鼠标的双击速度
        {
            case 0: tem_n = 900; break;
            case 1: tem_n = 830; break;
            case 2: tem_n = 760; break;
            case 3: tem_n = 690; break;
            case 4: tem_n = 620; break;
            case 5: tem_n = 550; break;
            case 6: tem_n = 480; break;
            case 7: tem_n = 410; break;
            case 8: tem_n = 340; break;
            case 9: tem_n = 270; break;
            case 10: tem_n = 200; break;
        }
        DoubleClickTime_Set(tem_n);//设置鼠标的双击的速度
    }

    private void trackBar2_Scroll(object sender, EventArgs e)
    {
        aMouseinfo = trackBar2.Value;//记录鼠标的移动速度
        SystemParametersInfo(SPI_SETMOUSE, 0, ref aMouseinfo, SPIF_UPDATEINIFILE);//设置鼠标的移动速度
    }
}
partial class Form1
{
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows 窗体设计器生成的代码

    /// <summary>
    /// 设计器支持所需的方法 - 不要
    /// 使用代码编辑器修改此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
        this.groupBox1 = new System.Windows.Forms.GroupBox();
        this.textBox1 = new System.Windows.Forms.TextBox();
        this.checkBox1 = new System.Windows.Forms.CheckBox();
        this.groupBox2 = new System.Windows.Forms.GroupBox();
        this.label3 = new System.Windows.Forms.Label();
        this.label2 = new System.Windows.Forms.Label();
        this.label1 = new System.Windows.Forms.Label();
        this.trackBar1 = new System.Windows.Forms.TrackBar();
        this.textBox2 = new System.Windows.Forms.TextBox();
        this.groupBox3 = new System.Windows.Forms.GroupBox();
        this.trackBar2 = new System.Windows.Forms.TrackBar();
        this.pictureBox3 = new System.Windows.Forms.PictureBox();
        this.pictureBox2 = new System.Windows.Forms.PictureBox();
        this.pictureBox1 = new System.Windows.Forms.PictureBox();
        this.label4 = new System.Windows.Forms.Label();
        this.label5 = new System.Windows.Forms.Label();
        this.label6 = new System.Windows.Forms.Label();
        this.groupBox1.SuspendLayout();
        this.groupBox2.SuspendLayout();
        ((System.ComponentModel.ISupportInitialize)(this.trackBar1)).BeginInit();
        this.groupBox3.SuspendLayout();
        ((System.ComponentModel.ISupportInitialize)(this.trackBar2)).BeginInit();
        ((System.ComponentModel.ISupportInitialize)(this.pictureBox3)).BeginInit();
        ((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).BeginInit();
        ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
        this.SuspendLayout();
        // 
        // groupBox1
        // 
        this.groupBox1.Controls.Add(this.pictureBox1);
        this.groupBox1.Controls.Add(this.textBox1);
        this.groupBox1.Controls.Add(this.checkBox1);
        this.groupBox1.Location = new System.Drawing.Point(12, 12);
        this.groupBox1.Name = "groupBox1";
        this.groupBox1.Size = new System.Drawing.Size(376, 131);
        this.groupBox1.TabIndex = 0;
        this.groupBox1.TabStop = false;
        this.groupBox1.Text = "鼠标键配置";
        // 
        // textBox1
        // 
        this.textBox1.BackColor = System.Drawing.Color.WhiteSmoke;
        this.textBox1.BorderStyle = System.Windows.Forms.BorderStyle.None;
        this.textBox1.Location = new System.Drawing.Point(15, 51);
        this.textBox1.Multiline = true;
        this.textBox1.Name = "textBox1";
        this.textBox1.ReadOnly = true;
        this.textBox1.Size = new System.Drawing.Size(230, 28);
        this.textBox1.TabIndex = 3;
        this.textBox1.Text = "选择些复选框来将右按钮设成用于主要功能如选择和拖放之用";
        // 
        // checkBox1
        // 
        this.checkBox1.AutoSize = true;
        this.checkBox1.Location = new System.Drawing.Point(15, 29);
        this.checkBox1.Name = "checkBox1";
        this.checkBox1.Size = new System.Drawing.Size(144, 16);
        this.checkBox1.TabIndex = 0;
        this.checkBox1.Text = "切换主要和次要的按钮";
        this.checkBox1.UseVisualStyleBackColor = true;
        this.checkBox1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.checkBox1_MouseUp);
        // 
        // groupBox2
        // 
        this.groupBox2.Controls.Add(this.pictureBox2);
        this.groupBox2.Controls.Add(this.label3);
        this.groupBox2.Controls.Add(this.label2);
        this.groupBox2.Controls.Add(this.label1);
        this.groupBox2.Controls.Add(this.trackBar1);
        this.groupBox2.Controls.Add(this.textBox2);
        this.groupBox2.Location = new System.Drawing.Point(12, 149);
        this.groupBox2.Name = "groupBox2";
        this.groupBox2.Size = new System.Drawing.Size(376, 100);
        this.groupBox2.TabIndex = 1;
        this.groupBox2.TabStop = false;
        this.groupBox2.Text = "双击速度";
        // 
        // label3
        // 
        this.label3.AutoSize = true;
        this.label3.Location = new System.Drawing.Point(227, 62);
        this.label3.Name = "label3";
        this.label3.Size = new System.Drawing.Size(17, 12);
        this.label3.TabIndex = 4;
        this.label3.Text = "快";
        // 
        // label2
        // 
        this.label2.AutoSize = true;
        this.label2.Location = new System.Drawing.Point(78, 62);
        this.label2.Name = "label2";
        this.label2.Size = new System.Drawing.Size(17, 12);
        this.label2.TabIndex = 3;
        this.label2.Text = "慢";
        // 
        // label1
        // 
        this.label1.AutoSize = true;
        this.label1.Location = new System.Drawing.Point(13, 60);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(59, 12);
        this.label1.TabIndex = 2;
        this.label1.Text = "速度(D):";
        // 
        // trackBar1
        // 
        this.trackBar1.AutoSize = false;
        this.trackBar1.Location = new System.Drawing.Point(97, 56);
        this.trackBar1.Name = "trackBar1";
        this.trackBar1.Size = new System.Drawing.Size(126, 30);
        this.trackBar1.TabIndex = 1;
        this.trackBar1.Scroll += new System.EventHandler(this.trackBar1_Scroll);
        // 
        // textBox2
        // 
        this.textBox2.BackColor = System.Drawing.Color.WhiteSmoke;
        this.textBox2.BorderStyle = System.Windows.Forms.BorderStyle.None;
        this.textBox2.Location = new System.Drawing.Point(15, 20);
        this.textBox2.Multiline = true;
        this.textBox2.Name = "textBox2";
        this.textBox2.ReadOnly = true;
        this.textBox2.Size = new System.Drawing.Size(230, 30);
        this.textBox2.TabIndex = 0;
        this.textBox2.Text = "双击文件夹以检测您的设置。如文件夹没打开或关闭,请用慢一点的设置再试一资。";
        // 
        // groupBox3
        // 
        this.groupBox3.Controls.Add(this.label6);
        this.groupBox3.Controls.Add(this.label5);
        this.groupBox3.Controls.Add(this.label4);
        this.groupBox3.Controls.Add(this.pictureBox3);
        this.groupBox3.Controls.Add(this.trackBar2);
        this.groupBox3.Location = new System.Drawing.Point(12, 255);
        this.groupBox3.Name = "groupBox3";
        this.groupBox3.Size = new System.Drawing.Size(376, 93);
        this.groupBox3.TabIndex = 2;
        this.groupBox3.TabStop = false;
        this.groupBox3.Text = "移动";
        // 
        // trackBar2
        // 
        this.trackBar2.AutoSize = false;
        this.trackBar2.Location = new System.Drawing.Point(108, 49);
        this.trackBar2.Maximum = 20;
        this.trackBar2.Minimum = 1;
        this.trackBar2.Name = "trackBar2";
        this.trackBar2.Size = new System.Drawing.Size(148, 34);
        this.trackBar2.TabIndex = 0;
        this.trackBar2.Value = 1;
        this.trackBar2.Scroll += new System.EventHandler(this.trackBar2_Scroll);
        // 
        // pictureBox3
        // 
        this.pictureBox3.Image = global::鼠标设置器.Properties.Resources.移动;
        this.pictureBox3.Location = new System.Drawing.Point(15, 20);
        this.pictureBox3.Name = "pictureBox3";
        this.pictureBox3.Size = new System.Drawing.Size(43, 37);
        this.pictureBox3.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
        this.pictureBox3.TabIndex = 1;
        this.pictureBox3.TabStop = false;
        // 
        // pictureBox2
        // 
        this.pictureBox2.Image = global::鼠标设置器.Properties.Resources.文件夹;
        this.pictureBox2.Location = new System.Drawing.Point(301, 23);
        this.pictureBox2.Name = "pictureBox2";
        this.pictureBox2.Size = new System.Drawing.Size(60, 57);
        this.pictureBox2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
        this.pictureBox2.TabIndex = 5;
        this.pictureBox2.TabStop = false;
        // 
        // pictureBox1
        // 
        this.pictureBox1.Image = global::鼠标设置器.Properties.Resources.鼠标左键;
        this.pictureBox1.Location = new System.Drawing.Point(263, 18);
        this.pictureBox1.Name = "pictureBox1";
        this.pictureBox1.Size = new System.Drawing.Size(98, 101);
        this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
        this.pictureBox1.TabIndex = 4;
        this.pictureBox1.TabStop = false;
        // 
        // label4
        // 
        this.label4.AutoSize = true;
        this.label4.Location = new System.Drawing.Point(64, 20);
        this.label4.Name = "label4";
        this.label4.Size = new System.Drawing.Size(113, 12);
        this.label4.TabIndex = 2;
        this.label4.Text = "选择指针移动速度:";
        // 
        // label5
        // 
        this.label5.AutoSize = true;
        this.label5.Location = new System.Drawing.Point(87, 55);
        this.label5.Name = "label5";
        this.label5.Size = new System.Drawing.Size(17, 12);
        this.label5.TabIndex = 3;
        this.label5.Text = "慢";
        // 
        // label6
        // 
        this.label6.AutoSize = true;
        this.label6.Location = new System.Drawing.Point(260, 55);
        this.label6.Name = "label6";
        this.label6.Size = new System.Drawing.Size(17, 12);
        this.label6.TabIndex = 4;
        this.label6.Text = "快";
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.BackColor = System.Drawing.Color.WhiteSmoke;
        this.ClientSize = new System.Drawing.Size(398, 358);
        this.Controls.Add(this.groupBox3);
        this.Controls.Add(this.groupBox2);
        this.Controls.Add(this.groupBox1);
        this.Name = "Form1";
        this.Text = "鼠标设置";
        this.Load += new System.EventHandler(this.Form1_Load);
        this.groupBox1.ResumeLayout(false);
        this.groupBox1.PerformLayout();
        this.groupBox2.ResumeLayout(false);
        this.groupBox2.PerformLayout();
        ((System.ComponentModel.ISupportInitialize)(this.trackBar1)).EndInit();
        this.groupBox3.ResumeLayout(false);
        this.groupBox3.PerformLayout();
        ((System.ComponentModel.ISupportInitialize)(this.trackBar2)).EndInit();
        ((System.ComponentModel.ISupportInitialize)(this.pictureBox3)).EndInit();
        ((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).EndInit();
        ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
        this.ResumeLayout(false);

    }

    #endregion

    private System.Windows.Forms.GroupBox groupBox1;
    private System.Windows.Forms.GroupBox groupBox2;
    private System.Windows.Forms.GroupBox groupBox3;
    private System.Windows.Forms.CheckBox checkBox1;
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.PictureBox pictureBox1;
    private System.Windows.Forms.TextBox textBox2;
    private System.Windows.Forms.Label label3;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.TrackBar trackBar1;
    private System.Windows.Forms.PictureBox pictureBox2;
    private System.Windows.Forms.TrackBar trackBar2;
    private System.Windows.Forms.PictureBox pictureBox3;
    private System.Windows.Forms.Label label6;
    private System.Windows.Forms.Label label5;
    private System.Windows.Forms.Label label4;
}

加载全部内容

相关教程
猜你喜欢
用户评论