Mouse Properties : Mouse Event « GUI Windows Forms « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » GUI Windows Forms » Mouse Event 
23.62.2.Mouse Properties
Mouse Properties
using System;
using System.Drawing;
using System.Windows.Forms;

public class MouseProperties : Form
{
  private Label lbl;
  int i = 0;

  Cursor[] theCursors = {Cursors.AppStarting, 
              Cursors.Arrow, 
              Cursors.Hand, 
              Cursors.Help, 
              Cursors.No};

  public MouseProperties()
  {
    Size = new Size(400,600);

    lbl = new Label();
    lbl.Parent = this;
    lbl.Location = new Point(50,50);
    lbl.Size = new Size(150,25);
    lbl.BorderStyle = BorderStyle.Fixed3D;
    lbl.MouseEnter += new System.EventHandler(lbl_MouseEnter);
    lbl.MouseHover += new System.EventHandler(lbl_MouseHover);
    lbl.MouseLeave += new System.EventHandler(lbl_MouseLeave);
    lbl.MouseDown += new System.Windows.Forms.MouseEventHandler(lbl_MouseDown);
    lbl.MouseMove += new System.Windows.Forms.MouseEventHandler(lbl_MouseMove);
    lbl.MouseUp += new System.Windows.Forms.MouseEventHandler(lbl_MouseUp);
    lbl.MouseWheel += new System.Windows.Forms.MouseEventHandler(lbl_MouseWheel);
    lbl.Click += new System.EventHandler(lbl_Click);
    lbl.DoubleClick += new System.EventHandler(lbl_DoubleClick);

  }

  static void Main() 
  {
    Application.Run(new MouseProperties());
  }

  private void lbl_MouseEnter(object sender, EventArgs e)
  {
    lbl.Text = "MouseEnter";
    EventArgsStrings();
    lbl.Cursor = Cursors.WaitCursor;
  }

  private void lbl_MouseHover(object sender, EventArgs e)
  {
    lbl.Cursor = theCursors[i % 5];
    i++;

    lbl.Text = "MouseHover";
    EventArgsStrings();
    Console.WriteLine("Label MouseHover");
  }

  private void lbl_MouseLeave(object sender, EventArgs e)
  {
    lbl.Text = "MouseLeave";
    EventArgsStrings();
    Console.WriteLine("Label MouseLeave");
  }

  private void lbl_MouseDown(object sender, MouseEventArgs e)
  {
    lbl.Text = "MouseDown";
    MouseEventArgsStrings(e);
    Console.WriteLine("Label MouseDown");
  }

  private void lbl_MouseMove(object sender, MouseEventArgs e)
  {
    lbl.Text = "MouseMove";
    MouseEventArgsStrings(e);
    Console.WriteLine("Label MouseMove");
  }

  private void lbl_MouseUp(object sender, MouseEventArgs e)
  {
    lbl.Text = "MouseUp";
    MouseEventArgsStrings(e);
    Console.WriteLine("Label MouseUp");
  }

  private void lbl_MouseWheel(object sender, MouseEventArgs e)
  {
    lbl.Text = "MouseWheel";
    MouseEventArgsStrings(e);
    Console.WriteLine("Label MouseWheel");
  }

  private void lbl_Click(object sender, EventArgs e)
  {
    lbl.Text = "Click";
    EventArgsStrings();
    Console.WriteLine("Label Click");
  }

  private void lbl_DoubleClick(object sender, EventArgs e)
  {
    lbl.Text = "DoubleClick";
    EventArgsStrings();
    Console.WriteLine("Label DoubleClick");
  }

  private void EventArgsStrings()
  {
    string str;
    str = "\tCursor:  " + lbl.Cursor.ToString();
    str += "\r\n\tCapture:  " + lbl.Capture.ToString();
    str += "\r\n\tMouseButtons:  " + MouseButtons.ToString();
    str += "\r\n\tMousePosition:  " + MousePosition.ToString();
    str += "\r\n\tModifierKeys:  " + ModifierKeys.ToString();
    Console.WriteLine(str);
  }

  private void MouseEventArgsStrings(MouseEventArgs e)
  {
    string str;
    str = "\tButton:  " + e.Button.ToString();
    str += "\r\n\tClicks:  " + e.Clicks.ToString();
    str += "\r\n\tDelta:  " + e.Delta.ToString();
    str += "\r\n\tX:  " + e.X.ToString();
    str += "\r\n\tY:  " + e.Y.ToString();
    Console.WriteLine(str);
  }
}
23.62.Mouse Event
23.62.1.Mouse Event information
23.62.2.Mouse PropertiesMouse Properties
23.62.3.Mouse event handlersMouse event handlers
23.62.4.Mouse Enter / Hover / LeaveMouse Enter / Hover / Leave
23.62.5.Mouse Button Left clicked
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.