How can I make the cursor design change when I hover the mouse cursor over a specific area of ​​the PictureBox in Windows Forms?

  • Handle the MouseMove and set the Cursor property. Show your code. - Alexander Petrov
  • Thank you very much for the answer. I implement drawing Bezier curves. I want to be able to move one of the control points when I hover over it. Code in 300 lines, what exactly do you want to see? - Gymon
  • Implement the MouseMove event handling. If something does not work out - show the code, tell you what is wrong. - Alexander Petrov
  • OK got it. Thank you very much for your help) - Gymon

1 answer 1

It is necessary to check that the cursor is over some coordinates of the control, as Alexander Petrov wrote: MouseMove is an excellent fit. As an example - 2 squares, red and blue, and the usual black background, when you hover on any of them, the cursor changes.

 public partial class Form1 : Form { public Form1() { InitializeComponent(); var pictureBox = new PictureBox() { Height = 100, Width = 100 }; var bitmap = new Bitmap(pictureBox.Width, pictureBox.Height); var redRectangle = new Rectangle(0, 0, 20, 20); var blueRectangle = new Rectangle(30, 20, 20, 20); var rectangles = new List<Rectangle> { redRectangle, blueRectangle }; // Рисуем изображение для PictureBox'а using(var graphics = Graphics.FromImage(bitmap)) { graphics.Clear(Color.Black); graphics.FillRectangle(Brushes.Red, redRectangle); graphics.FillRectangle(Brushes.Blue, blueRectangle); } pictureBox.Image = bitmap; pictureBox.MouseMove += (s, e) => { // Проверяем, что курсор ни в одной из заданных областей if(rectangles.All(x => !x.Contains(e.Location))) if(Cursor.Current != Cursors.No) Cursor.Current = Cursors.No; // Красный квадрат if(redRectangle.Contains(e.Location)) if(Cursor.Current != Cursors.Hand) Cursor.Current = Cursors.Hand; // Синий квадрат if(blueRectangle.Contains(e.Location)) if(Cursor.Current != Cursors.Cross) Cursor.Current = Cursors.Cross; }; Controls.Add(pictureBox); } }