Some time ago I have decided to try to make something like a "Photoshop" in C#. The first problem has appeared was custom cursors. If you are familiar with "Photoshop" you know that when you change brush size the cursor size also changes. This is solution to make this possible in C# . Note: hare will described only black and white cursors. It is possible that I will upgrade this article or create new for 32-bit cursors.
Standard realization of class Cursor practically completely repeats Win API for the cursor except for method CreateCursor . This method is necessary for drawing custom cursor. So we import it from library user32.dll.
[DllImport("user32.dll")]
private static extern IntPtr CreateCursor(
IntPtr hInst,
int xHotSpot,
int yHotSpot,
int nWidth,
int nHeight,
byte[] pvANDPlane,
byte[] pvXORPlane
);
The method needs handle to the current window, coordinates of the cursor's hot spot, the size and mask of the image.
The image of the cursor is set by means of two masks (AND and XOR masks). Masks consist from array of byte where every b i t designates one pixel. Here results of a combination of two pixels:
byte[] andMaskCursor = new byte[]
{
0xFF, 0xFC, 0x3F, 0xFF, // line 1
0xFF, 0xC0, 0x1F, 0xFF, // line 2
0xFF, 0x00, 0x3F, 0xFF, // line 3
...
0xFF, 0xC3, 0xFF, 0xFF, // line 31
0xFF, 0xFF, 0xFF, 0xFF // line 32
};
byte[] xorMaskCursor = new byte[]
{
0x00, 0x00, 0x00, 0x00, // line 1
0x00, 0x03, 0xC0, 0x00, // line 2
0x00, 0x3F, 0x00, 0x00, // line 3
...
0x00, 0x00, 0x00, 0x00, // line 31
0x00, 0x00, 0x00, 0x00 // line 32
};
IntPtr cursorHandle = CreateCursor(
handle, // app. instance
radius, // hot spot horiz pos
radius, // hot spot vert pos
sideLength, // cursor width
sideLength, // cursor height
andMaskCursor, // AND mask
xorMaskCursor // XOR mask
);
Cursor.Current = new Cursor(cursorHandle);
Huuh.. now we can create cursors programmatically . Masks now are hard coded that is not applicable for us! Lets create mask with circle cursor:
int sideLength = radius*2;
if (sideLength%16 != 15)
{
sideLength = sideLength + 16-sideLength%16;
}
int length = sideLength*sideLength/8;
byte[] andMaskCursor = new byte[length];
byte[] xorMaskCursor = new byte[length];
for (int i = 0; i < sideLength; i++ )
{
for (int j = 0; j < sideLength; j++ )
{
double x = i - radius;
double y = j - radius;
double pRadius = Math.Pow(
Math.Pow(x, 2) + Math.Pow(y, 2), 0.5
);
if ((int)pRadius != radius)
{
int ii = (i*sideLength)+j;
andMaskCursor[ii/8] =
(byte)Math.Pow(2, 7-ii%8);
}
}
}
That's all! Enjoy.