仿遨游的鼠标按住左键点击右键和按住右键点击左键的功能(C#源码)

namespace Test {
 partial class MouseClickEnhanceForm {
  /// <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.Init = new System.Windows.Forms.Button();
      this.textBox1 = new System.Windows.Forms.TextBox();
      this.SuspendLayout();
      //
      // Init
      //
      this.Init.Location = new System.Drawing.Point(107, 12);
      this.Init.Name = "Init";
      this.Init.Size = new System.Drawing.Size(75, 23);
      this.Init.TabIndex = 0;
      this.Init.Text = "初始化";
      this.Init.UseVisualStyleBackColor = true;
      this.Init.Click += new System.EventHandler(this.button1_Click);
      //
      // textBox1
      //
      this.textBox1.AcceptsReturn = true;
      this.textBox1.AcceptsTab = true;
      this.textBox1.Location = new System.Drawing.Point(12, 41);
      this.textBox1.Multiline = true;
      this.textBox1.Name = "textBox1";
      this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Both;
      this.textBox1.Size = new System.Drawing.Size(287, 356);
      this.textBox1.TabIndex = 1;
      this.textBox1.Text = "点击窗口进行测试";
      //
      // MouseClickEnhanceForm
      //
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
      this.ClientSize = new System.Drawing.Size(369, 457);
      this.Controls.Add(this.textBox1);
      this.Controls.Add(this.Init);
      this.Name = "MouseClickEnhanceForm";
      this.Text = "仿遨游双键鼠标功能";
      this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseUp);
      this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
      this.ResumeLayout(false);
      this.PerformLayout();

  }

  #endregion

  private System.Windows.Forms.Button Init;
    private System.Windows.Forms.TextBox textBox1;
 }
}

 另一个文件

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Test {
  /// <summary>
  /// 实现(1)按住右键然后点击左键,(2)按住左键然后点击右键
  /// 注意: 请在窗体中添加一个TextBox(变量名称为textBox1)
  ///       和Button(变量名为:botton1)
  /// </summary>
  public partial class MouseClickEnhanceForm : Form {
   
    public MouseClickEnhanceForm() {
      InitializeComponent();
    }

    /// <summary>
    /// 鼠标状态
    /// </summary>
    public enum MouseButtonsState {
      LeftDown = 1,
      LeftUp,
      RightDown,
      RightUp
    } ;

    //当前按键状态
    private int btnState = -1;
    //为了在TextBox中换行,使用此家伙,用TextBox.Text="/n"方式换行不好使,
    //不是鼠标功能的一部分只是为了换行显示信息
    private List<string> list = new List<string>();

    private void OutText(String text) {
      list.Add(text);
      this.textBox1.Lines = list.ToArray();
    }

    private void Form1_MouseDown(object sender, MouseEventArgs e) {
      switch (e.Button) {
        case MouseButtons.Left:
          if (btnState==(int)MouseButtonsState.RightDown) {
            this.OutText("1. 按住右键,点击左键");
          }
          btnState = (int) MouseButtonsState.LeftDown;
          this.OutText("2. 左键,MouseDown事件");
          break;
        case MouseButtons.Right:
          if (btnState==(int)MouseButtonsState.LeftDown) {
            this.OutText("3. 按住左键,点击右键");
          }
          btnState = (int) MouseButtonsState.RightDown;
          this.OutText("4. 右键,MouseDown事件");
          break;
      }
    }

    private void Form1_MouseUp(object sender, MouseEventArgs e) {
      this.btnState = -1;
      this.OutText("5. 初始btnSate值为: -1");
    }

    private void button1_Click(object sender, EventArgs e) {
      this.textBox1.Text = "";
      btnState = -1;
    }
  }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值