How can I make the cursor design change when I hover the mouse cursor over a specific area of the PictureBox in Windows Forms?
|
1 answer
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); } } |
MouseMoveand set theCursorproperty. Show your code. - Alexander Petrov