// MouseDemoApplication.cs (c) Kari Laitinen // http://www.naturalprogramming.com/ // 2008-02-18 File created. // 2022-12-20 Tested in Developer Command Prompt for VS 2022. // This program demonstrates how a C# program can react to // mouse events. A kind of 'mouse' is drawn to the screen // when the mouse is moved. // The program shows how to find out which mouse button // is used during mouse operations. In addition this program // checks whether the Control or Shift key is pressed down // simultaneously with a mouse button. using System ; using System.Windows.Forms ; using System.Drawing ; class MouseDemoForm : Form { int current_mouse_position_x = 265 ; int current_mouse_position_y = 100 ; bool left_mouse_button_is_down = false ; bool middle_mouse_button_is_down = false ; bool right_mouse_button_is_down = false ; bool control_key_is_down = false ; bool shift_key_is_down = false ; public MouseDemoForm() { Text = "REACTING TO MOUSE EVENTS" ; Size = new Size( 600, 400 ) ; // This sets the size of the window. } // The following method will be called automatically by the // program execution system when a mouse button is pressed // down. The MouseEventArgs object that is received as a parameter // contains information related to the mouse event. protected override void OnMouseDown( MouseEventArgs mouse_data ) { current_mouse_position_x = mouse_data.X ; current_mouse_position_y = mouse_data.Y ; if ( mouse_data.Button == MouseButtons.Left ) { left_mouse_button_is_down = true ; } else if ( mouse_data.Button == MouseButtons.Middle ) { middle_mouse_button_is_down = true ; } else if ( mouse_data.Button == MouseButtons.Right ) { right_mouse_button_is_down = true ; } Invalidate() ; } // The OnMouseMove() method is called when the mouse is moved. protected override void OnMouseMove( MouseEventArgs mouse_data ) { current_mouse_position_x = mouse_data.X ; current_mouse_position_y = mouse_data.Y ; Invalidate() ; } protected override void OnMouseUp( MouseEventArgs mouse_data ) { if ( mouse_data.Button == MouseButtons.Left ) { left_mouse_button_is_down = false ; } else if ( mouse_data.Button == MouseButtons.Middle ) { middle_mouse_button_is_down = false ; } else if ( mouse_data.Button == MouseButtons.Right ) { right_mouse_button_is_down = false ; } Invalidate() ; } protected override void OnKeyDown( KeyEventArgs key_data ) { if ( key_data.Control == true ) { control_key_is_down = true ; } if ( key_data.Shift == true ) { shift_key_is_down = true ; } Console.Write( " " + control_key_is_down ) ; } protected override void OnKeyUp( KeyEventArgs key_data ) { if ( key_data.Control == false ) { control_key_is_down = false ; } if ( key_data.Shift == false ) { shift_key_is_down = false ; } Console.Write( " " + control_key_is_down ) ; } protected override void OnPaint( PaintEventArgs paint_data ) { Graphics graphics = paint_data.Graphics ; // This method draws a kind of 'mouse' to the screen. // The following statement draws a body for the mouse. // The mouse body is a large rectangle. graphics.FillRectangle( new SolidBrush( Color.Black ), current_mouse_position_x - 70, current_mouse_position_y - 30, 140, 200 ) ; // The mouse buttons will be drawn with red color if either // the Ctrl or the Shift key is pressed down. SolidBrush brush_for_mouse_buttons ; if ( control_key_is_down == true || shift_key_is_down == true ) { brush_for_mouse_buttons = new SolidBrush( Color.Red ) ; } else { brush_for_mouse_buttons = new SolidBrush( Color.Yellow ) ; } // We'll draw only those mouse buttons that are being pressed. // Small rectangles represent mouse buttons. if ( left_mouse_button_is_down == true ) { graphics.FillRectangle( brush_for_mouse_buttons, current_mouse_position_x - 50, current_mouse_position_y, 30, 60 ) ; } if ( middle_mouse_button_is_down == true ) { graphics.FillRectangle( brush_for_mouse_buttons, current_mouse_position_x - 15, current_mouse_position_y, 30, 60 ) ; } if ( right_mouse_button_is_down == true ) { graphics.FillRectangle( brush_for_mouse_buttons, current_mouse_position_x + 20, current_mouse_position_y, 30, 60 ) ; } } } class MouseDemoApplication { static void Main() { Application.Run( new MouseDemoForm() ) ; } }