I've come across some weird behavior while playing with custom cursors in c# using Windows Forms. I've reduced the problem to a new project with 1 form, 2 panels added to the form, 1 icon added to project's properties/resources.resx, and 3 images added to the same place.
The only code the project will have is this
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace TestProject
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
panel1.Cursor = new Cursor(Properties.Resources.randomIcon.Handle);
panel2.Cursor = new Cursor(Properties.Resources.randomIcon.Handle);
}
}
}
Now, the weird behaviour starts when I modify the backgroundimage property of the Form. I've downloaded multiple random images from various sources and set them as form's background image.
When I set some of them and run the program, both of the panels have their custom cursor.
When I set other images, only the second panel has its cursor.
I've even found an image with which panel1 has its cursor for the first few seconds, but if I happen to get out and back in the panel after those few seconds, the cursor gets permanently removed.
If I change the backgroundimagelayout property of the form to None instead of Tile, the cursor works no matter what the image is.
There is nothing else that's being modified in the entire project. 1 Form, 2 Panels, 1 icon as the cursor, 3 images and a totally senseless (or maybe not, maybe i'm missing something) behavior.
I just want to find out what causes this behavior as I encountered it while working on bigger project, and it took me a while to track the core of the problem. Maybe i shouldn't be using the cursors this way, but the question remains, what makes c# behave in such a way when nothing but the background image is being modified.
I've made a sample project here - https://www.dropbox.com/s/bl4iomzyz1bv7kb/Sample.rar?dl=0