private void CreateCursor(int width, int height, BitmapHandle dibSectionHandle) { BitmapHandle monoBitmapHandle = null; try { monoBitmapHandle = new BitmapHandle(CreateBitmap(width, height, 1, 1, IntPtr.Zero)); ICONINFO icon = new ICONINFO(); icon.IsIcon = false; icon.xHotspot = 0; icon.yHotspot = 0; icon.ColorBitmap = dibSectionHandle; icon.MaskBitmap = monoBitmapHandle; _iconHandle = CreateIconIndirect(ref icon); if (!_iconHandle.IsInvalid) { _ghostCursor = CursorInteropHelper.Create(_iconHandle); } } finally { // destroy the temporary mono bitmap now ... if (monoBitmapHandle != null) { monoBitmapHandle.Dispose(); } } }
/// <summary>Loads an icon from the system image list.</summary> /// <param name="iIdx">A value of type int that contains the index of the image.</param> /// <param name="imgSz">The width, in pixels, of the icon.</param> /// <param name="hico">The resulting icon handle, on success, or <c>null</c> on failure.</param> /// <returns>The result of function.</returns> public static HRESULT LoadIconFromSystemImageList(int iIdx, ref uint imgSz, out SafeHICON hico) { HRESULT hr; if ((hr = SHGetImageList(PixelsToSHIL((int)imgSz), typeof(ComCtl32.IImageList).GUID, out var il)).Succeeded) { try { hico = il.GetIcon(iIdx, ComCtl32.IMAGELISTDRAWFLAGS.ILD_TRANSPARENT); using var icoInfo = new ICONINFO(); if (GetIconInfo(hico, icoInfo)) { imgSz = (uint)GetObject <BITMAP>(icoInfo.hbmColor).bmWidth; } } catch (COMException e) { hico = null; return((HRESULT)e.ErrorCode); } finally { Marshal.ReleaseComObject(il); } }
private void MyTimer_Tick3(object sender, EventArgs e) { //プリントスクリーンキー(0x2C)で判定 //右コントロールキー(0xA3) //現在押されていた場合、最上位ビットが1になる //if (((GetAsyncKeyState(0xA3) & 0x8000) >> 15) == 1) //{ // BitmapSource maskBitmap = GetIconMaskBitmap(); // BitmapSource screenBitmap = CaptureClientWithoutCursorFromDesktop(); // ICONINFO info = GetMyIconInfo2(); // //ICONINFO info = GetMyIconInfo(); // MyImage.Source = MixBitmap(screenBitmap, maskBitmap, GetCursorPositionInClient(), info.xHotspot, info.yHotspot); // Beep(1500, 10); //} //前回から押された形跡があった場合、最下位ビットが1 if ((GetAsyncKeyState(0xA3) & 1) == 1) { BitmapSource maskBitmap = GetIconMaskBitmap(); BitmapSource screenBitmap = CaptureClientWithoutCursorFromDesktop(); ICONINFO info = GetMyIconInfo2(); //ICONINFO info = GetMyIconInfo(); MyImage.Source = MixBitmap(screenBitmap, maskBitmap, GetCursorPositionInClient(), info.xHotspot, info.yHotspot); Beep(1500, 10); } }
public static ICONINFO?GetIconInfo(IntPtr hIcon) { var info = new ICONINFO(); if (GetIconInfo(hIcon, out info)) { return(info); } return(null); }
public BitmapCursor(Bitmap bmp, int HotSpotX, int HotSpotY) { iconInfo = new ICONINFO(); iconInfo.fIcon = false; iconInfo.xHotspot = 0; iconInfo.yHotspot = 0; iconInfo.hbmMask = bmp.GetHbitmap(); iconInfo.hbmColor = bmp.GetHbitmap(); Create(); }
public static Bitmap CaptureCursor(ref int x, ref int y) { //Return value initially nothing Bitmap bmp = null; CURSORINFO curInfo = new CURSORINFO(); curInfo.cbSize = Marshal.SizeOf(curInfo); //HandleMessage("Start") if (GetCursorInfo(ref curInfo)) { if (curInfo.flags == CURSOR_SHOWING) { IntPtr hicon = CopyIcon(curInfo.hCursor); if (hicon != IntPtr.Zero) { ICONINFO icoInfo = default(ICONINFO); if (GetIconInfo(hicon, ref icoInfo)) { //Delete the mask, if present. if (icoInfo.hbmMask != IntPtr.Zero) { DeleteObject(icoInfo.hbmMask); } //Delete the color bitmap, if present. if (icoInfo.hbmColor != IntPtr.Zero) { DeleteObject(icoInfo.hbmColor); } x = curInfo.ptScreenPos.X - icoInfo.xHotspot; y = curInfo.ptScreenPos.Y - icoInfo.yHotspot; } Icon ic = Icon.FromHandle(hicon); bmp = ic.ToBitmap(); //Must destroy the icon object we got from CopyIcon DestroyIcon(hicon); } } } //HandleMessage("End") return(bmp); }
/// <summary> /// Gets rid of the shadow on an icon. Thanks to Mick Doherty (http://dotnetrix.co.uk) /// </summary> /// <param name="ico"></param> /// <returns></returns> public static Bitmap IconToAlphaBitmap(Icon ico) { ICONINFO ii = new ICONINFO(); GetIconInfo(ico.Handle, out ii); Bitmap bmp = Bitmap.FromHbitmap(ii.hbmColor); DestroyIcon(ii.hbmColor); DestroyIcon(ii.hbmMask); if (Bitmap.GetPixelFormatSize(bmp.PixelFormat) < 32) { return(ico.ToBitmap()); } BitmapData bmData; Rectangle bmBounds = new Rectangle(0, 0, bmp.Width, bmp.Height); bmData = bmp.LockBits(bmBounds, ImageLockMode.ReadOnly, bmp.PixelFormat); Bitmap dstBitmap = new Bitmap(bmData.Width, bmData.Height, bmData.Stride, PixelFormat.Format32bppArgb, bmData.Scan0); bool IsAlphaBitmap = false; for (int y = 0; y <= bmData.Height - 1; y++) { for (int x = 0; x <= bmData.Width - 1; x++) { Color PixelColor = Color.FromArgb(Marshal.ReadInt32(bmData.Scan0, (bmData.Stride * y) + (4 * x))); if (PixelColor.A > 0 & PixelColor.A < 255) { IsAlphaBitmap = true; break; } } if (IsAlphaBitmap) { break; } } bmp.UnlockBits(bmData); if (IsAlphaBitmap == true) { return(new Bitmap(dstBitmap)); } else { return(new Bitmap(ico.ToBitmap())); } }
public static System.Windows.Forms.Cursor Cursor(this Bitmap input, int hotX, int hotY) { ICONINFO Info = new ICONINFO(); IntPtr Handle = input.GetHicon(); GetIconInfo(Handle, out Info); Info.xHotspot = hotX; Info.yHotspot = hotY; Info.fIcon = false; IntPtr h = CreateIconIndirect(ref Info); return new System.Windows.Forms.Cursor(h); }
private int BitmapToCursor(Bitmap bmp, int hot_x, int hot_y) { // Initialize the cursor information. ICONINFO icon_info = new ICONINFO(); IntPtr h_icon = bmp.GetHicon(); GetIconInfo(h_icon, out icon_info); icon_info.xHotspot = hot_x; icon_info.yHotspot = hot_y; icon_info.fIcon = false; // Cursor, not icon. // Create the cursor. return(CreateIconIndirect(ref icon_info).ToInt32()); }
internal System.Windows.Forms.Cursor GetFormsIcon(Bitmap bmp, int hot_x, int hot_y) { // Initialize the cursor information. ICONINFO icon_info = new ICONINFO(); IntPtr h_icon = bmp.GetHicon(); GetIconInfo(h_icon, out icon_info); icon_info.xHotspot = hot_x; icon_info.yHotspot = hot_y; icon_info.fIcon = false; // Cursor, not icon. // Create the cursor. IntPtr h_cursor = CreateIconIndirect(ref icon_info); return(new System.Windows.Forms.Cursor(h_cursor)); }
internal static IconHandle CreateIconCursor(byte[] colorArray, int width, int height, int xHotspot, int yHotspot, bool isIcon) { BitmapHandle colorBitmap = null; BitmapHandle maskBitmap = null; try { var bi = new BITMAPINFO(width, -height, 32) { bmiHeader_biCompression = NativeMethods.BI_RGB }; var bits = IntPtr.Zero; colorBitmap = UnsafeNativeMethods.CreateDIBSection(new HandleRef(null, IntPtr.Zero), ref bi, NativeMethods.DIB_RGB_COLORS, ref bits, null, 0); if (colorBitmap.IsInvalid || bits == IntPtr.Zero) { return(IconHandle.GetInvalidIcon()); } Marshal.Copy(colorArray, 0, bits, colorArray.Length); var maskArray = GenerateMaskArray(width, height, colorArray); maskBitmap = UnsafeNativeMethods.CreateBitmap(width, height, 1, 1, maskArray); if (maskBitmap.IsInvalid) { return(IconHandle.GetInvalidIcon()); } var iconInfo = new ICONINFO { fIcon = isIcon, xHotspot = xHotspot, yHotspot = yHotspot, hbmMask = maskBitmap, hbmColor = colorBitmap }; return(UnsafeNativeMethods.CreateIconIndirect(iconInfo)); } finally { colorBitmap?.Dispose(); maskBitmap?.Dispose(); } }
/// /// Creates a cursor from a bitmap and combines it with another cursor. /// public BitmapCursor(Bitmap bmp, Cursor Cursor) { iconInfo = new ICONINFO(); iconInfo.fIcon = false; iconInfo.xHotspot = 0; iconInfo.yHotspot = 0; using (Bitmap bmpdup = bmp.Clone() as Bitmap) { using (Graphics g = Graphics.FromImage(bmpdup)) { Cursor.Draw(g, new Rectangle(new Point(0, 0), Cursor.Size)); } iconInfo.hbmMask = bmpdup.GetHbitmap(); iconInfo.hbmColor = bmpdup.GetHbitmap(); Create(); } }
public static void DrawCursorEx(IntPtr hDc, int screenX, int screenY) { CURSORINFO pci; pci.cbSize = Marshal.SizeOf(typeof(CURSORINFO)); if (GetCursorInfo(out pci)) { if (pci.flags == CURSOR_SHOWING) { var hIcon = CopyIcon(pci.hCursor); try { ICONINFO pIconInfo = default(ICONINFO); try { bool result = GetIconInfo(hIcon, out pIconInfo); if (!result) { throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error()); } var pos = pci.ptScreenPos; int x = pos.x - pIconInfo.xHotspot - screenX; int y = pos.y - pIconInfo.yHotspot - screenY; result = DrawIconEx(hDc, x, y, hIcon, 0, 0, 0, IntPtr.Zero, DI_NORMAL); if (!result) { throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error()); } } finally { Gdi32.DeleteObject(pIconInfo.hbmColor); Gdi32.DeleteObject(pIconInfo.hbmMask); } } finally { DestroyIcon(hIcon); } } } }
public static Size GetSize(this Cursor cursor) { var size = Size.Empty; var info = new ICONINFO(); GetIconInfo(cursor.Handle, info); if (info.hbmColor != IntPtr.Zero) { using (var bm = Image.FromHbitmap(info.hbmColor)) size = bm.Size; } else if (info.hbmMask != IntPtr.Zero) { using (var bm = Image.FromHbitmap(info.hbmMask)) size = new Size(bm.Width, bm.Height / 2); } return(size); }
private Cursor CreateCursor(Bitmap bmp) { ICONINFO iInfo = new ICONINFO(); iInfo.fIcon = false; iInfo.xHotspot = 0; iInfo.yHotspot = 0; iInfo.hbmMask = bmp.GetHbitmap(); iInfo.hbmColor = bmp.GetHbitmap(); // Create a new pointer for the drag&drop cursor icon. IntPtr pointer = Marshal.AllocHGlobal(Marshal.SizeOf(iInfo)); Marshal.StructureToPtr(iInfo, pointer, true); IntPtr ddpointer = CreateIconIndirect(pointer); // Clean Up!!! DestroyIcon(pointer); DeleteObject(iInfo.hbmMask); DeleteObject(iInfo.hbmColor); return new Cursor(ddpointer); }
public static Size GetSize(this Cursor cursor) { var size = Size.Empty; var info = new ICONINFO(); GetIconInfo(new SafeHICON(cursor.Handle, false), info); if (!info.hbmColor.IsNull) { using (var bm = Image.FromHbitmap((IntPtr)info.hbmColor)) size = bm.Size; } else if (!info.hbmMask.IsNull) { using (var bm = Image.FromHbitmap((IntPtr)info.hbmMask)) size = new Size(bm.Width, bm.Height / 2); } return(size); }
public IntPtr update(byte[] bgra, int width, int height, bool topRowFirst = false) { var bitmap = CreateBitmap(width, height, 1, 32, null); var dc = GetDC(IntPtr.Zero); BITMAPINFOHEADER bmi = new BITMAPINFOHEADER(); bmi.Init(); bmi.biWidth = width; bmi.biHeight = topRowFirst ? -height : height; bmi.biPlanes = 1; bmi.biBitCount = 32; SetDIBits(dc, bitmap, 0, (uint)height, bgra, ref bmi, 0); ReleaseDC(IntPtr.Zero, dc); // var inf = new ICONINFO(); inf.IsIcon = true; inf.ColorBitmap = bitmap; inf.MaskBitmap = bitmap; // var icon = CreateIconIndirect(ref inf); if (icon == IntPtr.Zero) { DeleteObject(bitmap); return(IntPtr.Zero); } // if (this.bitmap != IntPtr.Zero) { DeleteObject(this.bitmap); } this.bitmap = bitmap; if (this.icon != IntPtr.Zero) { DestroyIcon(this.icon); } this.icon = icon; // return(icon); }
static Cursor CreateCursor() { using (var bmp = new Bitmap(32, 32)) { using (var g = Graphics.FromImage(bmp)) { var es = new Size(10, 10); g.DrawEllipse(Pens.Blue, new Rectangle(new Point(0, 0), es)); g.DrawEllipse(Pens.Yellow, new Rectangle(new Point(es.Width / 2, es.Height / 2), es)); g.DrawEllipse(Pens.Black, new Rectangle(new Point(es.Width, 0), es)); g.DrawEllipse(Pens.Green, new Rectangle(new Point(es.Width * 3 / 2, es.Height / 2), es)); g.DrawEllipse(Pens.Red, new Rectangle(new Point(es.Width * 2, 0), es)); } using (var tmpIcon = new Cursor(bmp.GetHicon())) { var info = new ICONINFO(); GetIconInfo(tmpIcon.Handle, info); info.xHotspot = 0; info.yHotspot = 0; info.fIcon = 0; IntPtr newIcon = CreateIconIndirect(info); return(new Cursor(newIcon)); } } }
internal CustomCursor(Bitmap bitmap, int x, int y) { ICONINFO iconInfo = new ICONINFO(); iconInfo.fIcon = false; iconInfo.xHotspot = x; iconInfo.yHotspot = y; iconInfo.hbmMask = bitmap.GetHbitmap(); iconInfo.hbmColor = bitmap.GetHbitmap(); handle = CreateIconIndirect(ref iconInfo); cursor = new Cursor(handle); }
public static extern bool GetIconInfo(IntPtr hIcon, ref ICONINFO iconInfo);
public static Bitmap IconToAlphaBitmap(Icon ico) { ICONINFO ii = new ICONINFO(); GetIconInfo(ico.Handle, out ii); Bitmap bmp = Bitmap.FromHbitmap(ii.hbmColor); DestroyIcon(ii.hbmColor); DestroyIcon(ii.hbmMask); if (Bitmap.GetPixelFormatSize(bmp.PixelFormat) < 32) return ico.ToBitmap(); BitmapData bmData; Rectangle bmBounds = new Rectangle(0,0,bmp.Width,bmp.Height); bmData = bmp.LockBits(bmBounds,ImageLockMode.ReadOnly, bmp.PixelFormat); Bitmap dstBitmap=new Bitmap(bmData.Width, bmData.Height, bmData.Stride, PixelFormat.Format32bppArgb, bmData.Scan0); bool IsAlphaBitmap = false; for (int y=0; y <= bmData.Height-1; y++) { for (int x=0; x <= bmData.Width-1; x++) { Color PixelColor = Color.FromArgb(Marshal.ReadInt32(bmData.Scan0, (bmData.Stride * y) + (4 * x))); if (PixelColor.A > 0 & PixelColor.A < 255) { IsAlphaBitmap = true; break; } } if (IsAlphaBitmap) break; } bmp.UnlockBits(bmData); if (IsAlphaBitmap==true) return new Bitmap(dstBitmap); else return new Bitmap(ico.ToBitmap()); }
public static extern bool GetIconInfo(HICON hIcon, out ICONINFO iconinfo);
public static extern bool GetIconInfo(IntPtr hIcon, [In, Out] ICONINFO info);
private static extern bool GetIconInfo(IntPtr hIcon, ref ICONINFO piconinfo);
internal static extern IntPtr CreateIconIndirect(ref ICONINFO piconInfo);
private static extern bool GetIconInfo(IntPtr hIcon, out ICONINFO iInfo);
internal static extern bool Win32GetIconInfo (IntPtr hIcon, out ICONINFO piconinfo);
static extern IntPtr CreateIconIndirect([In] ref ICONINFO piconinfo);
public static extern bool GetIconInfo(IntPtr icon, out ICONINFO info);
internal static partial bool GetIconInfo( #if NET7_0_OR_GREATER [MarshalUsing(typeof(HandleRefMarshaller))] #endif HandleRef hIcon, ref ICONINFO info);
public static extern SafeIconHandle CreateIconIndirect1(ref ICONINFO icon);
private void createIcon() { if (this.hIcon != IntPtr.Zero) { DestroyIcon(this.hIcon); this.hIcon = IntPtr.Zero; } ICONINFO piconInfo = new ICONINFO(); piconInfo.fIcon = 1; this.getIconBitmap(false, true, ref piconInfo.hBmColor); this.getIconBitmap(true, true, ref piconInfo.hBmMask); this.hIcon = CreateIconIndirect(ref piconInfo); DeleteObject(piconInfo.hBmColor); DeleteObject(piconInfo.hBmMask); }
private static Bitmap GetCursorImage(CURSORINFO cursorInfo, out ICONINFO iconInfo) { var hicon = NativeMethods.CopyIcon(cursorInfo.hCursor); if (hicon == IntPtr.Zero) { iconInfo = default(ICONINFO); return(null); } if (!NativeMethods.GetIconInfo(hicon, out iconInfo)) { return(null); } try { using (var maskBitmap = Image.FromHbitmap(iconInfo.hbmMask)) { // Is this a monochrome cursor? if (maskBitmap.Height == maskBitmap.Width * 2) { var resultBitmap = new Bitmap(maskBitmap.Width, maskBitmap.Width); using (var desktopGraphics = Graphics.FromHwnd(NativeMethods.GetDesktopWindow())) { var desktopHdc = desktopGraphics.GetHdc(); var maskHdc = NativeMethods.CreateCompatibleDC(desktopHdc); var hdbit = maskBitmap.GetHbitmap(); var oldPtr = NativeMethods.SelectObject(maskHdc, hdbit); using (var resultGraphics = Graphics.FromImage(resultBitmap)) { var resultHdc = resultGraphics.GetHdc(); // These two operation will result in a black cursor over a white background. // Later in the code, a call to MakeTransparent() will get rid of the white background. NativeMethods.BitBlt(resultHdc, 0, 0, 32, 32, maskHdc, 0, 32, TernaryRasterOperations.SRCCOPY); NativeMethods.BitBlt(resultHdc, 0, 0, 32, 32, maskHdc, 0, 0, TernaryRasterOperations.SRCINVERT); resultGraphics.ReleaseHdc(resultHdc); } var newPtr = NativeMethods.SelectObject(maskHdc, oldPtr); NativeMethods.DeleteDC(newPtr); NativeMethods.DeleteObject(hdbit); NativeMethods.DeleteDC(maskHdc); NativeMethods.DestroyIcon(hicon); desktopGraphics.ReleaseHdc(desktopHdc); } // Remove the white background from the BitBlt calls, // resulting in a black cursor over a transparent background. resultBitmap.MakeTransparent(Color.White); return(resultBitmap); } } } finally { NativeMethods.DeleteObject(iconInfo.hbmColor); NativeMethods.DeleteObject(iconInfo.hbmMask); } using (var icon = Icon.FromHandle(hicon)) { var bitmap = icon.ToBitmap(); NativeMethods.DestroyIcon(hicon); return(bitmap); } }
internal override void GetCursorInfo(IntPtr cursor, out int width, out int height, out int hotspot_x, out int hotspot_y) { ICONINFO ii = new ICONINFO (); if (!Win32GetIconInfo (cursor, out ii)) throw new Win32Exception (); width = 20; height = 20; hotspot_x = ii.xHotspot; hotspot_y = ii.yHotspot; }
public static extern IntPtr CreateIconIndirect(ref ICONINFO piconinfo);
private static extern IconHandle CreateIconIndirect([In] ref ICONINFO iconInfo);
public static extern int GetIconInfo(IntPtr hIcon, out ICONINFO piconinfo);
public static extern bool GetIconInfo(HandleRef hIcon, ref ICONINFO info);
public static CursorInfo GetCursorInfo() { int width = 16, height = 16; int hotX = 0, hotY = 0; var hicon = GetCursor(); if (hicon != IntPtr.Zero) { var iconInfo = new ICONINFO(); if (GetIconInfo(hicon, ref iconInfo)) { var bitmap = new BITMAP(); if (GetObject(iconInfo.hbmMask, Marshal.SizeOf <BITMAP>(), &bitmap) > 0) { int max = bitmap.Width * bitmap.Height / 8; var curMask = new byte[max * 2]; if (GetBitmapBits(iconInfo.hbmMask, curMask.Length, curMask) > 0) { bool hasXORMask = false; if (iconInfo.hbmColor == IntPtr.Zero) { hasXORMask = true; max /= 2; } bool empty = true; int bottom = max; for (bottom--; bottom >= 0; bottom--) { if (curMask[bottom] != 0xFF || (hasXORMask && (curMask[bottom + max] != 0))) { empty = false; break; } } if (!empty) { int top; for (top = 0; top < max; top++) { if (curMask[top] != 0xFF || (hasXORMask && (curMask[top + max] != 0))) { break; } } int byteWidth = bitmap.Width / 8; int right = (bottom % byteWidth) * 8; bottom = bottom / byteWidth; int left = top % byteWidth * 8; top = top / byteWidth; width = right - left + 1; height = bottom - top + 1; hotX = iconInfo.xHotspot - left; hotY = iconInfo.yHotspot - top; } else { width = bitmap.Width; height = bitmap.Height; hotX = iconInfo.xHotspot; hotY = iconInfo.yHotspot; } } } DeleteObject(iconInfo.hbmColor); DeleteObject(iconInfo.hbmMask); } } return(new CursorInfo { Width = width, Height = height, HotX = hotX, HotY = hotY }); }
private static extern bool GetIconInfo(IntPtr hIcon, out ICONINFO piconinfo);
extern static bool GetIconInfo(IntPtr hIcon, ref ICONINFO iconInfo);
public static extern bool GetIconInfo(IntPtr hIcon, out ICONINFO piconinfo);
private void createIcon() { if (this.hIcon != IntPtr.Zero) { DestroyIcon(this.hIcon); this.hIcon = IntPtr.Zero; } ICONINFO ii = new ICONINFO(); ii.fIcon = IMAGE_ICON; getIconBitmap(false, true, ref ii.hBmColor); getIconBitmap(true, true, ref ii.hBmMask); this.hIcon = CreateIconIndirect(ref ii); DeleteObject(ii.hBmColor); DeleteObject(ii.hBmMask); }