Download presentation
Presentation is loading. Please wait.
Published byOliver Chapman Modified over 8 years ago
1
2002 Prentice Hall. All rights reserved. 1 Outline Mouse Event Handling Keyboard Event Handling Graphical User Interface Concepts:
2
2002 Prentice Hall. All rights reserved. 2 Mouse Event Handling Mouse events generated when mouse interacts with a control Mouse event-handling methods take an object and a MouseEventArgs object as arguments Class MouseEventArgs –Contain coordinates of the mouse pointer relative to control –Mouse button pressed –Number of clicks and number of notches the wheel turned –Passing mouse event –The Click event uses delegate EventHandler and event arguments EventArgs
3
2002 Prentice Hall. All rights reserved. 3 Properties and Events Properties –Button –Clicks –X –Y Events (all relative to the control) –MouseEnter (enters control) –MouseLeave –MouseDown –MouseHover –MouseMove –MouseUp
4
2002 Prentice Hall. All rights reserved. Outline 4 // Using the mouse to draw on a form. // creates a form as a drawing surface public partial class PainterForm : Form { bool shouldPaint = false; // whether to paint //paint after mouse button has been pressed private void PainterForm_MouseDown ( object sender, System.Windows.Forms.MouseEventArgs e ) { shouldPaint = true; } // stop painting when mouse button released private void PainterForm_MouseUp ( object sender, System.Windows.Forms.MouseEventArgs e ) { shouldPaint = false; } Creates variable shouldPaint to determine whether to draw on the form The event handler for event MouseDown shouldPaint is set to true when this event occurs Mouse cursor will draw The event handler of event MouseUp shouldPaint set to false, mouse cursor will not draw
5
2002 Prentice Hall. All rights reserved. Outline 5 // draw circle whenever mouse button // moves (and mouse is down) protected void PainterForm_MouseMove( object sender, System.Windows.Forms.MouseEventArgs e ) { if ( shouldPaint ) { Graphics graphics = CreateGraphics(); graphics.FillEllipse( new SolidBrush( Color.BlueViolet ), e.X, e.Y, 4, 4 ); } } // end Painter_MouseMove Program will draw only if shouldPaint is true Creates the graphic object for the form Provides method for drawing various shapes Method FillEllipse draws a circle at every point the mouse cursor moves over and shouldPaint is true SolidBrush object determines the color of the shape drawn Create new SolidBrush object by passing the constructor a Color value Structure Color contain numerous predefined color constants Coordinates of x and y and the pixels height and width are supplied to the parameter list DEMO Painter
6
2002 Prentice Hall. All rights reserved. 6 Keyboard Event Handling Key events –Any control that inherits from System.Windows.Forms.Control Two Types 1.Delegate KeyPressEventHandler –Event argument KeyPressEventArgs –KeyPress ASCII character pressed No modifier keys 2.Delegate KeyEventHandler –Event argument KeyEventArgs –KeyUp or KeyDown Special modifier keys –Key enumeration value
7
2002 Prentice Hall. All rights reserved. 7 KeyPressEventHandler Properties and Events Properties –KeyChar (ascii) –Handled (whether key press event was handled) Events –KeyPress
8
2002 Prentice Hall. All rights reserved. 8 KeyEventHandler Properties and Events Properties –Alt, Contrl, Shift – whethere they were pressed –Handled –KeyData (returns as a Keys enumeration) –KeyValue (returns as an int) –Modifiers (returns a Key enum for any modifier key) Events –KeyDown (when initially pushed down) –KeyUp(when released)
9
2002 Prentice Hall. All rights reserved. Outline 9 // Displays info about the key pressed. public partial class KeyDemoForm :Form { private System.Windows.Forms.Label charLabel ; private System.Windows.Forms.Label keyInfoLabel ; private System.ComponentModel.Container components = null; // display the character pressed using key char protected void KeyDemoForm_KeyPress( object sender, System.Windows.Forms.KeyPressEventArgs e ) { charLabel.Text = "Key pressed: " + e.KeyChar; } Forms contain two LabelsLabel for key pressedLabel for modifier informationInitially emptyKeyPress event handler Accesses the KeyChar property of the KeyPressEventArgs object Key pressed as a char Display the key pressed If key pressed is not ASCII, charLabel remains empty
10
2002 Prentice Hall. All rights reserved. Outline 10 // display modifier keys, key code, key data and key value private void KeyDemoForm_KeyDown( object sender, System.Windows.Forms. KeyEventArgs e ) { keyInfoLabel.Text = "Alt: " + ( e.Alt ? "Yes" : "No") + '\n' + "Shift: " + ( e.Shift ? "Yes" : "No" ) + '\n'+ "Ctrl: " + ( e.Control ? "Yes" : "No" ) + '\n' + " KeyCode: " + e.KeyCode + '\n' + "KeyData: " + e.KeyData + '\n' + "KeyValue: " + e.KeyValue; } // clear labels when key released private void KeyDemoForm_KeyUp ( object sender, System.Windows.Forms. KeyEventArgs e ) { keyInfoLabel.Text = ""; charLabel.Text = ""; } KeyEventArgs object This block test for special keys, return bool if matched Uses Alt, Shift, and Control properties Displays the KeyCode, KeyData, and KeyValue properties KeyCode returns a Keys enumeration converted into a string using ToString KeyCode returns the key pressed without modifier keys information KeyData property returns a Keys enumeration with data about modifier keys KeyValue returns the key code as an integerInteger value is the Windows virtual key code KeyUp event handler clears both labels
11
2002 Prentice Hall. All rights reserved. Outline 11 KeyDemo.cs Program Output KeyPress event was not engagedKeyDown event still raised so keyInfoLabel displays information Keys enumeration can test for specific keys by comparing key pressed to KeyCode
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.