Windows Systems Programming: Spring 2004

[ Home | Syllabus | Course Notes | Assignments | Search]


Mouse and Keyboard Input

Form Methods for Processing Mouse and Keyboard Input 

Method

Called When

Argument Type

OnKeyDown

A key is pressed

KeyEventArgs

OnKeyPress

A character is typed on the keyboard

KeyPressEventArgs

OnKeyUp

A key is released

KeyEventArgs

OnMouseDown

A mouse button is pressed

MouseEventArgs

OnMouseEnter

The mouse cursor enters a form

EventArgs

OnMouseHover

The mouse cursor pauses over a form

EventArgs

OnMouseLeave

The mouse cursor leaves a form

EventArgs

OnMouseMove

The mouse cursor moves over a form

MouseEventArgs

OnMouseUp

A mouse button is released

MouseEventArgs

OnMouseWheel

The mouse wheel is rolled

MouseEventArgs

 

Some Examples

protected override void OnKeyPress (KeyPressEventArgs e)
{
    if (e.KeyChar == 'C') {
        // Do something
    }
    else if (e.KeyChar == 'c') {
        // Do something else
    }
}
 
protected override void OnMouseDown (MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left) {
        Graphics g = Graphics.FromHwnd (Handle);
        Pen pen = new Pen (Color.Black);
        g.DrawLine (pen, e.X - 4, e.Y - 4, e.X + 4, e.Y + 4);
        g.DrawLine (pen, e.X - 4, e.Y + 4, e.X + 4, e.Y - 4);
        pen.Dispose ();
        g.Dispose ();
    }
}

MouseTracker.cs

using System;
using System.Windows.Forms;
using System.Drawing;

class MyForm : Form
{
    int cx;
    int cy;

    MyForm ()
    {
        Text = "Mouse Tracker";
        Graphics g = Graphics.FromHwnd (Handle);
        SizeF size = g.MeasureString ("MMMM, MMMM", Font); 
        cx = (Convert.ToInt32 (size.Width) / 2) + 8;
        cy = (Convert.ToInt32 (size.Height) / 2) + 8;
        g.Dispose ();
    }

    protected override void OnMouseMove (MouseEventArgs e)
    {
        Graphics g = Graphics.FromHwnd (Handle);
        EraseCoordinates (g);
        ShowCoordinates (e.X, e.Y, g);
        g.Dispose ();
    }

    protected override void OnMouseLeave (EventArgs e)
    {
        Graphics g = Graphics.FromHwnd (Handle);
        EraseCoordinates (g);
        g.Dispose ();
    }

    void EraseCoordinates (Graphics g)
    {
        int x = ClientRectangle.Width / 2;
        int y = ClientRectangle.Height / 2;

        SolidBrush brush = new SolidBrush (BackColor);
        g.FillRectangle (brush, x - cx, y - cy, x + cx, y + cy);
        brush.Dispose ();
    }

    void ShowCoordinates (int x, int y, Graphics g)
    {
        StringFormat format = new StringFormat ();
        format.Alignment = StringAlignment.Center;
        format.LineAlignment = StringAlignment.Center;

        string coords = String.Format ("{0}, {1}", x, y);

        SolidBrush brush = new SolidBrush (Color.Black);
        g.DrawString (coords, Font, brush, ClientRectangle, format); 
        brush.Dispose ();
    }

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


Virtual Methods That Correspond to Form-Level Events

Method

Called When

OnActivated

A form is activated

OnClosed

A form is closed

OnClosing

A form is about to close

OnDeactivate

A form is deactivated

OnGotFocus

A form receives the input focus

OnLoad

A form is created

OnLocationChanged

A form is moved

OnLostFocus

A form loses the input focus

OnPaintBackground

A form’s background needs repainting

OnSizeChanged

A form is resized

 

 

 


Copyright chris wild 1999-2004.
For problems or questions regarding this web contact [Dr. Wild].
Last updated: December 22, 2003.