Jimi2000

DrawingCursor

Jul 2nd, 2021 (edited)
510
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.53 KB | None | 0 0
  1. // Usage:  
  2. // BrushCursor brushCursor = null;
  3. // [...]
  4. // brushCursor = new BrushCursor();
  5. // someControl.Cursor = brushCursor.CreateCursor(Color.Red, 10);
  6. // [...]
  7. // someControl.Cursor = brushCursor.CreateCursor(Color.Blue, 3);
  8. // [...]
  9. // brushCursor.Dispose();
  10. // someControl.Cursor = Cursors.Default;
  11.  
  12.     using System;
  13.     using System.Drawing;
  14.     using System.Drawing.Drawing2D;
  15.     using System.Runtime.InteropServices;
  16.     using System.Windows.Forms;
  17.  
  18.     public class BrushCursor : IDisposable
  19.     {
  20.         private Cursor currentCursor = null;
  21.  
  22.         public Cursor CreateCursor(Color color, int size)
  23.         {
  24.             if (currentCursor != null) Dispose();
  25.             size += (size ^ 2) == 0 ? 0 : 1;
  26.             size = Math.Max(Math.Min(size, 16), 2);
  27.             currentCursor = CreateCursorInternal(CreateBitmap(color, size), new Point(16, 16));
  28.             return currentCursor;
  29.         }
  30.  
  31.         private static Cursor CreateCursorInternal(Bitmap bitmap, Point hotSpot)
  32.         {
  33.             var iconInfo = new ICONINFO(hotSpot, bitmap.GetHbitmap());
  34.  
  35.             IntPtr memPtr = Marshal.AllocHGlobal(Marshal.SizeOf(iconInfo));
  36.             Marshal.StructureToPtr(iconInfo, memPtr, true);
  37.             IntPtr curPtr = CreateIconIndirect(memPtr);
  38.  
  39.             DestroyIcon(memPtr);
  40.             DeleteObject(iconInfo.hbmMask);
  41.             DeleteObject(iconInfo.hbmColor);
  42.  
  43.             return new Cursor(curPtr);
  44.         }
  45.  
  46.         private Bitmap CreateBitmap(Color color, int size)
  47.         {
  48.             var bitmap = new Bitmap(32, 32);
  49.             var rect = new RectangleF(Point.Empty, bitmap.Size);
  50.  
  51.             using (var g = Graphics.FromImage(bitmap))
  52.             using (var brush = new SolidBrush(color))
  53.             using (var pen = new Pen(Color.FromArgb(48, 48, 48)) { DashStyle = DashStyle.Dot }) {
  54.  
  55.                 // Clip the central region to support a transparent color
  56.                 g.SetClip(RectangleF.Inflate(rect, -8, -8), CombineMode.Exclude);
  57.                 g.DrawLine(pen, rect.Width / 2f, 0, rect.Width / 2f, rect.Height);
  58.                 g.DrawLine(pen, 0, rect.Height / 2f, rect.Width, rect.Height / 2f);
  59.                 g.ResetClip();
  60.  
  61.                 var spot = new RectangleF(new PointF((32 - size) / 2, (32 - size) / 2), new SizeF(size, size));
  62.                 g.SmoothingMode = SmoothingMode.AntiAlias;
  63.                 g.FillEllipse(brush, spot);
  64.             }
  65.             return bitmap;
  66.         }
  67.  
  68.         public void Dispose() {
  69.             Dispose(true);
  70.             GC.SuppressFinalize(this);
  71.         }
  72.  
  73.         protected virtual void Dispose(bool disposing) {
  74.             currentCursor?.Dispose();
  75.             currentCursor = null;
  76.         }
  77.  
  78.         private struct ICONINFO {
  79.             public bool fIcon;
  80.             public int xHotspot;
  81.             public int yHotspot;
  82.             public IntPtr hbmMask;
  83.             public IntPtr hbmColor;
  84.  
  85.             public ICONINFO(Point hotSpot, IntPtr hBitmap) {
  86.                 fIcon = false;
  87.                 xHotspot = hotSpot.X;
  88.                 yHotspot = hotSpot.Y;
  89.                 hbmMask = hBitmap;
  90.                 hbmColor = hBitmap;
  91.             }
  92.         }
  93.  
  94.         [DllImport("user32.dll", SetLastError = true)]
  95.         private static extern IntPtr CreateIconIndirect(IntPtr iconInfo);
  96.  
  97.         [DllImport("user32.dll", SetLastError = true)]
  98.         private static extern bool DestroyIcon(IntPtr hIcon);
  99.  
  100.         [DllImport("gdi32.dll")]
  101.         private static extern bool DeleteObject(IntPtr hObject);
  102.     }
  103.  
  104.  
Add Comment
Please, Sign In to add comment