Mouse left click, right click, middle click, hover : Mouse Event « Event « C# / C Sharp






Mouse left click, right click, middle click, hover

Mouse left click, right click, middle click, hover
 

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

public class frmMouseButtons : System.Windows.Forms.Form {
    private System.Windows.Forms.Label lblLeftClick;
    private System.Windows.Forms.Label lblRightClick;
    private System.Windows.Forms.Label lblMiddleClick;
    private System.Windows.Forms.Label lblHover;
    public frmMouseButtons() {
        this.lblLeftClick = new System.Windows.Forms.Label();
        this.lblRightClick = new System.Windows.Forms.Label();
        this.lblMiddleClick = new System.Windows.Forms.Label();
        this.lblHover = new System.Windows.Forms.Label();
        this.SuspendLayout();

        this.lblLeftClick.Location = new System.Drawing.Point(8, 8);

        this.lblRightClick.Location = new System.Drawing.Point(8, 32);
        this.lblRightClick.Size = new System.Drawing.Size(100, 32);

        this.lblMiddleClick.Location = new System.Drawing.Point(8, 72);

        this.lblHover.Location = new System.Drawing.Point(8, 104);
        this.lblHover.MouseEnter += new System.EventHandler(this.lblHover_MouseEnter);
        this.lblHover.MouseHover += new System.EventHandler(this.lblHover_MouseHover);
        this.lblHover.MouseLeave += new System.EventHandler(this.lblHover_MouseLeave);

        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(184, 198);
        this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                      this.lblHover,
                                      this.lblMiddleClick,
                                      this.lblRightClick,
                                      this.lblLeftClick});
        this.MaximizeBox = false;
        this.ResumeLayout(false);

    }
    protected override void OnMouseDown(MouseEventArgs e) {
        switch (e.Button) {
            case (MouseButtons.Left):
                lblLeftClick.Text = "Left Click";
                break;
            case (MouseButtons.Middle):
                lblLeftClick.Text = "Middle Click";
                break;
            case (MouseButtons.Right):
                lblLeftClick.Text = "Right Click";
                break;
            case (MouseButtons.XButton1):
                lblLeftClick.Text = "XButton1 Click";
                break;
            case (MouseButtons.XButton2):
                lblLeftClick.Text = "XButton2 Click";
                break;
        }
        switch (e.Clicks) {
            case 1:
                lblMiddleClick.Text = "Single Click";
                break;
            case 2:
                lblMiddleClick.Text = "Double Click!";
                break;
            default:
                lblMiddleClick.Text = "Many clicks!";
                break;
        }
    }

    protected override void OnMouseWheel(MouseEventArgs e) {
        switch (e.Delta) {
            case -360:
                lblRightClick.Text = "One Rotation Reverse";
                break;
            case -720:
                lblRightClick.Text = "Two Rotations Reverse";
                break;
            case 360:
                lblRightClick.Text = "One Rotation Forward";
                break;
            case 720:
                lblRightClick.Text = "Two Rotations Forward";
                break;
            default:
                lblRightClick.Text = "Rotation wasn't full turn of wheel";
                break;
        }
    }


    protected void lblHover_MouseEnter(object sender, EventArgs e) {
        lblRightClick.Text = "One Rotation Forward";
        lblHover.Text = "Entering label";
        Cursor = Cursors.NoMove2D;
    }

    protected void lblHover_MouseHover(object sender, EventArgs e) {
        lblHover.Text = "Hovering over label";
        Cursor = Cursors.Hand;
        System.Diagnostics.Debug.WriteLine("hover");
    }

    protected void lblHover_MouseLeave(object sender, EventArgs e) {
        lblHover.Text = "Leaving label";
        Cursor = Cursors.Default;
    }
    [STAThread]
    static void Main() {
        Application.Run(new frmMouseButtons());
    }
}

 








Related examples in the same category

1.Which mouse button was clicked?
2.Register Form Mouse Move, down and up actionRegister Form Mouse Move, down and up action
3.Block Mouse
4.Mouse Enter actionMouse Enter action
5.Mouse Exit actionMouse Exit action
6.Mouse click actionMouse click action
7.Mouse Hover actionMouse Hover action
8.Mouse Down actionMouse Down action
9.Mouse up actionMouse up action
10.Mouse Double Click eventMouse Double Click event
11.Handle Mouse click event on an ImageHandle Mouse click event on an Image
12.Catch Mouse Click event inside a strange shapeCatch Mouse Click event inside a strange shape
13.Mouse MovementMouse Movement
14.Mouse Buttons ClickMouse Buttons Click
15.Mouse moveMouse move
16.Full screen and KeyEvent and MouseEvent
17.Mouse HandlerMouse Handler
18.Hit TestingHit Testing