public static Cursor CreateCursor(Bitmap bmp, int xHotSpot, int yHotSpot) { IconInfo tmp = new IconInfo(); GetIconInfo(bmp.GetHicon(), ref tmp); tmp.xHotspot = xHotSpot; tmp.yHotspot = yHotSpot; tmp.fIcon = false; return new Cursor(CreateIconIndirect(ref tmp)); }
private static System.Windows.Forms.Cursor CreateCursor(Bitmap bmp, int xHotSpot, int yHotSpot) { IconInfo tmp = new IconInfo(); GetIconInfo(bmp.GetHicon(), ref tmp); tmp.xHotspot = xHotSpot; tmp.yHotspot = yHotSpot; tmp.fIcon = false; return new System.Windows.Forms.Cursor(CreateIconIndirect(ref tmp)); }
public static Cursor CreateCursor(Bitmap bmp, int xHotSpot, int yHotSpot) { IntPtr ptr = bmp.GetHicon(); IconInfo tmp = new IconInfo(); GetIconInfo(ptr, ref tmp); tmp.xHotspot = xHotSpot; tmp.yHotspot = yHotSpot; tmp.fIcon = false; ptr = CreateIconIndirect(ref tmp); return new Cursor(ptr); }
/// <summary> /// Loads our Cursor from an Image /// </summary> /// <param name="path">Path of Image</param> /// <param name="xHotSpot">X HotSpot Location</param> /// <param name="yHotSpot">Y HotSpot Location</param> /// <returns></returns> internal static IntPtr LoadCursor(string path, int xHotSpot, int yHotSpot) { var image = CreateNonIndexedImage(path); var bitmapsIntPtr = new Bitmap(image).GetHicon(); //Get Bitmaps handle var tmp = new IconInfo(); GetIconInfo(bitmapsIntPtr, ref tmp); //Get Icon info and pass it back tmp.XHotspot = xHotSpot; //Create HotSpots for our Icon tmp.YHotspot = yHotSpot; tmp.FIcon = false; return CreateIconIndirect(ref tmp); //Return the handle for our Icon }
public static Cursor Convert(Bitmap b, int centerX, int centerY) { IntPtr ptr = b.GetHicon(); IconInfo tmp = new IconInfo(); GetIconInfo(ptr, ref tmp); tmp.xHotspot = centerX; tmp.yHotspot = centerY; tmp.fIcon = false; ptr = CreateIconIndirect(ref tmp); return new Cursor(ptr); }
/// <summary> /// Converts an image into a cursor. /// </summary> /// <param name="source">The Image to be converted.</param> /// <returns>The source image as a Cursor.</returns> public static Cursor CreateCursor(Image source) { Bitmap bmpSource = source as Bitmap; if (bmpSource == null) bmpSource = new Bitmap(source); IconInfo iInfo = new IconInfo(); GetIconInfo(bmpSource.GetHicon(), ref iInfo); iInfo.xHotspot = 3; iInfo.yHotspot = 3; iInfo.fIcon = false; return new Cursor(CreateIconIndirect(ref iInfo)); }
public static Bitmap GetBitmapForIcon(Icon icon) { using (IconInfo iconInfo = new IconInfo(icon)) { if (!iconInfo.Info.fIcon) return null; // this is a cursor! if (iconInfo.BitmapStruct.bmBitsPixel!=32) return Bitmap.FromHicon(icon.Handle); // no alpha blending, so use regular .Net Bitmap iconBitmap = Bitmap.FromHbitmap(iconInfo.Info.hbmColor); // get broken bitmap (broken - pixelformat doesn't specify alpha channel, even though the alpha data is there), // copy to a new bitmap with a healthy (includes the alpha channel) pixelformat Bitmap transparentBitmap; using (LockedBitMap lockedBitmap = new LockedBitMap(iconBitmap)) { transparentBitmap = new Bitmap(lockedBitmap.Data.Width, lockedBitmap.Data.Height, lockedBitmap.Data.Stride, PixelFormat.Format32bppArgb, lockedBitmap.Data.Scan0); } return transparentBitmap; } }
private static extern IntPtr CreateIconIndirect(ref IconInfo icon);
private static extern bool GetIconInfo(IntPtr hIcon, ref IconInfo pIconInfo);
/// <summary> /// Create a cursor from the given Resource Name /// </summary> /// <param name = "resourceName"></param> /// <param name = "xHotSpot"></param> /// <param name = "yHotSpot"></param> /// <returns></returns> public static Cursor CreateCursorFromResource(string resourceName, int xHotSpot, int yHotSpot) { ResourceManager resourceManager = new ResourceManager("MPTagThat.Properties.Resources", Assembly.GetCallingAssembly()); Bitmap bmp = (Bitmap)resourceManager.GetObject(resourceName); if (bmp != null) { IntPtr ptr = bmp.GetHicon(); IconInfo tmp = new IconInfo(); GetIconInfo(ptr, ref tmp); tmp.xHotspot = xHotSpot; tmp.yHotspot = yHotSpot; tmp.fIcon = false; ptr = CreateIconIndirect(ref tmp); return new Cursor(ptr); } return Cursors.Default; }
public static extern bool GetIconInfo(IntPtr hIcon, out IconInfo piconinfo);
internal static extern IntPtr CreateIconIndirect([In] ref IconInfo piconinfo);
internal static extern bool GetIconInfo (IntPtr hIcon, out IconInfo iconinfo);
private Cursor GenerateCursor(string text) { Bitmap imgColor = new Bitmap(32, 32, System.Drawing.Imaging.PixelFormat.Format24bppRgb); Color color = Color.MidnightBlue; SolidBrush brush = new SolidBrush(color); SolidBrush brushMask = new SolidBrush(Color.White); string s = text; System.Drawing.Font fnt = new Font(FontFamily.GenericSerif, 10, FontStyle.Bold); using (Graphics g = Graphics.FromImage(imgColor)) { SizeF size = g.MeasureString(s, fnt); if (size.Width > 32) size.Width = 32; if (size.Height > 32) size.Height = 32; g.FillRectangle(brushMask, 0, 0, 32, 32); g.DrawString(s, fnt, brush, (32 - size.Width) / 2, (32 - size.Height) / 2); g.Flush(); //g.DrawRectangle(Pens.OrangeRed, new Rectangle(1, 1, 30, 30)); } imgColor.MakeTransparent(Color.White); IconInfo ii = new IconInfo(); ii.fIcon = false; ii.xHotspot = 16; ii.yHotspot = 16; ii.hbmMask = imgColor.GetHbitmap(); ii.hbmColor = imgColor.GetHbitmap(); unsafe { IntPtr iiPtr = new IntPtr(&ii); IntPtr curPtr = CreateIconIndirect(iiPtr); return new Cursor(curPtr); } }
public static extern IntPtr CreateIconIndirect(ref IconInfo icon);
public static extern bool GetIconInfo(IntPtr hIcon, ref IconInfo pIconInfo);
internal static extern bool GetIconInfo(IntPtr hIcon, out IconInfo iconinfo);