Csharp/CSharp Tutorial/GUI Windows Forms/Mouse Event — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 15:31, 26 мая 2010
Содержание
Mouse Button Left clicked
using System;
using System.Drawing;
using System.Windows.Forms;
class MouseConnect: Form
{
const int iMaxPoints = 1000;
int iNumPoints = 0;
Point[] apoint = new Point[iMaxPoints];
public static void Main()
{
Application.Run(new MouseConnect());
}
public MouseConnect()
{
}
protected override void OnMouseDown(MouseEventArgs mea)
{
if (mea.Button == MouseButtons.Left)
{
iNumPoints = 0;
Invalidate();
}
}
protected override void OnMouseMove(MouseEventArgs mea)
{
if (mea.Button == MouseButtons.Left)
{
apoint[iNumPoints++] = new Point(mea.X, mea.Y);
Graphics grfx = CreateGraphics();
grfx.DrawLine(new Pen(ForeColor), mea.X, mea.Y,
mea.X, mea.Y + 1);
grfx.Dispose();
}
}
protected override void OnMouseUp(MouseEventArgs mea)
{
if (mea.Button == MouseButtons.Left)
Invalidate();
}
protected override void OnPaint(PaintEventArgs pea)
{
Graphics grfx = pea.Graphics;
Pen pen = new Pen(ForeColor);
for (int i = 0 ; i < iNumPoints - 1; i++)
for (int j = i + 1; j < iNumPoints; j++)
grfx.DrawLine(pen, apoint[i], apoint[j]);
}
}
Mouse Enter / Hover / Leave
using System;
using System.Drawing;
using System.Windows.Forms;
public class MouseEnterHoverLeave : Form
{
private Button btn;
string str = "";
public MouseEnterHoverLeave()
{
Size = new Size(400,400);
btn = new Button();
btn.Parent = this;
btn.Location = new Point(50,50);
btn.Size = new Size(150,25);
btn.MouseEnter += new System.EventHandler(btn_MouseEnter);
btn.MouseHover += new System.EventHandler(btn_MouseHover);
btn.MouseLeave += new System.EventHandler(btn_MouseLeave);
}
static void Main()
{
Application.Run(new MouseEnterHoverLeave());
}
private void btn_MouseEnter(object sender, EventArgs e)
{
btn.Text = "MouseEnter";
}
private void btn_MouseHover(object sender, EventArgs e)
{
btn.Text = "MouseHover";
}
private void btn_MouseLeave(object sender, EventArgs e)
{
btn.Text = "MouseLeave";
}
protected override void OnMouseEnter(EventArgs e)
{
base.OnMouseEnter(e);
Console.WriteLine("Form MouseEnter");
}
protected override void OnMouseHover(EventArgs e)
{
base.OnMouseHover(e);
Console.WriteLine("Form MouseHover");
}
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
Console.WriteLine("Form MouseLeave");
}
}
Mouse event handlers
using System;
using System.Drawing;
using System.Windows.Forms;
public class MouseEvents : Form
{
private Label lbl;
public MouseEvents()
{
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 MouseEvents());
}
private void btnReset_Click(object sender, EventArgs e)
{
lbl.Text = "";
}
private void lbl_MouseEnter(object sender, EventArgs e)
{
lbl.Text = "MouseEnter";
TextBoxDraw("Label MouseEnter");
}
private void lbl_MouseHover(object sender, EventArgs e)
{
lbl.Text = "MouseHover";
TextBoxDraw("Label MouseHover");
}
private void lbl_MouseLeave(object sender, EventArgs e)
{
lbl.Text = "MouseLeave";
TextBoxDraw("Label MouseLeave");
}
private void lbl_MouseDown(object sender, MouseEventArgs e)
{
lbl.Text = "MouseDown";
string str;
str = "Label MouseDown";
str += "\r\n\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();
TextBoxDraw(str);
}
private void lbl_MouseMove(object sender, MouseEventArgs e)
{
lbl.Text = "MouseMove";
string str;
str = "Label MouseMove";
str += "\r\n\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();
TextBoxDraw(str);
}
private void lbl_MouseUp(object sender, MouseEventArgs e)
{
lbl.Text = "MouseUp";
string str;
str = "Label MouseUp";
str += "\r\n\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();
TextBoxDraw(str);
}
private void lbl_MouseWheel(object sender, MouseEventArgs e)
{
lbl.Text = "MouseWheel";
string str;
str = "Label MouseWheel";
str += "\r\n\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();
TextBoxDraw(str);
}
private void lbl_Click(object sender, EventArgs e)
{
lbl.Text = "Click";
TextBoxDraw("Label Click");
}
private void lbl_DoubleClick(object sender, EventArgs e)
{
lbl.Text = "DoubleClick";
TextBoxDraw("Label DoubleClick");
}
protected override void OnMouseEnter(EventArgs e)
{
base.OnMouseEnter(e);
TextBoxDraw("Form MouseEnter");
}
protected override void OnMouseHover(EventArgs e)
{
base.OnMouseHover(e);
TextBoxDraw("Form MouseHover");
}
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
TextBoxDraw("Form MouseLeave");
}
private void TextBoxDraw(string str){
Console.WriteLine(str);
}
}
Mouse Event information
using System;
using System.Drawing;
using System.Windows.Forms;
public class MainClass{
static void Main()
{
Console.WriteLine("DoubleClickSize"+ SystemInformation.DoubleClickSize.ToString());
Console.WriteLine("DoubleClickTime"+SystemInformation.DoubleClickTime.ToString());
Console.WriteLine("MouseButtons"+SystemInformation.MouseButtons.ToString());
Console.WriteLine("MouseButtonsSwapped"+SystemInformation.MouseButtonsSwapped.ToString());
Console.WriteLine("MousePresent"+SystemInformation.MousePresent.ToString());
Console.WriteLine("MouseWheelPresent"+SystemInformation.MouseWheelPresent.ToString());
Console.WriteLine("MouseWheelScrollLines"+SystemInformation.MouseWheelScrollLines.ToString());
Console.WriteLine("NativeMouseWheelSupport"+SystemInformation.NativeMouseWheelSupport.ToString());
}
}
DoubleClickSize{Width=4, Height=4} DoubleClickTime500 MouseButtons5 MouseButtonsSwappedFalse MousePresentTrue MouseWheelPresentTrue MouseWheelScrollLines3 NativeMouseWheelSupportTrue
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);
}
}