Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

How do I determine if a mouse button is pressed and moving over a form?

0
Posted

How do I determine if a mouse button is pressed and moving over a form?

0

Catch the form’s MouseMove event which is called when the mouse moves over the form. To determine if a button is pressed during the move, check the event arg’s Button property. The code below draw’s a line on the form as the mouse moves over it. The line is red when the left button is pressed, white when the right button is pressed and blue when no button is pressed. private Point point = new Point( -1, -1 ); private void InitializeComponent() { // … MouseMove += new MouseEventHandler( Form1_MouseMove ); // … } private void Form1_MouseMove( object sender, MouseEventArgs e ) { if ( point.X == -1 ) point = new Point( e.X, e.Y ); Color color = BackColor; if ( e.Button == MouseButtons.Left ) color = Color.Red; else if ( e.Button == MouseButtons.Right ) color = Color.White; else if ( e.Button == MouseButtons.None ) color = Color.Blue; Point point2 = new Point( e.X,e.Y ); using ( Graphics g = Graphics.FromHwnd( Handle ) ) { using ( Pen pen = new Pen( color, 1 ) ) g.DrawLine( pen, point,

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123