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; }
public Bitmap CaptureCursor(ref int x, ref int y) { Bitmap bmp; IntPtr hicon; CURSORINFO ci = new CURSORINFO(); ICONINFO icInfo; ci.cbSize = Marshal.SizeOf(ci); if (GetCursorInfo(out ci)) { if (ci.flags == CURSOR_SHOWING) { hicon = CopyIcon(ci.hCursor); if (hicon != IntPtr.Zero) { if (GetIconInfo(hicon, out icInfo)) { // See: http://www.codeproject.com/KB/cs/DesktopCaptureWithMouse.aspx if (icInfo.hbmMask != IntPtr.Zero) { DeleteObject(icInfo.hbmMask); } if(icInfo.hbmColor != IntPtr.Zero) { DeleteObject(icInfo.hbmColor); } x = ci.ptScreenPos.x - ((int)icInfo.xHotspot); y = ci.ptScreenPos.y - ((int)icInfo.yHotspot); Icon ic = Icon.FromHandle(hicon); bmp = ic.ToBitmap(); ic.Dispose(); return bmp; } } } } return null; }
public static extern bool GetCursorInfo(ref CURSORINFO cInfo);
private static extern bool GetCursorInfo(ref CURSORINFO pci);
/// <summary> /// Captures the cursor as bitmap and returns the bitmap and the position on screen of the cursor. /// </summary> public static Bitmap CaptureCursor(ref Point position) { var cursorInfo = new CURSORINFO(); cursorInfo.cbSize = Marshal.SizeOf(cursorInfo); if (!User32.GetCursorInfo(out cursorInfo)) { return(null); } if (cursorInfo.flags != CursorState.CURSOR_SHOWING) { return(null); } var hicon = User32.CopyIcon(cursorInfo.hCursor); if (hicon == IntPtr.Zero) { return(null); } if (!User32.GetIconInfo(hicon, out var iconInfo)) { return(null); } // Calculate the position respecting the hotspot offset position.X = cursorInfo.ptScreenPos.X - iconInfo.xHotspot; position.Y = cursorInfo.ptScreenPos.Y - iconInfo.yHotspot; using (var maskBitmap = Image.FromHbitmap(iconInfo.hbmMask)) { // Special handling for monchome icons if (maskBitmap.Height == maskBitmap.Width * 2) { var cursor = new Bitmap(maskBitmap.Width, maskBitmap.Width, PixelFormat.Format32bppArgb); var black = Color.FromArgb(255, 0, 0, 0); //cannot compare Color.Black because of different names var white = Color.FromArgb(255, 255, 255, 255); //cannot compare Color.White because of different names for (var y = 0; y < maskBitmap.Width; y++) { for (var x = 0; x < maskBitmap.Width; x++) { var maskPixel = maskBitmap.GetPixel(x, y); var cursorPixel = maskBitmap.GetPixel(x, y + maskBitmap.Width); if (maskPixel == white && cursorPixel == black) { cursor.SetPixel(x, y, Color.Transparent); } else if (maskPixel == black) { cursor.SetPixel(x, y, cursorPixel); } else { cursor.SetPixel(x, y, cursorPixel == black ? white : black); } } } return(cursor); } } // Just return the icon converted to a bitmap var icon = Icon.FromHandle(hicon); return(icon.ToBitmap()); }
internal static bool IsCursorSuppressed() { CURSORINFO ci = new CURSORINFO(); ci.Size = CursorInfoSize; if (GetCursorInfo(ref ci)) { if (ci.Flags == CURSOR_STATE.SUPPRESSED) { return true; } } return false; }
private static extern bool GetCursorInfo(ref CURSORINFO pci);
static Bitmap CaptureCursor(out float xOffset, out float yOffset) { xOffset = yOffset = 0; CURSORINFO cursorInfo = new CURSORINFO(); cursorInfo.cbSize = Marshal.SizeOf(cursorInfo); if (!GetCursorInfo(ref cursorInfo)) { return(null); } if (cursorInfo.flags != CURSOR_SHOWING) { return(null); } IntPtr hicon = CopyIcon(cursorInfo.hCursor); if (hicon == IntPtr.Zero) { return(null); } ICONINFO iconInfo; if (!GetIconInfo(hicon, out iconInfo)) { return(null); } xOffset = iconInfo.xHotspot; yOffset = iconInfo.yHotspot; using (Bitmap maskBitmap = Bitmap.FromHbitmap(iconInfo.hbmMask)) { // Is this a monochrome cursor? if (maskBitmap.Height == maskBitmap.Width * 2) { Bitmap resultBitmap = new Bitmap(maskBitmap.Width, maskBitmap.Width); Graphics desktopGraphics = Graphics.FromHwnd(GetDesktopWindow()); IntPtr desktopHdc = desktopGraphics.GetHdc(); IntPtr maskHdc = CreateCompatibleDC(desktopHdc); IntPtr oldPtr = SelectObject(maskHdc, maskBitmap.GetHbitmap()); using (Graphics resultGraphics = Graphics.FromImage(resultBitmap)) { IntPtr 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. BitBlt(resultHdc, 0, 0, 32, 32, maskHdc, 0, 32, TernaryRasterOperations.SRCCOPY); BitBlt(resultHdc, 0, 0, 32, 32, maskHdc, 0, 0, TernaryRasterOperations.SRCINVERT); resultGraphics.ReleaseHdc(resultHdc); } IntPtr newPtr = SelectObject(maskHdc, oldPtr); DeleteObject(newPtr); DeleteDC(maskHdc); 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); } } Icon icon = Icon.FromHandle(hicon); return(icon.ToBitmap()); }
static extern int GetCursorInfo(out CURSORINFO pci);
Bitmap CaptureCursor(ref int x, ref int y) { int TernaryRasterOperations_SRCCOPY = 0x00CC0020; int TernaryRasterOperations_SRCPAINT = 0x00EE0086; int TernaryRasterOperations_SRCAND = 0x008800C6; int TernaryRasterOperations_SRCINVERT = 0x00660046; int TernaryRasterOperations_SRCERASE = 0x00440328; int TernaryRasterOperations_NOTSRCCOPY = 0x00330008; int TernaryRasterOperations_NOTSRCERASE = 0x001100A6; int TernaryRasterOperations_MERGECOPY = 0x00C000CA; int TernaryRasterOperations_MERGEPAINT = 0x00BB0226; int TernaryRasterOperations_PATCOPY = 0x00F00021; int TernaryRasterOperations_PATPAINT = 0x00FB0A09; int TernaryRasterOperations_PATINVERT = 0x005A0049; int TernaryRasterOperations_DSTINVERT = 0x00550009; int TernaryRasterOperations_BLACKNESS = 0x00000042; int TernaryRasterOperations_WHITENESS = 0x00FF0062; int TernaryRasterOperations_CAPTUREBLT = 0x40000000; CURSORINFO cursorInfo = new CURSORINFO(); cursorInfo.cbSize = Marshal.SizeOf(cursorInfo); if (!GetCursorInfo(out cursorInfo)) { return(null); } if (cursorInfo.flags != CURSOR_SHOWING) { return(null); } IntPtr hicon = CopyIcon(cursorInfo.hCursor); if (hicon == IntPtr.Zero) { return(null); } ICONINFO iconInfo; if (!GetIconInfo(hicon, out iconInfo)) { return(null); } x = cursorInfo.ptScreenPos.x - ((int)iconInfo.xHotspot); y = cursorInfo.ptScreenPos.y - ((int)iconInfo.yHotspot); using (Bitmap maskBitmap = Bitmap.FromHbitmap(iconInfo.MaskBitmap)) { // Is this a monochrome cursor? if (maskBitmap.Height == maskBitmap.Width * 2) { Bitmap resultBitmap = new Bitmap(maskBitmap.Width, maskBitmap.Width); Graphics desktopGraphics = Graphics.FromHwnd(GetDesktopWindow()); IntPtr desktopHdc = desktopGraphics.GetHdc(); IntPtr maskHdc = CreateCompatibleDC(desktopHdc); IntPtr oldPtr = SelectObject(maskHdc, maskBitmap.GetHbitmap()); using (Graphics resultGraphics = Graphics.FromImage(resultBitmap)) { IntPtr 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. BitBlt(resultHdc, 0, 0, 32, 32, maskHdc, 0, 32, TernaryRasterOperations_SRCCOPY); BitBlt(resultHdc, 0, 0, 32, 32, maskHdc, 0, 0, TernaryRasterOperations_SRCINVERT); resultGraphics.ReleaseHdc(resultHdc); } IntPtr newPtr = SelectObject(maskHdc, oldPtr); DeleteObject(newPtr); DeleteDC(maskHdc); 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); } } Icon icon = Icon.FromHandle(hicon); return(icon.ToBitmap()); }
public static Bitmap GetScreenAreaBitmap(int x, int y, int width, int height, PixelFormat pixelFormat) { var bmp = new Bitmap(width, height); using (var graphics = Graphics.FromImage(bmp)) { #if USE_WINAPI IntPtr hdc_source = IntPtr.Zero; IntPtr hdc_destination = IntPtr.Zero; #if USE_WINAPI_METHOD_I try { hdc_source = WinAPI.GetDC(WinAPI.GetDesktopWindow()); // hdc_source = WinAPI.GetDC(IntPtr.Zero); hdc_destination = graphics.GetHdc(); WinAPI.BitBlt(hdc_destination, 0, 0, width, height, hdc_source, x, y, TernaryRasterOperations.SRCCOPY); } catch { throw; } finally { WinAPI.ReleaseDC(IntPtr.Zero, hdc_source); WinAPI.ReleaseDC(IntPtr.Zero, hdc_destination); WinAPI.DeleteDC(hdc_source); WinAPI.DeleteDC(hdc_destination); graphics.ReleaseHdc(); } #else IntPtr compatible_bitmap_handle = IntPtr.Zero; try { hdc_source = WinAPI.GetDC(WinAPI.GetDesktopWindow()); hdc_destination = WinAPI.CreateCompatibleDC(hdc_source); compatible_bitmap_handle = WinAPI.CreateCompatibleBitmap(hdc_source, width, height); WinAPI.SelectObject(hdc_destination, compatible_bitmap_handle); WinAPI.BitBlt(hdc_destination, 0, 0, width, height, hdc_source, x, y, TernaryRasterOperations.SRCCOPY); } catch { throw; } finally { WinAPI.ReleaseDC(IntPtr.Zero, hdc_source); WinAPI.ReleaseDC(IntPtr.Zero, hdc_destination); WinAPI.DeleteDC(hdc_source); WinAPI.DeleteDC(hdc_destination); WinAPI.DeleteObject(compatible_bitmap_handle); } #endif #else graphics.CopyFromScreen(x, y, 0, 0, bmp.Size); #endif var cursorInfo = new CURSORINFO(); cursorInfo.Initialize(); WinAPI.GetCursorInfo(ref cursorInfo); if (cursorInfo.hCursor != IntPtr.Zero) { var cursor = new Cursor(cursorInfo.hCursor); //IntPtr hdc_source_2 = WinAPI.GetDC(cursor.Handle); //WinAPI.BitBlt(hdc_destination, cursor.HotSpot.X, cursor.HotSpot.Y, cursor.Size.Width, cursor.Size.Height, hdc_source_2, 0, 0, TernaryRasterOperations.SRCPAINT); //WinAPI.ReleaseDC(cursor.Handle, hdc_source_2); //WinAPI.ReleaseDC(IntPtr.Zero, hdc_source_2); //WinAPI.DeleteDC(hdc_source_2); // FIXME - Cursors.IBeam is white var cursorPosition = new Point(Cursor.Position.X - x, Cursor.Position.Y - y); cursor.Draw(graphics, new Rectangle(cursorPosition, new Size(cursor.Size.Width, cursor.Size.Height))); } } Thread.Sleep(10); return(bmp.Clone(new Rectangle(0, 0, bmp.Width, bmp.Height), pixelFormat)); }
[DllImport("user32.dll")] private static extern bool GetCursorInfo(out CURSORINFO pci);
static extern int GetCursorInfo(out CURSORINFO pci);
public static extern bool GetCursorInfo(out CURSORINFO cursorinfo);
private void CaptureScreenToBitmap(Bitmap bitmap) { try { using (Graphics graphics = Graphics.FromImage(bitmap)) { graphics.CopyFromScreen(0, 0, 0, 0, new Size(screenWidth, screenHeight), CopyPixelOperation.SourceCopy); CURSORINFO cursorInfo = new CURSORINFO(); cursorInfo.cbSize = Marshal.SizeOf(cursorInfo); if (GetCursorInfo(ref cursorInfo)) { if (cursorInfo.flags == CURSOR_SHOWING) { Cursor cursor = new Cursor(cursorInfo.hCursor); cursor.Draw(graphics, new Rectangle( cursorInfo.ptScreenPos.x - cursor.HotSpot.X, cursorInfo.ptScreenPos.y - cursor.HotSpot.Y, cursor.Size.Width, cursor.Size.Height)); } } } } catch (Exception ex) { throw new ScreenshotNotAvailableException("Could not capture screenshot. The application may be running as a service that has not been granted the right to interact with the desktop.", ex); } }
private static string getCursorType() { var cursorType = ""; CURSORINFO pci = new CURSORINFO(); pci.cbSize = Marshal.SizeOf(typeof(CURSORINFO)); GetCursorInfo(ref pci); if (pci.hCursor == Cursors.Default.Handle) { cursorType = "Default"; } else if (pci.hCursor == Cursors.AppStarting.Handle) { cursorType = "AppStarting"; } else if (pci.hCursor == Cursors.Arrow.Handle) { cursorType = "Arrow"; } else if (pci.hCursor == Cursors.Cross.Handle) { cursorType = "Cross"; } else if (pci.hCursor == Cursors.Hand.Handle) { cursorType = "Hand"; } else if (pci.hCursor == Cursors.Help.Handle) { cursorType = "Help"; } else if (pci.hCursor == Cursors.HSplit.Handle) { cursorType = "HSplit"; } else if (pci.hCursor == Cursors.IBeam.Handle) { cursorType = "IBeam"; } else if (pci.hCursor == Cursors.No.Handle) { cursorType = "No"; } else if (pci.hCursor == Cursors.NoMove2D.Handle) { cursorType = "NoMove2D"; } else if (pci.hCursor == Cursors.NoMoveHoriz.Handle) { cursorType = "NoMoveHoriz"; } else if (pci.hCursor == Cursors.NoMoveVert.Handle) { cursorType = "NoMoveVert"; } else if (pci.hCursor == Cursors.PanEast.Handle) { cursorType = "PanEast"; } else if (pci.hCursor == Cursors.PanNE.Handle) { cursorType = "PanNE"; } else if (pci.hCursor == Cursors.PanNorth.Handle) { cursorType = "PanNorth"; } else if (pci.hCursor == Cursors.PanNW.Handle) { cursorType = "PanNW"; } else if (pci.hCursor == Cursors.PanSE.Handle) { cursorType = "PanSE"; } else if (pci.hCursor == Cursors.PanSouth.Handle) { cursorType = "PanSouth"; } else if (pci.hCursor == Cursors.PanSW.Handle) { cursorType = "PanSW"; } else if (pci.hCursor == Cursors.PanWest.Handle) { cursorType = "PanWest"; } else if (pci.hCursor == Cursors.SizeAll.Handle) { cursorType = "SizeAll"; } else if (pci.hCursor == Cursors.SizeNESW.Handle) { cursorType = "SizeNESW"; } else if (pci.hCursor == Cursors.SizeNS.Handle) { cursorType = "SizeNS"; } else if (pci.hCursor == Cursors.SizeNWSE.Handle) { cursorType = "SizeNWSE"; } else if (pci.hCursor == Cursors.SizeWE.Handle) { cursorType = "SizeWE"; } else if (pci.hCursor == Cursors.UpArrow.Handle) { cursorType = "UpArrow"; } else if (pci.hCursor == Cursors.VSplit.Handle) { cursorType = "VSplit"; } else if (pci.hCursor == Cursors.WaitCursor.Handle) { cursorType = "WaitCursor"; } else { cursorType = "#" + pci.hCursor.ToString(); } return(cursorType); }
static extern bool GetCursorInfo(out CURSORINFO pci);//调用api,获取光标信息
// ホットキーによる動作 protected override void WndProc(ref Message m) { const int WM_SYSCOMMAND = 0x0112; const int SC_CLOSE = 0xF060; const int WM_HOTKEY = 0x312; if (m.Msg == WM_HOTKEY && m.LParam == hotkeyPrtSc.LParam) { // カーソルを追加する場合 if (Settings.Instance.CaptureCursorMode == true) { // カーソルの情報を取得 CURSORINFO ci = new CURSORINFO(); ci.cbSize = (uint)Marshal.SizeOf(typeof(CURSORINFO)); GetCursorInfo(ref ci); // カーソルの形状を取得 ClsCapture.shotCursor = new Cursor(ci.hCursor); // カーソルのホットスポットを取得 ClsCapture.shotCursorHotspot = System.Windows.Forms.Cursor.Current.HotSpot; // カーソルの位置を取得 ClsCapture.shotPoint = System.Windows.Forms.Cursor.Position; } // ホットキーの登録を解除(念のため) hotkeyPrtSc.Unregister(); hotkeyAltPrtSc.Unregister(); // トリミングモードを実行 SetForm_Capture(); try { // PrtSrcをID 0のホットキーとして登録 hotkeyPrtSc = new ClsHotkeys(this.Handle, 0, Keys.PrintScreen); // Alt+PrtSrcをID 1のホットキーとして登録 hotkeyAltPrtSc = new ClsHotkeys(this.Handle, 1, Keys.Alt | Keys.PrintScreen); } catch (Exception ex) { // エラーメッセージ表示 MessageBox.Show("キャプチャソフトの併用はできません。" + "\n" + "他のキャプチャソフトを終了してからお試しください。", "エラー", MessageBoxButtons.OK, MessageBoxIcon.Error); // 終了(フォーム表示していないとうまく終了しない) this.Show(); Application.Exit(); } } else if (m.Msg == WM_HOTKEY && m.LParam == hotkeyAltPrtSc.LParam) { // キャプチャを取得 Bitmap bmp = ClsCapture.ActiveWindowsCapture(); // キャプチャ後の処理実行 Common.ImageCaptured(bmp); // メモ画面が開いているとき if (Common.fPreview.Visible == true) { // キャプチャ終了後メモ画面に貼り付け Common.fPreview.RichTextBox1.Paste(); } bmp.Dispose(); } else if (m.Msg == WM_SYSCOMMAND && m.WParam.ToInt32() == SC_CLOSE) { Hide(); } else { base.WndProc(ref m); } }
//ref int x, ref int y) /* static System.Drawing.Bitmap CaptureCursor() //ref int x, ref int y) { System.Drawing.Bitmap bmp; IntPtr hicon; CURSORINFO ci = new CURSORINFO(); aICONINFO icInfo; ci.cbSize = Marshal.SizeOf(ci); if (GetCursorInfo(out ci)) { if (ci.flags == CURSOR_SHOWING) { hicon = CopyIcon(ci.hCursor); if (GetIconInfo(hicon, out icInfo)) { //x = ci.ptScreenPos.x - ((int)icInfo.xHotspot); //y = ci.ptScreenPos.y - ((int)icInfo.yHotspot); System.Drawing.Icon ic = System.Drawing.Icon.FromHandle(hicon); bmp = ic.ToBitmap(); DestroyIcon(hicon); return bmp; } } } return null; }*/ static BitmapSource CaptureCursor() { IntPtr hicon; CURSORINFO ci = new CURSORINFO(); aICONINFO icInfo; ci.cbSize = Marshal.SizeOf(ci); if (GetCursorInfo(out ci)) { if (ci.flags == CURSOR_SHOWING) { hicon = CopyIcon(ci.hCursor); if (GetIconInfo(hicon, out icInfo)) { System.Drawing.Icon ic = System.Drawing.Icon.FromHandle(hicon); System.Drawing.Bitmap bmp = ic.ToBitmap(); BitmapSource retVal=System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); DestroyIcon(hicon); return retVal; } } } return null; }
public static MouseCursor GetCursorInfo() { CURSORINFO info = new CURSORINFO(); info.cbSize = Marshal.SizeOf(info); GetCursorInfo(out info); try { Cursor c = new Cursor(info.hCursor); Console.WriteLine(c.Tag); return (MouseCursor)info.hCursor.ToInt32(); } catch (Exception e) { return MouseCursor.Unknown; } }
public static void SetSystemCursor(Cursor cursor) { // Get previous cursor var pci = new CURSORINFO(); pci.cbSize = Marshal.SizeOf(typeof(CURSORINFO)); GetCursorInfo(out pci); // Get previous cursor type (the one to be replaced) _previousCursorType = GetCursorType(pci); // Copy previous cursor because next SetSystemCursor might destroy it _previousCursor = CopyImage(pci.hCursor, (uint)CopyImageType.IMAGE_CURSOR, Cursors.Default.Size.Width, Cursors.Default.Size.Height, (uint)FUFlags.LR_COPYFROMRESOURCE); // Copy the cursor to set IntPtr newCursor = CopyImage(cursor.Handle, (uint)CopyImageType.IMAGE_CURSOR, cursor.Size.Width, cursor.Size.Height, (uint)FUFlags.LR_COPYFROMRESOURCE); // Set the cursor in replacement of the current one bool r = SetSystemCursor(newCursor, (uint)_previousCursorType); if (!r) Console.WriteLine("Error: " + Marshal.GetLastWin32Error()); }
internal static extern bool GetCursorInfo(ref CURSORINFO ci);
private static OCR_SYSTEM_CURSORS GetCursorType(CURSORINFO pci) { if (pci.hCursor == LoadCursor(IntPtr.Zero, (int)OCR_SYSTEM_CURSORS.OCR_APPSTARTING)) return OCR_SYSTEM_CURSORS.OCR_APPSTARTING; if (pci.hCursor == LoadCursor(IntPtr.Zero, (int)OCR_SYSTEM_CURSORS.OCR_CROSS)) return OCR_SYSTEM_CURSORS.OCR_CROSS; if (pci.hCursor == LoadCursor(IntPtr.Zero, (int)OCR_SYSTEM_CURSORS.OCR_HAND)) return OCR_SYSTEM_CURSORS.OCR_HAND; if (pci.hCursor == LoadCursor(IntPtr.Zero, (int)OCR_SYSTEM_CURSORS.OCR_HELP)) return OCR_SYSTEM_CURSORS.OCR_HELP; if (pci.hCursor == LoadCursor(IntPtr.Zero, (int)OCR_SYSTEM_CURSORS.OCR_IBEAM)) return OCR_SYSTEM_CURSORS.OCR_IBEAM; if (pci.hCursor == LoadCursor(IntPtr.Zero, (int)OCR_SYSTEM_CURSORS.OCR_ICON)) return OCR_SYSTEM_CURSORS.OCR_ICON; if (pci.hCursor == LoadCursor(IntPtr.Zero, (int)OCR_SYSTEM_CURSORS.OCR_NO)) return OCR_SYSTEM_CURSORS.OCR_NO; if (pci.hCursor == LoadCursor(IntPtr.Zero, (int)OCR_SYSTEM_CURSORS.OCR_NORMAL)) return OCR_SYSTEM_CURSORS.OCR_NORMAL; if (pci.hCursor == LoadCursor(IntPtr.Zero, (int)OCR_SYSTEM_CURSORS.OCR_SIZE)) return OCR_SYSTEM_CURSORS.OCR_SIZE; if (pci.hCursor == LoadCursor(IntPtr.Zero, (int)OCR_SYSTEM_CURSORS.OCR_SIZEALL)) return OCR_SYSTEM_CURSORS.OCR_SIZEALL; if (pci.hCursor == LoadCursor(IntPtr.Zero, (int)OCR_SYSTEM_CURSORS.OCR_SIZENESW)) return OCR_SYSTEM_CURSORS.OCR_SIZENESW; if (pci.hCursor == LoadCursor(IntPtr.Zero, (int)OCR_SYSTEM_CURSORS.OCR_SIZENS)) return OCR_SYSTEM_CURSORS.OCR_SIZENS; if (pci.hCursor == LoadCursor(IntPtr.Zero, (int)OCR_SYSTEM_CURSORS.OCR_SIZENWSE)) return OCR_SYSTEM_CURSORS.OCR_SIZENWSE; if (pci.hCursor == LoadCursor(IntPtr.Zero, (int)OCR_SYSTEM_CURSORS.OCR_SIZEWE)) return OCR_SYSTEM_CURSORS.OCR_SIZEWE; if (pci.hCursor == LoadCursor(IntPtr.Zero, (int)OCR_SYSTEM_CURSORS.OCR_UP)) return OCR_SYSTEM_CURSORS.OCR_UP; if (pci.hCursor == LoadCursor(IntPtr.Zero, (int)OCR_SYSTEM_CURSORS.OCR_WAIT)) return OCR_SYSTEM_CURSORS.OCR_WAIT; // If the cursor has not been recognized, use the NORMAL/DEFAULT one return OCR_SYSTEM_CURSORS.OCR_NORMAL; }
private unsafe Cursor ExtractIcon(PointerInfo pointerInfo) { if (_frameInfo.PointerShapeBufferSize > pointerInfo.PtrShapeBuffer.Length) { pointerInfo.PtrShapeBuffer = new byte[_frameInfo.PointerShapeBufferSize]; } //int bufferSize; fixed(byte *ptrShapeBufferPtr = pointerInfo.PtrShapeBuffer) { _deskDupl.GetFramePointerShape(_frameInfo.PointerShapeBufferSize, (IntPtr)ptrShapeBufferPtr, out int bufferSize, out pointerInfo.ShapeInfo); } var ci = new CURSORINFO(); ci.cbSize = Marshal.SizeOf(ci); if (GetCursorInfo(out ci)) { return(new Cursor(ci.hCursor)); } return(null); /* * var width = pointerInfo.ShapeInfo.Width; * var height = pointerInfo.ShapeInfo.Height; * * * var inputArray = pointerInfo.PtrShapeBuffer; * var linePitch = pointerInfo.ShapeInfo.Pitch; * * //var maskPitch = (width + 7) >> 3; * var imageSize = bufferSize; // pointerInfo.ShapeInfo.Type == 4 ? maskPitch * height * 2 : bufferSize;}} * * var imageArray = new byte[imageSize]; * //if (pointerInfo.ShapeInfo.Type != 4) * { * // invert line order or Icon will be upside down * for (var y = 0; y < height; y++) * { * Array.Copy(inputArray, y * linePitch, imageArray, (height - y - 1) * linePitch, * linePitch); * } * } * /*else * { * // convert color mask to bitmask * for (var y = 0; y < height; y++) * { * for (var x = 0; x < width;) * { * byte andMask = 0; * byte xorMask = 0; * for (var bit = 0; bit < 8 && x < width; x++, bit++) * { * var offset = (height - y - 1) * linePitch + x * 4; * if (inputArray[offset + 0] != 0 || inputArray[offset + 1] != 0 || * inputArray[offset + 2] != 0) * xorMask |= (byte) (1 << bit); * if (inputArray[offset + 3] != 0) * andMask |= (byte) (1 << bit); * } * imageArray[y * maskPitch + ((x - 1) >> 3)] = xorMask; * imageArray[imageSize / 2 + y * maskPitch + ((x - 1) >> 3)] = andMask; * } * } * }* / * * * if (pointerInfo.ShapeInfo.Type == 1) * { * var iconBuilder = new IconBuilder * { * BmpHeader = * { * Width = (uint) width, * Height = (uint) height / 2, * BitCount = (ushort) 1, * ClrUsed = (byte) 2, * }, * Palette = * { * Data = BitPalette * }, * Image = * { * Data = imageArray * } * }; * * return iconBuilder.Build(); * } * else if (pointerInfo.ShapeInfo.Type == 2) * { * var iconBuilder = new IconBuilder * { * BmpHeader = * { * Width = (uint) width, * Height = (uint) height, * BitCount = (ushort) 32, * }, * Image = * { * Data = imageArray * } * }; * * return iconBuilder.Build(); * } * else //if (pointerInfo.ShapeInfo.Type == 4) * { * var iconBuilder = new IconBuilder * { * BmpHeader = * { * Width = (uint) width, * Height = (uint) height, * BitCount = (ushort) 24, * ClrUsed = (byte) 2, * }, * Image = * { * Data = imageArray * } * }; * * return iconBuilder.Build().; * }*/ }
//できた、アルファの問題解消 //画面全体をキャプチャして、そこから目的の領域を切り抜く方法 //メニューの項目を表示した状態もキャプチャできた private static BitmapSource Capture3() { //SystemParameters.VirtualScreenWidthはマルチモニタ環境で使うと意味がある //IntPtr hBitmap = CreateCompatibleBitmap(screenDC, (int)SystemParameters.VirtualScreenWidth, (int)SystemParameters.VirtualScreenHeight); GetClientRect(GetForegroundWindow(), out RECT clientRect);//アクティブウィンドウのクライアント領域Rect取得 int clientWidth = clientRect.Right - clientRect.Left;//実際はtopとleftは必ず0なので、rightとbottomだけでいい int clientHeight = clientRect.Bottom - clientRect.Top; ClientToScreen(GetForegroundWindow(), out POINT ClientLocate);//クライアント領域の左上座標 IntPtr screenDC = GetDC(IntPtr.Zero);//画面全体のDC IntPtr memDC = CreateCompatibleDC(screenDC);//目的のウィンドウ用DC IntPtr hBitmap = CreateCompatibleBitmap(screenDC, clientWidth, clientHeight); SelectObject(memDC, hBitmap); BitBlt(memDC, 0, 0, clientWidth, clientHeight, screenDC, ClientLocate.X, ClientLocate.Y, SRCCOPY); //カーソルの描画 GetCursorPos(out POINT cursorScreenPoint);//画面上でのカーソル位置 int cursorClientX = cursorScreenPoint.X - ClientLocate.X; int cursorClientY = cursorScreenPoint.Y - ClientLocate.Y; IntPtr cursor = GetCursor(); //カーソルの形状が見た目通りにならないことがある //DrawIcon(memDC, mpX, cursorClientY, cursor); //DrawIconEx(memDC, mpX, cursorClientY, cursor, 0, 0, 0, IntPtr.Zero, DI_DEFAULTSIZE); //DrawIconEx(memDC, mpX, cursorClientY, cursor, 0, 0, 0, IntPtr.Zero, DI_NORMAL);//NORMAL以外は表示されなかったり枠がついたりする //VB現在のマウスポインタの種類を取得したいのですが、いくら調べても方法が... - Yahoo!知恵袋 //https://detail.chiebukuro.yahoo.co.jp/qa/question_detail/q1180420296 GetIconInfo(cursor, out ICONINFO iCONINFO); //BitmapSource ibmp = Imaging.CreateBitmapSourceFromHIcon(iCONINFO.hbmColor, new Int32Rect(), BitmapSizeOptions.FromEmptyOptions());//カーソルハンドルが無効エラー var icolor = Imaging.CreateBitmapSourceFromHBitmap(iCONINFO.hbmColor, IntPtr.Zero, new Int32Rect(), BitmapSizeOptions.FromEmptyOptions()); var imask = Imaging.CreateBitmapSourceFromHBitmap(iCONINFO.hbmMask, IntPtr.Zero, new Int32Rect(), BitmapSizeOptions.FromEmptyOptions()); CURSORINFO curInfo = new CURSORINFO(); curInfo.cbSize = Marshal.SizeOf(typeof(CURSORINFO)); GetCursorInfo(out curInfo); //DrawIcon(memDC, mpX, cursorClientY, curInfo.hCursor);//かなり良くなったけど、I型アイコンのとき真っ白になる //DrawIconEx(memDC, mpX, cursorClientY, curInfo.hCursor, 0, 0, 0, IntPtr.Zero, DI_NORMAL);//これでも変わらず // c# - C#-マウスカーソルイメージのキャプチャ //https://python5.com/q/ukmbkppc //これがわかれば解決できそう //カーソルインフォ → コピーアイコン → アイコンインフォのビットマップマスク画像でI型アイコンの元?の画像取得できた IntPtr hicon = CopyIcon(curInfo.hCursor); GetIconInfo(hicon, out ICONINFO icInfo); BitmapSource imask2 = Imaging.CreateBitmapSourceFromHBitmap(icInfo.hbmMask, IntPtr.Zero, new Int32Rect(), BitmapSizeOptions.FromEmptyOptions()); //var icolor2 = Imaging.CreateBitmapSourceFromHBitmap(icInfo.hbmColor, IntPtr.Zero, new Int32Rect(), BitmapSizeOptions.FromEmptyOptions()); IntPtr maskHdc = CreateCompatibleDC(screenDC); //SelectObject(memDC, hBitmap); //IntPtr iconDC = GetDC(icInfo.hbmMask); IntPtr iconDC = GetDC(hicon); //BitBlt(memDC, 0, 0, clientWidth, clientHeight, maskHdc, 0, 32, SRCCOPY); //BitBlt(memDC, 0, 0, clientWidth, clientHeight, maskHdc, 0, 0, SRCINVERT); //BitBlt(memDC, 0, 0, 32, 32, maskHdc, 0, 32, SRCCOPY); //BitBlt(memDC, 0, 0, 32, 32, maskHdc, 0, 0, SRCINVERT); //BitBlt(memDC, 0, 0, clientWidth, clientHeight, iconDC, 0, 32, SRCCOPY); BitBlt(memDC, clientWidth, clientHeight, 32, 32, iconDC, 0, 32, SRCCOPY); //DrawIconEx(memDC, cursorClientX, cursorClientY, hicon, 0, 0, 0, IntPtr.Zero, DI_NORMAL);//カーソル位置に白四角 //DrawIconEx(memDC, 0, 0, hicon, 0, 0, 0, hicon, DI_NORMAL);//左上に白四角 //DrawIconEx(memDC, cursorClientX, cursorClientY, hicon, 32, 32, 0, IntPtr.Zero, DI_NORMAL);//カーソル位置に白四角 //DrawIconEx(memDC, 0, 0, icInfo.hbmMask, 32, 32, 0, hicon, DI_NORMAL); //DrawIconEx(memDC, cursorClientX, cursorClientY, hicon, 0, 0, 0, IntPtr.Zero, DI_COMPAT);//描画なし //DrawIconEx(memDC, cursorClientX, cursorClientY, hicon, 0, 0, 0, IntPtr.Zero, DI_MASK);//カーソル位置に白四角 //DrawIconEx(memDC, cursorClientX, cursorClientY, hicon, 0, 0, 0, IntPtr.Zero, DI_IMAGE);//カーソル位置に白四角 BitmapSource source = Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); DeleteObject(iCONINFO.hbmColor); DeleteObject(iCONINFO.hbmMask); DeleteObject(hBitmap); ReleaseDC(IntPtr.Zero, screenDC); ReleaseDC(IntPtr.Zero, memDC); DeleteObject(cursor); DestroyIcon(hicon); ReleaseDC(IntPtr.Zero, maskHdc); ReleaseDC(IntPtr.Zero, iconDC); return source; }
static extern bool GetCursorInfo(out CURSORINFO pci);
internal static extern bool GetCursorInfo(ref CURSORINFO ci);
public static extern bool GetCursorInfo(ref CURSORINFO cInfo);
private static void InputCommand(int noteId) { Process[] processes = Process.GetProcessesByName("Mordhau-Win64-Shipping"); // i refresh it on each call because it may get closed or rebooted etc if (processes.Length != 1) // if there is no game detected then f**k off { return; } IntPtr winhandle = processes[0].MainWindowHandle; if (winhandle != GetForegroundWindow()) // if the game has no focus, then f**k off { return; } CURSORINFO pci = new CURSORINFO(); pci.cbSize = Marshal.SizeOf(typeof(CURSORINFO)); GetCursorInfo(ref pci); // it stores the cursordata to the struct /* * If the cursor is showing, it means the user is typing or doesn't have the required state to play lute. */ if (ConfigManager.GetBooleanProperty(PropertyItem.DontPlayNoteWhenRequired) == true) { if (pci.flags == CURSOR_SHOWING) { return; } if (((Control.ModifierKeys & Keys.Control) == Keys.Control) || ((Control.ModifierKeys & Keys.Shift) == Keys.Shift) || (Control.ModifierKeys & Keys.Alt) == Keys.Alt) { return; } } if (AutoConsoleModeFromString(ConfigManager.GetProperty(PropertyItem.ConsoleOpenMode)) == AutoConsoleMode.New) { foreach (Process proc in processes) { PostMessage(proc.MainWindowHandle, WM_KEYDOWN, (int)ConfigManager.GetKeybindProperty(PropertyItem.OpenConsole), 0); } } foreach (char key in consoleCommand) { Enum.TryParse <Keys>("" + key, out Keys tempKey); foreach (Process proc in processes) { PostMessage(proc.MainWindowHandle, WM_KEYDOWN, (int)tempKey, 0); } } foreach (Process proc in processes) { PostMessage(proc.MainWindowHandle, WM_KEYDOWN, (int)Keys.Space, 0); } foreach (char key in noteId.ToString()) { Enum.TryParse <Keys>("NumPad" + key, out Keys tempKey); foreach (Process proc in processes) { PostMessage(proc.MainWindowHandle, WM_KEYDOWN, (int)tempKey, 0); } } foreach (Process proc in processes) { Thread.Sleep(ConfigManager.GetIntegerProperty(PropertyItem.NoteCooldown)); PostMessage(proc.MainWindowHandle, WM_KEYUP, (int)Keys.Enter, 0); } }
static extern bool GetCursorInfo(out CURSORINFO pci);
private static OCR_SYSTEM_CURSORS GetCursorType(CURSORINFO pci) { if (pci.hCursor == LoadCursor(IntPtr.Zero, (int)OCR_SYSTEM_CURSORS.OCR_APPSTARTING)) { return(OCR_SYSTEM_CURSORS.OCR_APPSTARTING); } if (pci.hCursor == LoadCursor(IntPtr.Zero, (int)OCR_SYSTEM_CURSORS.OCR_CROSS)) { return(OCR_SYSTEM_CURSORS.OCR_CROSS); } if (pci.hCursor == LoadCursor(IntPtr.Zero, (int)OCR_SYSTEM_CURSORS.OCR_HAND)) { return(OCR_SYSTEM_CURSORS.OCR_HAND); } if (pci.hCursor == LoadCursor(IntPtr.Zero, (int)OCR_SYSTEM_CURSORS.OCR_HELP)) { return(OCR_SYSTEM_CURSORS.OCR_HELP); } if (pci.hCursor == LoadCursor(IntPtr.Zero, (int)OCR_SYSTEM_CURSORS.OCR_IBEAM)) { return(OCR_SYSTEM_CURSORS.OCR_IBEAM); } if (pci.hCursor == LoadCursor(IntPtr.Zero, (int)OCR_SYSTEM_CURSORS.OCR_ICON)) { return(OCR_SYSTEM_CURSORS.OCR_ICON); } if (pci.hCursor == LoadCursor(IntPtr.Zero, (int)OCR_SYSTEM_CURSORS.OCR_NO)) { return(OCR_SYSTEM_CURSORS.OCR_NO); } if (pci.hCursor == LoadCursor(IntPtr.Zero, (int)OCR_SYSTEM_CURSORS.OCR_NORMAL)) { return(OCR_SYSTEM_CURSORS.OCR_NORMAL); } if (pci.hCursor == LoadCursor(IntPtr.Zero, (int)OCR_SYSTEM_CURSORS.OCR_SIZE)) { return(OCR_SYSTEM_CURSORS.OCR_SIZE); } if (pci.hCursor == LoadCursor(IntPtr.Zero, (int)OCR_SYSTEM_CURSORS.OCR_SIZEALL)) { return(OCR_SYSTEM_CURSORS.OCR_SIZEALL); } if (pci.hCursor == LoadCursor(IntPtr.Zero, (int)OCR_SYSTEM_CURSORS.OCR_SIZENESW)) { return(OCR_SYSTEM_CURSORS.OCR_SIZENESW); } if (pci.hCursor == LoadCursor(IntPtr.Zero, (int)OCR_SYSTEM_CURSORS.OCR_SIZENS)) { return(OCR_SYSTEM_CURSORS.OCR_SIZENS); } if (pci.hCursor == LoadCursor(IntPtr.Zero, (int)OCR_SYSTEM_CURSORS.OCR_SIZENWSE)) { return(OCR_SYSTEM_CURSORS.OCR_SIZENWSE); } if (pci.hCursor == LoadCursor(IntPtr.Zero, (int)OCR_SYSTEM_CURSORS.OCR_SIZEWE)) { return(OCR_SYSTEM_CURSORS.OCR_SIZEWE); } if (pci.hCursor == LoadCursor(IntPtr.Zero, (int)OCR_SYSTEM_CURSORS.OCR_UP)) { return(OCR_SYSTEM_CURSORS.OCR_UP); } if (pci.hCursor == LoadCursor(IntPtr.Zero, (int)OCR_SYSTEM_CURSORS.OCR_WAIT)) { return(OCR_SYSTEM_CURSORS.OCR_WAIT); } // If the cursor has not been recognized, use the NORMAL/DEFAULT one return(OCR_SYSTEM_CURSORS.OCR_NORMAL); }