using System;
using System.Drawing;
using System.Windows.Forms;
class BlockOut: Form
{
bool bBlocking, bValidBox;
Point ptBeg, ptEnd;
Rectangle rectBox;
public static void Main()
{
Application.Run(new BlockOut());
}
protected override void OnMouseDown(MouseEventArgs mea)
{
if (mea.Button == MouseButtons.Left)
{
ptBeg = ptEnd = new Point(mea.X, mea.Y);
Graphics grfx = CreateGraphics();
grfx.DrawRectangle(new Pen(ForeColor), Rect(ptBeg, ptEnd));
grfx.Dispose();
bBlocking = true;
}
}
protected override void OnMouseMove(MouseEventArgs mea)
{
if (bBlocking)
{
Graphics grfx = CreateGraphics();
grfx.DrawRectangle(new Pen(BackColor), Rect(ptBeg, ptEnd));
ptEnd = new Point(mea.X, mea.Y);
grfx.DrawRectangle(new Pen(ForeColor), Rect(ptBeg, ptEnd));
grfx.Dispose();
Invalidate();
}
}
protected override void OnMouseUp(MouseEventArgs mea)
{
if (bBlocking && mea.Button == MouseButtons.Left)
{
Graphics grfx = CreateGraphics();
rectBox = Rect(ptBeg, new Point(mea.X, mea.Y));
grfx.DrawRectangle(new Pen(ForeColor), rectBox);
grfx.Dispose();
bBlocking = false;
bValidBox = true;
Invalidate();
}
}
protected override void OnPaint(PaintEventArgs pea)
{
Graphics grfx = pea.Graphics;
if (bValidBox)
grfx.FillRectangle(new SolidBrush(ForeColor), rectBox);
if (bBlocking)
grfx.DrawRectangle(new Pen(ForeColor), Rect(ptBeg, ptEnd));
}
Rectangle Rect(Point ptBeg, Point ptEnd)
{
return new Rectangle(Math.Min(ptBeg.X, ptEnd.X),
Math.Min(ptBeg.Y, ptEnd.Y),
Math.Abs(ptEnd.X - ptBeg.X),
Math.Abs(ptEnd.Y - ptBeg.Y));
}
}
Event
Register Form Mouse Move, down and up action
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form
{
bool shouldPaint = false;
public Form1() {
InitializeComponent();
}
private void PainterForm_MouseDown( object sender, MouseEventArgs e )
{
shouldPaint = true;
}
private void PainterForm_MouseUp( object sender, MouseEventArgs e )
{
shouldPaint = false;
}
private void PainterForm_MouseMove( object sender, MouseEventArgs e )
{
if ( shouldPaint )
{
Graphics graphics = CreateGraphics();
graphics.FillEllipse(new SolidBrush( Color.BlueViolet ), e.X, e.Y, 4, 4 );
graphics.Dispose();
}
}
private void InitializeComponent()
{
this.SuspendLayout();
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(184, 180);
this.Name = "PainterForm";
this.Text = "Painter";
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.PainterForm_MouseDown);
this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.PainterForm_MouseUp);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.PainterForm_MouseMove);
this.ResumeLayout(false);
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}
}
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());
}
}
Which mouse button was clicked?
using System;
using System.Windows.Forms;
using System.Drawing;
public class MainWindow : Form {
public MainWindow() {
Height = 300;
Width = 500;
this.MouseMove += new MouseEventHandler(MainForm_MouseMove);
this.MouseUp += new MouseEventHandler(MainForm_MouseUp);
this.KeyUp += new KeyEventHandler(MainForm_KeyUp);
}
protected void MainForm_MouseMove(object sender, MouseEventArgs e) {
this.Text = string.Format("Current Pos: ({0}, {1})", e.X, e.Y);
}
protected void MainForm_MouseUp(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Left)
MessageBox.Show("Left click!");
if (e.Button == MouseButtons.Right)
MessageBox.Show("Right click!");
if (e.Button == MouseButtons.Middle)
MessageBox.Show("Middle click!");
}
protected void MainForm_KeyUp(object sender, KeyEventArgs e) {
MessageBox.Show(e.KeyCode.ToString(), "Key Pressed!");
}
public static void Main(string[] args) {
Application.Run(new MainWindow());
}
}
Register Form window message filter
using System;
using System.Windows.Forms;
using Microsoft.Win32;
public class MyMessageFilter : IMessageFilter
{
public bool PreFilterMessage(ref Message m)
{
// Intercept the left mouse button down message.
if (m.Msg == 513)
{
Console.WriteLine("WM_LBUTTONDOWN is: " + m.Msg);
return true;
}
return false;
}
}
public class mainForm : System.Windows.Forms.Form
{
private MyMessageFilter msgFliter = new MyMessageFilter();
public mainForm()
{
GetStats();
Application.ApplicationExit += new EventHandler(Form_OnExit);
Application.AddMessageFilter(msgFliter);
}
[STAThread]
static void Main()
{
Application.Run(new mainForm());
}
private void GetStats()
{
Console.WriteLine(Application.CompanyName+ " Company:");
Console.WriteLine(Application.ProductName+ " App Name:");
Console.WriteLine(Application.StartupPath);
}
// Event handlers.
private void Form_OnExit(object sender, EventArgs evArgs)
{
Console.WriteLine("Exit", "This app is dead...");
Application.RemoveMessageFilter(msgFliter);
}
}
Cast event sender to a control
using System;
using System.Drawing;
using System.Windows.Forms;
class InstantiateHelloWorld
{
public static void Main()
{
Form form = new Form();
form.Text = "Instantiate " + form.Text;
form.Paint += new PaintEventHandler(MyPaintHandler);
Application.Run(form);
}
static void MyPaintHandler(object objSender, PaintEventArgs pea)
{
Form form = (Form)objSender;
Graphics graphics = pea.Graphics;
graphics.DrawString("Hello from InstantiateHelloWorld!",
form.Font, Brushes.Black, 0, 100);
}
}
Keyboard Sample
/*
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 KeyboardSample
{
/// <summary>
/// Summary description for KeyboardSample.
/// </summary>
public class KeyboardSample : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.Label label2;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public KeyboardSample()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <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.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.label1 = new System.Windows.Forms.Label();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.label2 = new System.Windows.Forms.Label();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(16, 24);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(168, 20);
this.textBox1.TabIndex = 5;
this.textBox1.Text = "";
this.textBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyDown);
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(16, 24);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(168, 20);
this.textBox2.TabIndex = 6;
this.textBox2.Text = "";
this.textBox2.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox2_KeyDown);
//
// groupBox1
//
this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
this.label1,
this.textBox1});
this.groupBox1.Location = new System.Drawing.Point(8, 8);
this.groupBox1.Name = "groupBox1";
this.groupBox1.TabIndex = 7;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Key Monitor";
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 64);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(168, 20);
this.label1.TabIndex = 6;
//
// groupBox2
//
this.groupBox2.Controls.AddRange(new System.Windows.Forms.Control[] {
this.textBox2,
this.label2});
this.groupBox2.Location = new System.Drawing.Point(8, 120);
this.groupBox2.Name = "groupBox2";
this.groupBox2.TabIndex = 8;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "Keys Enumeration";
//
// label2
//
this.label2.Location = new System.Drawing.Point(16, 64);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(168, 20);
this.label2.TabIndex = 9;
//
// KeyboardSample
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(216, 229);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.groupBox2,
this.groupBox1});
this.Name = "KeyboardSample";
this.Text = "KeyboardSample";
this.groupBox1.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new KeyboardSample());
}
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
label1.Text = Convert.ToString(e.KeyValue);
}
private void textBox2_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
StringBuilder sb = new StringBuilder();
if(e.Shift) sb.Append("Shift, ");
if(e.Alt) sb.Append("Alt, ");
if(e.Control) sb.Append("Ctrl, ");
if(e.KeyCode==Keys.W||e.KeyCode==Keys.R||e.KeyCode==Keys.O||e.KeyCode==Keys.X)
{
sb.Append("Wrox Press!!");
}
else if(e.KeyCode==Keys.Escape&&e.Modifiers==(Keys.Shift|Keys.Alt))
{
sb.Append("Escape - that won't work!");
}
else if(e.KeyCode == Keys.C && e.Modifiers==(Keys.Alt | Keys.Control))
{
sb.Append("CopyRight");
textBox2.SelectedText = "CopyRight";
textBox2.SelectionLength = 0;
}
else
{
sb.Append(Convert.ToString(e.KeyData));
}
label2.Text = sb.ToString();
}
}
}