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

.NET
305
0
0
2023-07-06
标签   C#
目录
  • 实践过程
  • 效果
  • 代码

实践过程

效果

代码

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

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

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

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

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

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

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

    private void trackBar_Scroll(object sender, EventArgs e)
    {
        int tem_n =;
        switch (((TrackBar)sender).Value)//记录鼠标的双击速度
        {
            case: tem_n = 900; break;
            case: tem_n = 830; break;
            case: tem_n = 760; break;
            case: tem_n = 690; break;
            case: tem_n = 620; break;
            case: tem_n = 550; break;
            case: tem_n = 480; break;
            case: tem_n = 410; break;
            case: tem_n = 340; break;
            case: tem_n = 270; break;
            case: tem_n = 200; break;
        }
        DoubleClickTime_Set(tem_n);//设置鼠标的双击的速度
    }

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

    }

    #endregion

    private System.Windows.Forms.GroupBox groupBox;
    private System.Windows.Forms.GroupBox groupBox;
    private System.Windows.Forms.GroupBox groupBox;
    private System.Windows.Forms.CheckBox checkBox;
    private System.Windows.Forms.TextBox textBox;
    private System.Windows.Forms.PictureBox pictureBox;
    private System.Windows.Forms.TextBox textBox;
    private System.Windows.Forms.Label label;
    private System.Windows.Forms.Label label;
    private System.Windows.Forms.Label label;
    private System.Windows.Forms.TrackBar trackBar;
    private System.Windows.Forms.PictureBox pictureBox;
    private System.Windows.Forms.TrackBar trackBar;
    private System.Windows.Forms.PictureBox pictureBox;
    private System.Windows.Forms.Label label;
    private System.Windows.Forms.Label label;
    private System.Windows.Forms.Label label;
}