基于C#實(shí)現(xiàn)鼠標(biāo)設(shè)置功能
更新時(shí)間:2022年12月18日 14:01:22 作者:芝麻粒兒
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)鼠標(biāo)設(shè)置功能,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下
實(shí)踐過程
效果

代碼
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;//如左右鼠標(biāo)鍵已經(jīng)交換,則為TRUE
const int SPI_SETMOUSE = 4;//設(shè)置鼠標(biāo)的移動(dòng)速度
const int SPI_GETMOUSESPEED = 112;//檢索當(dāng)前鼠標(biāo)速度
const uint SPIF_UPDATEINIFILE = 0x0001;//更新win.ini和(或)注冊表中的用戶配置文件
const uint SPIF_SENDWININICHANGE = 0x0002;//該消息告訴應(yīng)用程序已經(jīng)改變了用戶配置設(shè)置
int aMouseinfo=0;
private void Form1_Load(object sender, EventArgs e)
{
if (GetSystemMetrics(SM_SWAPBUTTON) == 0)//如果鼠標(biāo)的左右鍵沒有切換
{
pictureBox1.Image = null;//清空圖片
pictureBox1.Image = Properties.Resources.鼠標(biāo)左鍵;//顯示圖片
checkBox1.Checked = false;//設(shè)置復(fù)選框?yàn)椴贿x中狀態(tài)
}
else//如果鼠標(biāo)左右切換
{
pictureBox1.Image = null;//清空圖片
pictureBox1.Image = Properties.Resources.鼠標(biāo)右鍵;//顯示圖片
checkBox1.Checked = true;//設(shè)置復(fù)選框?yàn)檫x中狀態(tài)
}
int tem_n = 0;
switch (Convert.ToInt32(DoubleClickTime_Get()))//獲取鼠標(biāo)的雙擊速度
{
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;//顯示鼠標(biāo)的雙擊速度
SystemParametersInfo(SPI_GETMOUSESPEED, 0, ref aMouseinfo, 0);//獲取當(dāng)前鼠標(biāo)的移動(dòng)速度
trackBar2.Value = aMouseinfo;//顯示當(dāng)前鼠標(biāo)的移動(dòng)速度
}
public string DoubleClickTime_Get()
{
return GetDoubleClickTime().ToString();//獲取鼠標(biāo)的雙擊速度
}
public void DoubleClickTime_Set(int MouseDoubleClickTime)
{
SetDoubleClickTime(MouseDoubleClickTime);//設(shè)置獲取鼠標(biāo)的雙擊速度
}
private void checkBox1_MouseUp(object sender, MouseEventArgs e)
{
if (((CheckBox)sender).Checked == true)//如果為選中狀態(tài)
{
pictureBox1.Image = null;//清空圖片
pictureBox1.Image = Properties.Resources.鼠標(biāo)右鍵;//顯示圖片
SwapMouseButton(1);//切換鼠標(biāo)左右鍵
}
else//如果不為選中狀態(tài)
{
if (((CheckBox)sender).Checked == false)
{
pictureBox1.Image = null;//清空圖片
pictureBox1.Image = Properties.Resources.鼠標(biāo)左鍵;//顯示圖片
SwapMouseButton(0);//恢復(fù),設(shè)置左鍵為主鍵
}
}
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
int tem_n = 0;
switch (((TrackBar)sender).Value)//記錄鼠標(biāo)的雙擊速度
{
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);//設(shè)置鼠標(biāo)的雙擊的速度
}
private void trackBar2_Scroll(object sender, EventArgs e)
{
aMouseinfo = trackBar2.Value;//記錄鼠標(biāo)的移動(dòng)速度
SystemParametersInfo(SPI_SETMOUSE, 0, ref aMouseinfo, SPIF_UPDATEINIFILE);//設(shè)置鼠標(biāo)的移動(dòng)速度
}
}
partial class Form1
{
/// <summary>
/// 必需的設(shè)計(jì)器變量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
/// <param name="disposing">如果應(yīng)釋放托管資源,為 true;否則為 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗體設(shè)計(jì)器生成的代碼
/// <summary>
/// 設(shè)計(jì)器支持所需的方法 - 不要
/// 使用代碼編輯器修改此方法的內(nèi)容。
/// </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 = "鼠標(biāo)鍵配置";
//
// 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 = "選擇些復(fù)選框來將右按鈕設(shè)成用于主要功能如選擇和拖放之用";
//
// 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 = "雙擊文件夾以檢測您的設(shè)置。如文件夾沒打開或關(guān)閉,請用慢一點(diǎn)的設(shè)置再試一資。";
//
// 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 = "移動(dòng)";
//
// 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::鼠標(biāo)設(shè)置器.Properties.Resources.移動(dòng);
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::鼠標(biāo)設(shè)置器.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::鼠標(biāo)設(shè)置器.Properties.Resources.鼠標(biāo)左鍵;
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 = "選擇指針移動(dòng)速度:";
//
// 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 = "鼠標(biāo)設(shè)置";
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;
}
到此這篇關(guān)于基于C#實(shí)現(xiàn)鼠標(biāo)設(shè)置功能的文章就介紹到這了,更多相關(guān)C#鼠標(biāo)設(shè)置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#實(shí)現(xiàn)將聊天數(shù)據(jù)發(fā)送加密
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)將聊天數(shù)據(jù)發(fā)送加密的功能,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2022-12-12
C#實(shí)現(xiàn)身份證驗(yàn)證功能的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)身份證驗(yàn)證功能,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2022-12-12
C#將PDF文檔轉(zhuǎn)換為Markdown文檔的代碼實(shí)現(xiàn)
將PDF文件轉(zhuǎn)換為Markdown格式是一個(gè)非常實(shí)用的需求,尤其是在需要將內(nèi)容從固定布局的PDF文件中提取出來,并轉(zhuǎn)換為更易于編輯和處理的文本格式時(shí),本文將介紹如何通過C#代碼將PDF文檔轉(zhuǎn)換Markdown(MD)文檔,需要的朋友可以參考下2024-10-10
DevExpress中GridControl列轉(zhuǎn)義的實(shí)現(xiàn)方法
這篇文章主要介紹了DevExpress中GridControl列轉(zhuǎn)義的實(shí)現(xiàn)方法,在項(xiàng)目開發(fā)中有一定的實(shí)用價(jià)值,需要的朋友可以參考下2014-08-08

