/*
Professional Windows GUI Programming Using C#
by Jay Glynn, Csaba Torok, Richard Conway, Wahid Choudhury,
Zach Greenvoss, Shripad Kulkarni, Neil Whitlow
Publisher: Peer Information
ISBN: 1861007663
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Text;
namespace Wrox.ProgrammingWindowsGUI.Chapter5
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class MouseMovement : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox txtMouseInfo;
private System.Windows.Forms.Button btTrackMouseOn;
private System.Windows.Forms.Button btTrackMouseOff;
private bool toggleMouse = false;
private Timer tCheckMouse = new Timer();
private System.Windows.Forms.Button btScreenTrack;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public MouseMovement()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
txtMouseInfo.Text = GetMouseInfo().ToString();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.txtMouseInfo = new System.Windows.Forms.TextBox();
this.btTrackMouseOn = new System.Windows.Forms.Button();
this.btTrackMouseOff = new System.Windows.Forms.Button();
this.btScreenTrack = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// txtMouseInfo
//
this.txtMouseInfo.Multiline = true;
this.txtMouseInfo.Name = "txtMouseInfo";
this.txtMouseInfo.ReadOnly = true;
this.txtMouseInfo.Size = new System.Drawing.Size(432, 320);
this.txtMouseInfo.TabIndex = 0;
this.txtMouseInfo.Text = "";
this.txtMouseInfo.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Control_MouseMove);
//
// btTrackMouseOn
//
this.btTrackMouseOn.Location = new System.Drawing.Point(16, 328);
this.btTrackMouseOn.Name = "btTrackMouseOn";
this.btTrackMouseOn.Size = new System.Drawing.Size(192, 23);
this.btTrackMouseOn.TabIndex = 1;
this.btTrackMouseOn.Text = "Start Track Mouse";
this.btTrackMouseOn.Click += new System.EventHandler(this.btTrackMouseOn_Click);
this.btTrackMouseOn.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Control_MouseMove);
//
// btTrackMouseOff
//
this.btTrackMouseOff.Location = new System.Drawing.Point(232, 328);
this.btTrackMouseOff.Name = "btTrackMouseOff";
this.btTrackMouseOff.Size = new System.Drawing.Size(184, 23);
this.btTrackMouseOff.TabIndex = 2;
this.btTrackMouseOff.Text = "Stop Track Mouse";
this.btTrackMouseOff.Click += new System.EventHandler(this.btTrackMouseOff_Click_1);
this.btTrackMouseOff.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Control_MouseMove);
//
// btScreenTrack
//
this.btScreenTrack.Location = new System.Drawing.Point(16, 360);
this.btScreenTrack.Name = "btScreenTrack";
this.btScreenTrack.Size = new System.Drawing.Size(400, 23);
this.btScreenTrack.TabIndex = 3;
this.btScreenTrack.Text = "Start Screen wide mouse tracking";
this.btScreenTrack.Click += new System.EventHandler(this.btScreenTrack_Click);
//
// MouseMovement
//
tCheckMouse.Tick += new EventHandler(Timer_Check);
tCheckMouse.Enabled = true;
tCheckMouse.Stop();
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(432, 390);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.btScreenTrack,
this.btTrackMouseOff,
this.btTrackMouseOn,
this.txtMouseInfo});
this.MaximizeBox = false;
this.Name = "MouseMovement";
this.Text = "Mouse Movement";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new MouseMovement());
}
private StringBuilder GetMouseInfo()
{
StringBuilder sb = new StringBuilder();
sb.Append("Mouse Present: "+SystemInformation.MousePresent+"\r\n");
sb.Append("Number of Mouse Buttons: "+SystemInformation.MouseButtons+"\r\n");
sb.Append("Mouse Wheel Present: "+SystemInformation.MouseWheelPresent+"\r\n");
sb.Append("Number of Mouse Wheel scroll lines: "+SystemInformation.MouseWheelScrollLines+"\r\n");
sb.Append("Native wheel support: "+SystemInformation.NativeMouseWheelSupport+"\r\n");
sb.Append("Mouse buttons swapped: "+SystemInformation.MouseButtonsSwapped+"\r\n");
return sb;
}
private void btTrackMouseOn_Click(object sender, System.EventArgs e)
{
toggleMouse = true;
tCheckMouse.Stop();
}
protected override void OnMouseMove(MouseEventArgs e)
{
CheckMousePosition(e, null);
}
protected void Control_MouseMove(object sender, MouseEventArgs e)
{
CheckMousePosition(e, sender);
}
private void CheckMousePosition(MouseEventArgs e, object control)
{
if(control==null)
{
if(toggleMouse) txtMouseInfo.Text = "x: "+e.X+", y:"+e.Y;
}
else
{
int left = e.X+((Control)control).Left;
int top = e.Y+((Control)control).Top;
if(toggleMouse) txtMouseInfo.Text = "x: "+left+", y:"+top;
}
}
private void btTrackMouseOff_Click(object sender, System.EventArgs e)
{
toggleMouse = false;
txtMouseInfo.Text = GetMouseInfo().ToString();
}
private void Timer_Check(object sender, EventArgs e)
{
Point pMousePosition = Control.MousePosition;
txtMouseInfo.Text = "x: "+pMousePosition.X+", y:"+pMousePosition.Y;
}
private void btScreenTrack_Click(object sender, System.EventArgs e)
{
toggleMouse = false;
tCheckMouse.Start();
}
private void btTrackMouseOff_Click_1(object sender, System.EventArgs e)
{
toggleMouse = false;
txtMouseInfo.Text = GetMouseInfo().ToString();
}
}
}
|