基於C#實現鼠標設置功能

實踐過程

效果

代碼

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;
}

到此這篇關於基於C#實現鼠標設置功能的文章就介紹到這瞭,更多相關C#鼠標設置內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: