SIGN UP MEMBER LOGIN:    
ARTICLE

Custom Drawing Cursors in C#

Posted by Rajesh Charagandla Articles | Windows Forms C# March 14, 2006
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# .
Reader Level:

Introduction

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 sizes of the cursor :

Win API contains method GetSystemMetrics which returns the possible size of cursor, but it always return 32/32 for standard video devices or 64/64 for hi-resolution devices. In the experimental way I have find out that the sizes of the cursor should be multiple 16 px. So we will not use GetSystemMetrics . Attempt of use of the non-standard size leads to wrong display of the image. As greater accuracy is necessary for us than 16 px we shall take the size of the cursor with a stock. All that remains will be in visible so this is for us!

Masks of the cursor:

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:

 img1.gif

Result code:


Using CreateCursor function this is simple to create new cursor from masks:

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.

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor