[ Home | Syllabus | Course Notes | Assignments | Search]
Method |
Called When |
Argument Type |
OnKeyDown |
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 (); } }
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 ()); }
F1: Difference between raw and processed keyboard input (need to account for shift, control, etc)
F2: "Handle" is like Windows kernel object ID, "FromHwnd" is static method
F3: Gets height and width of the string "MMMM, MMMM" (which is a place holder for the mouse coordinates actually to be displayed) in the default "font" - used to position the display string
F4: used to erase area containing old coordinate string
F5: claim it will always be in the center
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 |